Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

otel: Add OpenTelemetry functionality to NGINX Unit #1463

Merged
merged 6 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
root = true

[{configure,{*.{c,cpp,h,go,java,js,py,rs}}]
[{configure,{*.{c,cpp,h,go,java,js,py,rs}}}]
charset = utf-8
end_of_line = lf
insert_final_newline = true
Expand Down
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
54 changes: 43 additions & 11 deletions auto/make
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

$echo "creating $NXT_MAKEFILE"


cat << END > $NXT_MAKEFILE

# Pretty print compiler etc actions...
Expand All @@ -22,6 +21,8 @@ AR = $AR

EXTRA_CFLAGS =
CFLAGS = $NXT_CFLAGS $NXT_CC_OPT $CFLAGS \$(EXTRA_CFLAGS)
RUST_FLAGS =
NXT_OTEL_LIB_LOC =

NXT_EXEC_LINK = $NXT_EXEC_LINK $NXT_LD_OPT
NXT_SHARED_LOCAL_LINK = $NXT_SHARED_LOCAL_LINK $NXT_LD_OPT
Expand Down Expand Up @@ -62,6 +63,9 @@ D := 0

ifeq (\$D,1)
CFLAGS += -O0
RUST_FLAGS += --debug
else
RUST_FLAGS += --release
endif

# Optionally disable -Werror with
Expand All @@ -76,6 +80,18 @@ END

fi

# potentially set otel lib location
if [ $NXT_OTEL = YES ]; then
cat << END >> $NXT_MAKEFILE

ifeq (\$D,1)
NXT_OTEL_LIB_LOC = $NXT_OTEL_LIB_DIR/target/debug/libotel.a
else
NXT_OTEL_LIB_LOC = $NXT_OTEL_LIB_DIR/target/release/libotel.a
endif

END
fi

# The include paths list.

Expand Down Expand Up @@ -138,14 +154,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 +375,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 +551,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 +563,24 @@ 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

NXT_OTEL_DEPS=" \
build/src/nxt_otel.o \
src/otel/src/lib.rs \
"

cat << END >> $NXT_MAKEFILE

\$(NXT_OTEL_LIB_LOC): $NXT_OTEL_DEPS
cargo build \$(RUST_FLAGS) --manifest-path $NXT_OTEL_LIB_DIR/Cargo.toml
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
54 changes: 54 additions & 0 deletions auto/otel
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

# Copyright (C) NGINX, Inc.

if [ $NXT_OTEL = YES ]; then

$echo "checking for OTEL requirements:"

$echo -n " - checking for rust compiler ... "
if [ -z $(which rustc 2>/dev/null) ]; then
$echo "not found"
exit 1;
fi
$echo "found"

$echo -n " - checking for cargo ... "
if [ -z $(which cargo 2>/dev/null) ]; then
$echo "not found."
exit 1;
fi
$echo "found"

$echo -n " - "

nxt_feature="OpenSSL library"
nxt_feature_run=yes
nxt_feature_incs=
nxt_feature_libs="-lssl -lcrypto"
nxt_feature_test="#include <openssl/ssl.h>

int main(void) {
SSL_library_init();
return 0;
}"
. auto/feature

if [ ! $nxt_found = yes ]; then
$echo
$echo $0: error: OpenTelemetry support requires OpenSSL.
$echo
exit 1;
fi

NXT_OTEL_LIBS="-lssl -lcrypto"
if [ $(which pkg-config) ]; then
NXT_OTEL_LIBS="$(pkg-config openssl --cflags --libs)"
avahahn marked this conversation as resolved.
Show resolved Hide resolved
fi
cat << END >> $NXT_AUTO_CONFIG_H

#ifndef NXT_HAVE_OTEL
#define NXT_HAVE_OTEL 1
#endif

END
fi
avahahn marked this conversation as resolved.
Show resolved Hide resolved
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
6 changes: 6 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ if [ $NXT_NJS != NO ]; then
. auto/njs
fi

NXT_OTEL_LIB_DIR=src/otel
if [ $NXT_OTEL != NO ]; then
. auto/otel
NXT_LIB_AUX_LIBS="$NXT_LIB_AUX_LIBS $NXT_OTEL_LIBS"
fi

. auto/make
. auto/fuzzing
. auto/summary
90 changes: 90 additions & 0 deletions docs/unit-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3090,6 +3090,68 @@ paths:
"404":
$ref: "#/components/responses/responseNotFound"

/config/settings/telemetry:
summary: "Endpoint for the `telemetry` object in `settings`"
get:
operationId: getSettingsTelemetry
summary: "Retrieve the `telemetry` object from settings"
description: "Retrieves the `telemetry` object that represents Unit's
[Telemetry settings](https://unit.nginx.org/configuration/#settings)."
tags:
- settings
- config
responses:
"200":
description: "Ok; the `telemetry` object exists in the configuration."
content:
application/json:
schema:
$ref: "#/components/schemas/configSettingsTelemetry"
"404":
$ref: "#/components/responses/responseNotFound"

put:
operationId: putSettingsTelemetry
summary: "Create or update the `telemetry` object in settings"
description: "Creates or updates the `telemetry` object that represents Unit's
[Telemetry settings](https://unit.nginx.org/configuration/#settings)."
tags:
- settings
- config
requestBody:
required: true
content:
application/json:
schema:
$ref: "#/components/schemas/configSettingsTelemetry"
responses:
"200":
$ref: "#/components/responses/responseOkUpdated"

"400":
$ref: "#/components/responses/responseBadRequest"

"404":
$ref: "#/components/responses/responseNotFound"

"500":
$ref: "#/components/responses/responseInternalError"

delete:
operationId: deleteSettingsTelemetry
summary: "Delete the telemetry object"
description: "Deletes the `telemetry` object from the configuration."
tags:
- settings
- config

responses:
"200":
$ref: "#/components/responses/responseOkDeleted"

"404":
$ref: "#/components/responses/responseNotFound"

/config/settings/http:
summary: "Endpoint for the `http` object in `settings`"

Expand Down Expand Up @@ -6545,10 +6607,38 @@ components:
Unit settings."

properties:
telemetry:
description: "Represents global telemetry settings in Unit."
$ref: "#/components/schemas/configSettingsTelemetry"

http:
description: "Represents global HTTP settings in Unit."
$ref: "#/components/schemas/configSettingsHttp"

# /config/settings/telemetry
configSettingsTelemetry:
avahahn marked this conversation as resolved.
Show resolved Hide resolved
type: object
description: "An object whose options represent global telemetry settings in Unit."
required: ["endpoint"]
properties:
batch_size:
type: integer
description: "Number of spans to cache before sending to telemetry collector."
default: 128

endpoint:
type: string
description: "A valid endpoint to which Unit can send OpenTelemetry spans."

protocol:
type: string
description: "Protocol to use when communicating with the aforementioned endpoint."

sampling_ratio:
type: number
default: 1
description: "A number in between 0 and 1 that describes the percent of requests traced"

# /config/settings/http
configSettingsHttp:
type: object
Expand Down
Loading