Skip to content

Commit c6fa01a

Browse files
avahahnjavorszky
andcommitted
Phase 1 OpenTelemetry Proof of Concept
* configure feature for OpenTelemetry support * feature gated request fields for traceparent and tracestate headers * feature gated triggers to OpenTelemetry state machine * feature gated logic to intialize OpenTelemetry state structure * temporary call to initialize OpenTelemetry tracer * temporary logging callback * OpenTelemetry state machine to manage calls into rust crate * incoming header/field parsing for traceparent and tracestate * Rust crate holding all the tracer logic. Co-authored-by: Gabor Javorszky <[email protected]> Signed-off-by: Ava Hahn <[email protected]>
1 parent 2022427 commit c6fa01a

File tree

17 files changed

+1765
-2
lines changed

17 files changed

+1765
-2
lines changed

auto/help

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ cat << END
5252

5353
--njs enable njs library usage
5454

55+
--otel enable otel library usage
56+
5557
--debug enable debug logging
5658

5759
--fuzz=ENGINE enable fuzz testing

auto/make

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ $NXT_BUILD_DIR/sbin/$NXT_DAEMON: $NXT_BUILD_DIR/lib/$NXT_LIB_STATIC \\
362362
\$(NXT_OBJS)
363363
\$(PP_LD) \$@
364364
\$(v)\$(NXT_EXEC_LINK) -o \$@ \$(CFLAGS) \\
365-
\$(NXT_OBJS) $NXT_BUILD_DIR/lib/$NXT_LIB_STATIC \\
365+
\$(NXT_OBJS) $NXT_BUILD_DIR/lib/$NXT_LIB_STATIC src/otel/target/debug/libotel.a \\
366366
$NXT_LIBM $NXT_LIBS $NXT_LIB_AUX_LIBS
367367

368368
END

auto/options

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ NXT_CYASSL=NO
2727
NXT_POLARSSL=NO
2828

2929
NXT_NJS=NO
30+
NXT_OTEL=NO
3031

3132
NXT_TEST_BUILD_EPOLL=NO
3233
NXT_TEST_BUILD_EVENTPORT=NO
@@ -112,6 +113,7 @@ do
112113
--polarssl) NXT_POLARSSL=YES ;;
113114

114115
--njs) NXT_NJS=YES ;;
116+
--otel) NXT_OTEL=YES ;;
115117

116118
--test-build-epoll) NXT_TEST_BUILD_EPOLL=YES ;;
117119
--test-build-eventport) NXT_TEST_BUILD_EVENTPORT=YES ;;

auto/otel

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
# Copyright (C) NGINX, Inc.
3+
4+
5+
nxt_found=no
6+
NXT_HAVE_OTEL=NO
7+
8+
# Temporary build step.
9+
# later we will refactor the otel rust crate into
10+
# its own (open source) module
11+
12+
cd src/otel
13+
cargo build
14+
cd ../../
15+
16+
nxt_feature="otel"
17+
nxt_feature_name=NXT_HAVE_OTEL
18+
nxt_feature_run=no
19+
nxt_feature_libs=""
20+
nxt_feature_test="int main(void){return 0;}"
21+
22+
. auto/feature
23+
24+
if [ $nxt_found = no ]; then
25+
$echo
26+
$echo $0: error: no OpenTelemetry library found.
27+
$echo
28+
exit 1;
29+
fi
30+
31+
NXT_LIB_AUX_LIBS="$NXT_LIB_AUX_LIBS -lssl -lcrypto src/otel/target/debug/libotel.a"

auto/sources

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ if [ "$NXT_NJS" != "NO" ]; then
127127
NXT_LIB_SRCS="$NXT_LIB_SRCS src/nxt_js.c src/nxt_http_js.c src/nxt_script.c"
128128
fi
129129

130+
if [ "$NXT_OTEL" != "NO" ]; then
131+
NXT_LIB_SRCS="$NXT_LIB_SRCS src/nxt_otel.c"
132+
fi
133+
130134
NXT_LIB_EPOLL_SRCS="src/nxt_epoll_engine.c"
131135
NXT_LIB_KQUEUE_SRCS="src/nxt_kqueue_engine.c"
132136
NXT_LIB_EVENTPORT_SRCS="src/nxt_eventport_engine.c"

auto/summary

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Unit configuration summary:
3030
TLS support: ............... $NXT_OPENSSL
3131
Regex support: ............. $NXT_REGEX
3232
njs support: ............... $NXT_NJS
33+
otel support: .............. $NXT_OTEL
3334

3435
process isolation: ......... $NXT_ISOLATION
3536
cgroupv2: .................. $NXT_HAVE_CGROUP

configure

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ if [ $NXT_NJS != NO ]; then
178178
. auto/njs
179179
fi
180180

181+
if [ $NXT_OTEL != NO ]; then
182+
. auto/otel
183+
fi
184+
181185
. auto/make
182186
. auto/fuzzing
183187
. auto/summary

src/nxt_h1proto.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,10 @@ static nxt_http_field_proc_t nxt_h1p_fields[] = {
178178
{ nxt_string("Content-Length"), &nxt_http_request_content_length, 0 },
179179
{ nxt_string("Authorization"), &nxt_http_request_field,
180180
offsetof(nxt_http_request_t, authorization) },
181+
#if (NXT_HAVE_OTEL)
182+
{ nxt_string("Traceparent"), &nxt_otel_parse_traceparent, 0 },
183+
{ nxt_string("Tracestate"), &nxt_otel_parse_tracestate, 0 },
184+
#endif
181185
};
182186

183187

@@ -518,6 +522,10 @@ nxt_h1p_conn_request_init(nxt_task_t *task, void *obj, void *data)
518522
h1p->parser.discard_unsafe_fields = skcf->discard_unsafe_fields;
519523

520524
nxt_h1p_conn_request_header_parse(task, c, h1p);
525+
#if (NXT_HAVE_OTEL)
526+
nxt_otel_test_and_call_state(task, r);
527+
#endif
528+
521529
return;
522530
}
523531

@@ -1685,6 +1693,11 @@ nxt_h1p_request_discard(nxt_task_t *task, nxt_http_request_t *r,
16851693

16861694
nxt_sendbuf_drain(task, wq, b);
16871695
nxt_sendbuf_drain(task, wq, last);
1696+
1697+
#if (NXT_HAVE_OTEL)
1698+
// TODO Phase 2: Test
1699+
nxt_otel_test_and_call_state(task, r);
1700+
#endif
16881701
}
16891702

16901703

@@ -1824,6 +1837,11 @@ nxt_h1p_conn_sent(nxt_task_t *task, void *obj, void *data)
18241837
{
18251838
nxt_conn_t *c;
18261839
nxt_event_engine_t *engine;
1840+
#if (NXT_HAVE_OTEL)
1841+
nxt_http_request_t *r;
1842+
r = ((nxt_h1proto_t *) data)->request;
1843+
nxt_otel_test_and_call_state(task, r);
1844+
#endif
18271845

18281846
c = obj;
18291847

src/nxt_http.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#define _NXT_HTTP_H_INCLUDED_
99

1010
#include <nxt_regex.h>
11+
#include <nxt_otel.h>
1112

1213

1314
typedef enum {
@@ -190,6 +191,10 @@ struct nxt_http_request_s {
190191

191192
nxt_http_response_t resp;
192193

194+
#if (NXT_HAVE_OTEL)
195+
nxt_otel_state_t *otel;
196+
#endif
197+
193198
nxt_http_status_t status:16;
194199

195200
uint8_t log_route; /* 1 bit */

src/nxt_http_request.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,14 @@ nxt_http_request_create(nxt_task_t *task)
285285
task->thread->engine->requests_cnt++;
286286

287287
r->tstr_cache.var.pool = mp;
288-
288+
#if (NXT_HAVE_OTEL)
289+
r->otel = nxt_mp_zget(r->mem_pool, sizeof(nxt_otel_state_t));
290+
if (!r->otel) {
291+
goto fail;
292+
}
293+
// TODO: detect and only set if otel is configured
294+
r->otel->status = NXT_OTEL_INIT_STATE;
295+
#endif
289296
return r;
290297

291298
fail:
@@ -313,6 +320,10 @@ nxt_http_request_start(nxt_task_t *task, void *obj, void *data)
313320

314321
r = obj;
315322

323+
#if (NXT_HAVE_OTEL)
324+
nxt_otel_test_and_call_state(task, r);
325+
#endif
326+
316327
r->state = &nxt_http_request_body_state;
317328

318329
skcf = r->conf->socket_conf;
@@ -584,6 +595,10 @@ nxt_http_request_ready(nxt_task_t *task, void *obj, void *data)
584595
r = obj;
585596
action = r->conf->socket_conf->action;
586597

598+
#if (NXT_HAVE_OTEL)
599+
nxt_otel_test_and_call_state(task, r);
600+
#endif
601+
587602
if (r->chunked) {
588603
ret = nxt_http_request_chunked_transform(r);
589604
if (nxt_slow_path(ret != NXT_OK)) {
@@ -852,6 +867,11 @@ nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data)
852867

853868
r->error = 1;
854869

870+
#if (NXT_HAVE_OTEL)
871+
// TODO
872+
// PHASE 2: add error event to span
873+
#endif
874+
855875
if (nxt_fast_path(proto.any != NULL)) {
856876
nxt_http_proto[r->protocol].discard(task, r, nxt_http_buf_last(r));
857877
}

0 commit comments

Comments
 (0)