Skip to content

Commit

Permalink
encode_opentelemetry: tests: Add a mechanism for opt-in to cutoff sta…
Browse files Browse the repository at this point in the history
…led otel payloads

Signed-off-by: Hiroshi Hatake <[email protected]>
  • Loading branch information
cosmo0920 committed Sep 30, 2024
1 parent 5cade1b commit f6f0e93
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 9 deletions.
13 changes: 13 additions & 0 deletions include/cmetrics/cmt_encode_opentelemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,29 @@
#define CMT_ENCODE_OPENTELEMETRY_CUTOFF_ERROR 5

#define CMT_ENCODE_OPENTELEMETRY_CUTOFF_THRESHOLD 300000000000L /* 5 minutes in nanoseconds */
#define CMT_ENCODE_OPENTELEMETRY_CUTOFF_DISABLED -1L /* disabled */


struct cmt_opentelemetry_context
{
size_t resource_index;
Opentelemetry__Proto__Metrics__V1__MetricsData *metrics_data;
struct cmt *cmt;
int use_cutoff;
int64_t cutoff_threshold;
};

struct cmt_opentelemetry_context_cutoff_opts
{
int use_cutoff;
int64_t cutoff_threshold;
};

cfl_sds_t cmt_encode_opentelemetry_create(struct cmt *cmt);
cfl_sds_t cmt_encode_opentelemetry_create_with_cutoff(struct cmt *cmt, int use_cutoff);
cfl_sds_t cmt_encode_opentelemetry_create_with_cutoff_opts(struct cmt *cmt,
struct cmt_opentelemetry_context_cutoff_opts *opts);

void cmt_encode_opentelemetry_destroy(cfl_sds_t text);

#endif
39 changes: 31 additions & 8 deletions src/cmt_encode_opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ static void destroy_opentelemetry_context(
struct cmt_opentelemetry_context *context);

static struct cmt_opentelemetry_context *initialize_opentelemetry_context(
struct cmt *cmt);
struct cmt *cmt, struct cmt_opentelemetry_context_cutoff_opts *opts);

static inline Opentelemetry__Proto__Common__V1__AnyValue *cfl_variant_to_otlp_any_value(struct cfl_variant *value);
static inline Opentelemetry__Proto__Common__V1__KeyValue *cfl_variant_kvpair_to_otlp_kvpair(struct cfl_kvpair *input_pair);
Expand Down Expand Up @@ -2138,7 +2138,7 @@ static Opentelemetry__Proto__Resource__V1__Resource *
}

static struct cmt_opentelemetry_context *initialize_opentelemetry_context(
struct cmt *cmt)
struct cmt *cmt, struct cmt_opentelemetry_context_cutoff_opts *opts)
{
struct cfl_kvlist *resource_metrics_root;
struct cfl_kvlist *scope_metrics_root;
Expand Down Expand Up @@ -2166,6 +2166,8 @@ static struct cmt_opentelemetry_context *initialize_opentelemetry_context(
memset(context, 0, sizeof(struct cmt_opentelemetry_context));

context->cmt = cmt;
context->use_cutoff = opts->use_cutoff;
context->cutoff_threshold = opts->cutoff_threshold;

resource = initialize_resource(resource_root, &result);

Expand Down Expand Up @@ -2447,8 +2449,9 @@ int pack_basic_type(struct cmt_opentelemetry_context *context,
&map->metric,
sample_index++);

if (check_staled_timestamp(&map->metric, now,
CMT_ENCODE_OPENTELEMETRY_CUTOFF_THRESHOLD)) {
if (context->use_cutoff == CMT_TRUE &&
check_staled_timestamp(&map->metric, now,
context->cutoff_threshold)) {
destroy_metric(metric);

/* Skip processing metrics which are staled over the threshold */
Expand All @@ -2465,8 +2468,9 @@ int pack_basic_type(struct cmt_opentelemetry_context *context,
cfl_list_foreach(head, &map->metrics) {
sample = cfl_list_entry(head, struct cmt_metric, _head);

if (check_staled_timestamp(&map->metric, now,
CMT_ENCODE_OPENTELEMETRY_CUTOFF_THRESHOLD)) {
if (context->use_cutoff == CMT_TRUE &&
check_staled_timestamp(&map->metric, now,
context->cutoff_threshold)) {
destroy_metric(metric);

/* Skip processing metrics which are staled over the threshold */
Expand Down Expand Up @@ -2527,7 +2531,8 @@ static cfl_sds_t render_opentelemetry_context_to_sds(
return result_buffer;
}

cfl_sds_t cmt_encode_opentelemetry_create(struct cmt *cmt)
cfl_sds_t cmt_encode_opentelemetry_create_with_cutoff_opts(struct cmt *cmt,
struct cmt_opentelemetry_context_cutoff_opts *opts)
{
size_t metric_index;
struct cmt_opentelemetry_context *context;
Expand All @@ -2543,7 +2548,7 @@ cfl_sds_t cmt_encode_opentelemetry_create(struct cmt *cmt)
buf = NULL;
result = 0;

context = initialize_opentelemetry_context(cmt);
context = initialize_opentelemetry_context(cmt, opts);

if (context == NULL) {
return NULL;
Expand Down Expand Up @@ -2638,6 +2643,24 @@ cfl_sds_t cmt_encode_opentelemetry_create(struct cmt *cmt)
return buf;
}

cfl_sds_t cmt_encode_opentelemetry_create_with_cutoff(struct cmt *cmt, int use_cutoff)
{
struct cmt_opentelemetry_context_cutoff_opts opts;
opts.use_cutoff = use_cutoff;
opts.cutoff_threshold = CMT_ENCODE_OPENTELEMETRY_CUTOFF_THRESHOLD;

return cmt_encode_opentelemetry_create_with_cutoff_opts(cmt, &opts);
}

cfl_sds_t cmt_encode_opentelemetry_create(struct cmt *cmt)
{
struct cmt_opentelemetry_context_cutoff_opts opts;
opts.use_cutoff = CMT_FALSE;
opts.cutoff_threshold = CMT_ENCODE_OPENTELEMETRY_CUTOFF_DISABLED;

return cmt_encode_opentelemetry_create_with_cutoff_opts(cmt, &opts);
}

void cmt_encode_opentelemetry_destroy(cfl_sds_t text)
{
cfl_sds_destroy(text);
Expand Down
26 changes: 25 additions & 1 deletion tests/encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,30 @@ void test_opentelemetry_outdated()

cmt = generate_encoder_test_data_with_timestamp(ts);

payload = cmt_encode_opentelemetry_create(cmt);
payload = cmt_encode_opentelemetry_create_with_cutoff(cmt, CMT_TRUE);
TEST_CHECK(NULL == payload);

cmt_encode_opentelemetry_destroy(payload);

cmt_destroy(cmt);
}

void test_opentelemetry_outdated_with_cutoff_opts()
{
cfl_sds_t payload;
struct cmt *cmt;
uint64_t ts;
struct cmt_opentelemetry_context_cutoff_opts opts;

opts.use_cutoff = CMT_TRUE;
opts.cutoff_threshold = CMT_ENCODE_OPENTELEMETRY_CUTOFF_THRESHOLD;

cmt_initialize();
ts = cfl_time_now() - CMT_ENCODE_OPENTELEMETRY_CUTOFF_THRESHOLD * 1.5;

cmt = generate_encoder_test_data_with_timestamp(ts);

payload = cmt_encode_opentelemetry_create_with_cutoff_opts(cmt, &opts);
TEST_CHECK(NULL == payload);

cmt_encode_opentelemetry_destroy(payload);
Expand Down Expand Up @@ -1195,6 +1218,7 @@ TEST_LIST = {
{"cmt_msgpack", test_cmt_to_msgpack},
{"opentelemetry", test_opentelemetry},
{"opentelemetry_old_context", test_opentelemetry_outdated},
{"opentelemetry_cutoff_opts", test_opentelemetry_outdated_with_cutoff_opts},
{"cloudwatch_emf", test_cloudwatch_emf},
{"prometheus", test_prometheus},
{"text", test_text},
Expand Down

0 comments on commit f6f0e93

Please sign in to comment.