Skip to content

Commit

Permalink
otel: configuration items and their validation
Browse files Browse the repository at this point in the history
Adds code responsible for users to apply the `telemetry` configuration
options.

configuration snippet as follows:
{
    "settings": {
        "telemetry": {
            "batch_size": 20,
            "endpoint": "http://lgtm:4318/v1/traces",
            "protocol": "http",
            "sampling_ratio": 1
        }
    },
    "listeners": {
        "*:80": {
            "pass": "routes"
        }
    },
    "routes": [
        {
            "match": {
                "headers": {
                    "accept": "*text/html*"
                }
            },
            "action": {
                "share": "/usr/share/unit/welcome/welcome.html"
            }
        },
        {
            "action": {
                "share": "/usr/share/unit/welcome/welcome.md"
            }
        }
    ]
}

Signed-off-by: Ava Hahn <a.hahn@f5.com>
Signed-off-by: Gabor Javorszky <g.javorszky@f5.com>

Co-authored-by: Ava Hahn <a.hahn@f5.com>
  • Loading branch information
javorszky and avahahn committed Oct 28, 2024
1 parent 1df311e commit 215d20c
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
103 changes: 103 additions & 0 deletions src/nxt_conf_validation.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ static nxt_int_t nxt_conf_vldt_js_module_element(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value);
#endif

#if (NXT_HAVE_OTEL)
static nxt_int_t nxt_otel_validate_batch_size(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data);
static nxt_int_t nxt_otel_validate_sample_ratio(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data);
static nxt_int_t nxt_otel_validate_protocol(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value, void *data);
#endif


static nxt_conf_vldt_object_t nxt_conf_vldt_setting_members[];
static nxt_conf_vldt_object_t nxt_conf_vldt_http_members[];
Expand Down Expand Up @@ -307,6 +316,33 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_root_members[] = {
};



#if (NXT_HAVE_OTEL)
static nxt_conf_vldt_object_t nxt_conf_vldt_otel_members[] = {
{
.name = nxt_string("endpoint"),
.type = NXT_CONF_VLDT_STRING,
.flags = NXT_CONF_VLDT_REQUIRED
}, {
.name = nxt_string("batch_size"),
.type = NXT_CONF_VLDT_INTEGER,
.validator = nxt_otel_validate_batch_size,
}, {
.name = nxt_string("protocol"),
.type = NXT_CONF_VLDT_STRING,
.validator = nxt_otel_validate_protocol,
.flags = NXT_CONF_VLDT_REQUIRED
}, {
.name = nxt_string("sampling_ratio"),
.type = NXT_CONF_VLDT_NUMBER,
.validator = nxt_otel_validate_sample_ratio,
},

NXT_CONF_VLDT_END
};
#endif


static nxt_conf_vldt_object_t nxt_conf_vldt_setting_members[] = {
{
.name = nxt_string("listen_threads"),
Expand All @@ -317,6 +353,13 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_setting_members[] = {
.type = NXT_CONF_VLDT_OBJECT,
.validator = nxt_conf_vldt_object,
.u.members = nxt_conf_vldt_http_members,
#if (NXT_HAVE_OTEL)
}, {
.name = nxt_string("telemetry"),
.type = NXT_CONF_VLDT_OBJECT,
.validator = nxt_conf_vldt_object,
.u.members = nxt_conf_vldt_otel_members,
#endif
#if (NXT_HAVE_NJS)
}, {
.name = nxt_string("js_module"),
Expand Down Expand Up @@ -1465,6 +1508,66 @@ nxt_conf_validate(nxt_conf_validation_t *vldt)
"a number, a string, an array, or an object"



#if (NXT_HAVE_OTEL)
nxt_int_t
nxt_otel_validate_batch_size(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value,
void *data)
{
double batch_size;

batch_size = nxt_conf_get_number(value);
if (batch_size <= 0) {
return NXT_ERROR;
}

return NXT_OK;
}

nxt_int_t
nxt_otel_validate_sample_ratio(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value,
void *data)
{
double sample_ratio;

sample_ratio = nxt_conf_get_number(value);
if (sample_ratio < 0 || sample_ratio > 1) {
return NXT_ERROR;
}

return NXT_OK;
}

nxt_int_t
nxt_otel_validate_protocol(nxt_conf_validation_t *vldt,
nxt_conf_value_t *value,
void *data)
{
nxt_str_t proto;

nxt_conf_get_string(value, &proto);
if (nxt_str_eq(&proto, "HTTP", 4)
|| nxt_str_eq(&proto, "http", 4))
{
goto happy;
}

if (nxt_str_eq(&proto, "GRPC", 4)
|| nxt_str_eq(&proto, "grpc", 4))
{
goto happy;
}

return NXT_ERROR;

happy:
return NXT_OK;
}
#endif


static nxt_int_t
nxt_conf_vldt_type(nxt_conf_validation_t *vldt, const nxt_str_t *name,
nxt_conf_value_t *value, nxt_conf_vldt_type_t type)
Expand Down
50 changes: 50 additions & 0 deletions src/nxt_router.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#include <nxt_port_queue.h>

#define NXT_SHARED_PORT_ID 0xFFFFu
#if (NXT_HAVE_OTEL)
#define NXT_OTEL_BATCH_DEFAULT 128
#define NXT_OTEL_SAMPLING_DEFAULT 1
#endif

typedef struct {
nxt_str_t type;
Expand Down Expand Up @@ -1613,6 +1617,13 @@ static nxt_conf_map_t nxt_router_websocket_conf[] = {
};


#if (NXT_HAVE_OTEL)
static void nxt_otel_log_callback(u_char *arg)
{
printf("otel: %s", (char *) arg);
}
#endif

static nxt_int_t
nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
u_char *start, u_char *end)
Expand All @@ -1635,6 +1646,11 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
#endif
#if (NXT_HAVE_NJS)
nxt_conf_value_t *js_module;
#endif
#if (NXT_HAVE_OTEL)
nxt_conf_value_t *otel, *otel_endpoint, *otel_sampling, *otel_batching, *otel_proto;
nxt_str_t telemetry_endpoint, telemetry_proto;
double telemetry_sample_fraction, telemetry_batching;
#endif
nxt_conf_value_t *root, *conf, *http, *value, *websocket;
nxt_conf_value_t *applications, *application, *settings;
Expand Down Expand Up @@ -1671,6 +1687,13 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,
nxt_string("/settings/http/websocket");
static const nxt_str_t forwarded_path = nxt_string("/forwarded");
static const nxt_str_t client_ip_path = nxt_string("/client_ip");
#if (NXT_HAVE_OTEL)
static const nxt_str_t telemetry_path = nxt_string("/settings/telemetry");
static const nxt_str_t telemetry_endpoint_path = nxt_string("/settings/telemetry/endpoint");
static const nxt_str_t telemetry_batch_path = nxt_string("/settings/telemetry/batch_size");
static const nxt_str_t telemetry_sample_path = nxt_string("/settings/telemetry/sampling_ratio");
static const nxt_str_t telemetry_proto_path = nxt_string("/settings/telemetry/protocol");
#endif

root = nxt_conf_json_parse(tmcf->mem_pool, start, end, NULL);
if (root == NULL) {
Expand Down Expand Up @@ -2172,6 +2195,33 @@ nxt_router_conf_create(nxt_task_t *task, nxt_router_temp_conf_t *tmcf,

#endif

#if (NXT_HAVE_OTEL)
otel = nxt_conf_get_path(root, &telemetry_path);
otel_endpoint = nxt_conf_get_path(root, &telemetry_endpoint_path);
otel_batching = nxt_conf_get_path(root, &telemetry_batch_path);
otel_sampling = nxt_conf_get_path(root, &telemetry_sample_path);
otel_proto = nxt_conf_get_path(root, &telemetry_proto_path);

if (otel) {
nxt_conf_get_string(otel_endpoint, &telemetry_endpoint);
nxt_conf_get_string(otel_proto, &telemetry_proto);
telemetry_batching = otel_batching
? nxt_conf_get_number(otel_batching)
: NXT_OTEL_BATCH_DEFAULT;
telemetry_sample_fraction = otel_sampling
? nxt_conf_get_number(otel_sampling)
: NXT_OTEL_SAMPLING_DEFAULT;

nxt_otel_rs_init(&nxt_otel_log_callback,
&telemetry_endpoint,
&telemetry_proto,
telemetry_sample_fraction,
telemetry_batching);
} else {
nxt_otel_rs_uninit();
}
#endif

nxt_queue_add(&deleting_sockets, &router->sockets);
nxt_queue_init(&router->sockets);

Expand Down

0 comments on commit 215d20c

Please sign in to comment.