Skip to content

Commit

Permalink
otel: add build tooling to include otel code
Browse files Browse the repository at this point in the history
Adds the --otel flag to the configure command and the various build time
variables and checks that are needed in this flow.

It also includes the nxt_otel.c and nxt_otel.h files that are needed for
the rest of Unit to talk to the compiled static library that's generated
from the rust crate.

Signed-off-by: Ava Hahn <[email protected]>
Signed-off-by: Gabor Javorszky <[email protected]>

Co-authored-by: Ava Hahn <[email protected]>
  • Loading branch information
javorszky and avahahn committed Oct 21, 2024
1 parent 3f7251b commit fb15df7
Show file tree
Hide file tree
Showing 12 changed files with 577 additions and 11 deletions.
2 changes: 2 additions & 0 deletions auto/help
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ cat << END

--njs enable njs library usage

--otel enable otel library usage

--debug enable debug logging

--fuzz=ENGINE enable fuzz testing
Expand Down
39 changes: 29 additions & 10 deletions auto/make
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

$echo "creating $NXT_MAKEFILE"

if [ $NXT_OTEL = "NO" ]; then
NXT_OTEL_LIB_LOC=
NXT_OTEL_BUILD_FLAG=
NXT_OTEL_LIB_DIR=
fi

cat << END > $NXT_MAKEFILE

Expand Down Expand Up @@ -138,14 +143,14 @@ cat << END >> $NXT_MAKEFILE

libnxt: $NXT_BUILD_DIR/lib/$NXT_LIB_SHARED $NXT_BUILD_DIR/lib/$NXT_LIB_STATIC

$NXT_BUILD_DIR/lib/$NXT_LIB_SHARED: \$(NXT_LIB_OBJS)
$NXT_BUILD_DIR/lib/$NXT_LIB_SHARED: \$(NXT_LIB_OBJS) $NXT_OTEL_LIB_LOC
\$(PP_LD) \$@
\$(v)\$(NXT_SHARED_LOCAL_LINK) -o \$@ \$(NXT_LIB_OBJS) \\
$NXT_LIBM $NXT_LIBS $NXT_LIB_AUX_LIBS
$NXT_LIBM $NXT_LIBS $NXT_LIB_AUX_LIBS $NXT_OTEL_LIB_LOC

$NXT_BUILD_DIR/lib/$NXT_LIB_STATIC: \$(NXT_LIB_OBJS)
$NXT_BUILD_DIR/lib/$NXT_LIB_STATIC: \$(NXT_LIB_OBJS) $NXT_OTEL_LIB_LOC
\$(PP_AR) \$@
\$(v)$NXT_STATIC_LINK \$@ \$(NXT_LIB_OBJS)
\$(v)$NXT_STATIC_LINK \$@ \$(NXT_LIB_OBJS) $NXT_OTEL_LIB_LOC

$NXT_BUILD_DIR/lib/$NXT_LIB_UNIT_STATIC: \$(NXT_LIB_UNIT_OBJS) \\
$NXT_BUILD_DIR/share/pkgconfig/unit.pc \\
Expand Down Expand Up @@ -359,11 +364,11 @@ $echo >> $NXT_MAKEFILE
cat << END >> $NXT_MAKEFILE

$NXT_BUILD_DIR/sbin/$NXT_DAEMON: $NXT_BUILD_DIR/lib/$NXT_LIB_STATIC \\
\$(NXT_OBJS)
\$(NXT_OBJS) $NXT_OTEL_LIB_LOC
\$(PP_LD) \$@
\$(v)\$(NXT_EXEC_LINK) -o \$@ \$(CFLAGS) \\
\$(NXT_OBJS) $NXT_BUILD_DIR/lib/$NXT_LIB_STATIC \\
$NXT_LIBM $NXT_LIBS $NXT_LIB_AUX_LIBS
$NXT_LIBM $NXT_LIBS $NXT_LIB_AUX_LIBS $NXT_OTEL_LIB_LOC

END

Expand Down Expand Up @@ -535,10 +540,6 @@ cat << END > Makefile

include $NXT_MAKEFILE

.PHONY: clean
clean:
rm -rf $NXT_BUILD_DIR *.dSYM Makefile

.PHONY: help
help:
@echo "Variables to control make/build behaviour:"
Expand All @@ -551,4 +552,22 @@ help:
@echo
@echo " Variables can be combined."

.PHONY: clean
clean:
rm -rf $NXT_BUILD_DIR *.dSYM Makefile
END

if [ $NXT_OTEL = YES ]; then
cat << END >> Makefile
cd "$NXT_OTEL_LIB_DIR" && cargo clean
END

cat << END >> $NXT_MAKEFILE

$NXT_OTEL_LIB_LOC:
cd src/otel/ && \
cargo build $NXT_OTEL_BUILD_FLAG && \
cd ../../

END
fi
2 changes: 2 additions & 0 deletions auto/options
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ NXT_CYASSL=NO
NXT_POLARSSL=NO

NXT_NJS=NO
NXT_OTEL=NO

NXT_TEST_BUILD_EPOLL=NO
NXT_TEST_BUILD_EVENTPORT=NO
Expand Down Expand Up @@ -112,6 +113,7 @@ do
--polarssl) NXT_POLARSSL=YES ;;

--njs) NXT_NJS=YES ;;
--otel) NXT_OTEL=YES ;;

--test-build-epoll) NXT_TEST_BUILD_EPOLL=YES ;;
--test-build-eventport) NXT_TEST_BUILD_EVENTPORT=YES ;;
Expand Down
27 changes: 27 additions & 0 deletions auto/otel
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

# Copyright (C) NGINX, Inc.

if [ $NXT_DEBUG = YES ]; then
NXT_OTEL_LIB_DIR=src/otel/
NXT_OTEL_LIB_LOC=src/otel/target/debug/libotel.a
NXT_OTEL_BUILD_FLAG=""
else
NXT_OTEL_LIB_DIR=src/otel/
NXT_OTEL_LIB_LOC=src/otel/target/release/libotel.a
NXT_OTEL_BUILD_FLAG="--release"
fi

if [ $NXT_OTEL ]; then
NXT_OTEL_LIBS="-lssl -lcrypto"
if [ $(which pkgconf) ]; then
NXT_OTEL_LIBS="$(pkgconf openssl --cflags --libs)"
elif [ $(which pkg-config) ]; then
NXT_OTEL_LIBS="$(pkg-config openssl --cflags --libs)"
fi
NXT_LIB_AUX_LIBS="$NXT_LIB_AUX_LIBS $NXT_OTEL_LIBS"
cat << END >> $NXT_AUTO_CONFIG_H
#ifndef NXT_HAVE_OTEL
#define NXT_HAVE_OTEL 1
#endif
END
fi
4 changes: 4 additions & 0 deletions auto/sources
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ if [ "$NXT_NJS" != "NO" ]; then
NXT_LIB_SRCS="$NXT_LIB_SRCS src/nxt_js.c src/nxt_http_js.c src/nxt_script.c"
fi

if [ "$NXT_OTEL" != "NO" ]; then
NXT_LIB_SRCS="$NXT_LIB_SRCS src/nxt_otel.c"
fi

NXT_LIB_EPOLL_SRCS="src/nxt_epoll_engine.c"
NXT_LIB_KQUEUE_SRCS="src/nxt_kqueue_engine.c"
NXT_LIB_EVENTPORT_SRCS="src/nxt_eventport_engine.c"
Expand Down
1 change: 1 addition & 0 deletions auto/summary
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Unit configuration summary:
TLS support: ............... $NXT_OPENSSL
Regex support: ............. $NXT_REGEX
njs support: ............... $NXT_NJS
otel support: .............. $NXT_OTEL

process isolation: ......... $NXT_ISOLATION
cgroupv2: .................. $NXT_HAVE_CGROUP
Expand Down
4 changes: 4 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ if [ $NXT_NJS != NO ]; then
. auto/njs
fi

if [ $NXT_OTEL != NO ]; then
. auto/otel
fi

. auto/make
. auto/fuzzing
. auto/summary
7 changes: 7 additions & 0 deletions src/nxt_http.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

#include <nxt_regex.h>

#if (NXT_HAVE_OTEL)
#include <nxt_otel.h>
#endif

typedef enum {
NXT_HTTP_UNSET = -1,
Expand Down Expand Up @@ -190,6 +193,10 @@ struct nxt_http_request_s {

nxt_http_response_t resp;

#if (NXT_HAVE_OTEL)
nxt_otel_state_t *otel;
#endif

nxt_http_status_t status:16;

uint8_t log_route; /* 1 bit */
Expand Down
9 changes: 9 additions & 0 deletions src/nxt_http_error.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@
#include <nxt_http.h>


#if (NXT_HAVE_OTEL)
#include <nxt_otel.h>
#endif


static void nxt_http_request_send_error_body(nxt_task_t *task, void *r,
void *data);

Expand Down Expand Up @@ -55,6 +60,10 @@ nxt_http_request_error(nxt_task_t *task, nxt_http_request_t *r,
r->resp.content_length = NULL;
r->resp.content_length_n = NXT_HTTP_ERROR_LEN;

#if (NXT_HAVE_OTEL)
nxt_otel_request_error_path(task, r);
#endif

r->state = &nxt_http_request_send_error_body_state;

nxt_http_request_header_send(task, r,
Expand Down
10 changes: 9 additions & 1 deletion src/nxt_http_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,15 @@ nxt_http_request_create(nxt_task_t *task)
task->thread->engine->requests_cnt++;

r->tstr_cache.var.pool = mp;

#if (NXT_HAVE_OTEL)
if (nxt_otel_rs_is_init()) {
r->otel = nxt_mp_zget(r->mem_pool, sizeof(nxt_otel_state_t));
if (r->otel == NULL) {
goto fail;
}
r->otel->status = NXT_OTEL_INIT_STATE;
}
#endif
return r;

fail:
Expand Down
Loading

0 comments on commit fb15df7

Please sign in to comment.