Skip to content

Commit f6f0e93

Browse files
committed
encode_opentelemetry: tests: Add a mechanism for opt-in to cutoff staled otel payloads
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 5cade1b commit f6f0e93

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

include/cmetrics/cmt_encode_opentelemetry.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,29 @@
3333
#define CMT_ENCODE_OPENTELEMETRY_CUTOFF_ERROR 5
3434

3535
#define CMT_ENCODE_OPENTELEMETRY_CUTOFF_THRESHOLD 300000000000L /* 5 minutes in nanoseconds */
36+
#define CMT_ENCODE_OPENTELEMETRY_CUTOFF_DISABLED -1L /* disabled */
3637

3738

3839
struct cmt_opentelemetry_context
3940
{
4041
size_t resource_index;
4142
Opentelemetry__Proto__Metrics__V1__MetricsData *metrics_data;
4243
struct cmt *cmt;
44+
int use_cutoff;
45+
int64_t cutoff_threshold;
46+
};
47+
48+
struct cmt_opentelemetry_context_cutoff_opts
49+
{
50+
int use_cutoff;
51+
int64_t cutoff_threshold;
4352
};
4453

4554
cfl_sds_t cmt_encode_opentelemetry_create(struct cmt *cmt);
55+
cfl_sds_t cmt_encode_opentelemetry_create_with_cutoff(struct cmt *cmt, int use_cutoff);
56+
cfl_sds_t cmt_encode_opentelemetry_create_with_cutoff_opts(struct cmt *cmt,
57+
struct cmt_opentelemetry_context_cutoff_opts *opts);
58+
4659
void cmt_encode_opentelemetry_destroy(cfl_sds_t text);
4760

4861
#endif

src/cmt_encode_opentelemetry.c

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ static void destroy_opentelemetry_context(
257257
struct cmt_opentelemetry_context *context);
258258

259259
static struct cmt_opentelemetry_context *initialize_opentelemetry_context(
260-
struct cmt *cmt);
260+
struct cmt *cmt, struct cmt_opentelemetry_context_cutoff_opts *opts);
261261

262262
static inline Opentelemetry__Proto__Common__V1__AnyValue *cfl_variant_to_otlp_any_value(struct cfl_variant *value);
263263
static inline Opentelemetry__Proto__Common__V1__KeyValue *cfl_variant_kvpair_to_otlp_kvpair(struct cfl_kvpair *input_pair);
@@ -2138,7 +2138,7 @@ static Opentelemetry__Proto__Resource__V1__Resource *
21382138
}
21392139

21402140
static struct cmt_opentelemetry_context *initialize_opentelemetry_context(
2141-
struct cmt *cmt)
2141+
struct cmt *cmt, struct cmt_opentelemetry_context_cutoff_opts *opts)
21422142
{
21432143
struct cfl_kvlist *resource_metrics_root;
21442144
struct cfl_kvlist *scope_metrics_root;
@@ -2166,6 +2166,8 @@ static struct cmt_opentelemetry_context *initialize_opentelemetry_context(
21662166
memset(context, 0, sizeof(struct cmt_opentelemetry_context));
21672167

21682168
context->cmt = cmt;
2169+
context->use_cutoff = opts->use_cutoff;
2170+
context->cutoff_threshold = opts->cutoff_threshold;
21692171

21702172
resource = initialize_resource(resource_root, &result);
21712173

@@ -2447,8 +2449,9 @@ int pack_basic_type(struct cmt_opentelemetry_context *context,
24472449
&map->metric,
24482450
sample_index++);
24492451

2450-
if (check_staled_timestamp(&map->metric, now,
2451-
CMT_ENCODE_OPENTELEMETRY_CUTOFF_THRESHOLD)) {
2452+
if (context->use_cutoff == CMT_TRUE &&
2453+
check_staled_timestamp(&map->metric, now,
2454+
context->cutoff_threshold)) {
24522455
destroy_metric(metric);
24532456

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

2468-
if (check_staled_timestamp(&map->metric, now,
2469-
CMT_ENCODE_OPENTELEMETRY_CUTOFF_THRESHOLD)) {
2471+
if (context->use_cutoff == CMT_TRUE &&
2472+
check_staled_timestamp(&map->metric, now,
2473+
context->cutoff_threshold)) {
24702474
destroy_metric(metric);
24712475

24722476
/* Skip processing metrics which are staled over the threshold */
@@ -2527,7 +2531,8 @@ static cfl_sds_t render_opentelemetry_context_to_sds(
25272531
return result_buffer;
25282532
}
25292533

2530-
cfl_sds_t cmt_encode_opentelemetry_create(struct cmt *cmt)
2534+
cfl_sds_t cmt_encode_opentelemetry_create_with_cutoff_opts(struct cmt *cmt,
2535+
struct cmt_opentelemetry_context_cutoff_opts *opts)
25312536
{
25322537
size_t metric_index;
25332538
struct cmt_opentelemetry_context *context;
@@ -2543,7 +2548,7 @@ cfl_sds_t cmt_encode_opentelemetry_create(struct cmt *cmt)
25432548
buf = NULL;
25442549
result = 0;
25452550

2546-
context = initialize_opentelemetry_context(cmt);
2551+
context = initialize_opentelemetry_context(cmt, opts);
25472552

25482553
if (context == NULL) {
25492554
return NULL;
@@ -2638,6 +2643,24 @@ cfl_sds_t cmt_encode_opentelemetry_create(struct cmt *cmt)
26382643
return buf;
26392644
}
26402645

2646+
cfl_sds_t cmt_encode_opentelemetry_create_with_cutoff(struct cmt *cmt, int use_cutoff)
2647+
{
2648+
struct cmt_opentelemetry_context_cutoff_opts opts;
2649+
opts.use_cutoff = use_cutoff;
2650+
opts.cutoff_threshold = CMT_ENCODE_OPENTELEMETRY_CUTOFF_THRESHOLD;
2651+
2652+
return cmt_encode_opentelemetry_create_with_cutoff_opts(cmt, &opts);
2653+
}
2654+
2655+
cfl_sds_t cmt_encode_opentelemetry_create(struct cmt *cmt)
2656+
{
2657+
struct cmt_opentelemetry_context_cutoff_opts opts;
2658+
opts.use_cutoff = CMT_FALSE;
2659+
opts.cutoff_threshold = CMT_ENCODE_OPENTELEMETRY_CUTOFF_DISABLED;
2660+
2661+
return cmt_encode_opentelemetry_create_with_cutoff_opts(cmt, &opts);
2662+
}
2663+
26412664
void cmt_encode_opentelemetry_destroy(cfl_sds_t text)
26422665
{
26432666
cfl_sds_destroy(text);

tests/encoding.c

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,30 @@ void test_opentelemetry_outdated()
627627

628628
cmt = generate_encoder_test_data_with_timestamp(ts);
629629

630-
payload = cmt_encode_opentelemetry_create(cmt);
630+
payload = cmt_encode_opentelemetry_create_with_cutoff(cmt, CMT_TRUE);
631+
TEST_CHECK(NULL == payload);
632+
633+
cmt_encode_opentelemetry_destroy(payload);
634+
635+
cmt_destroy(cmt);
636+
}
637+
638+
void test_opentelemetry_outdated_with_cutoff_opts()
639+
{
640+
cfl_sds_t payload;
641+
struct cmt *cmt;
642+
uint64_t ts;
643+
struct cmt_opentelemetry_context_cutoff_opts opts;
644+
645+
opts.use_cutoff = CMT_TRUE;
646+
opts.cutoff_threshold = CMT_ENCODE_OPENTELEMETRY_CUTOFF_THRESHOLD;
647+
648+
cmt_initialize();
649+
ts = cfl_time_now() - CMT_ENCODE_OPENTELEMETRY_CUTOFF_THRESHOLD * 1.5;
650+
651+
cmt = generate_encoder_test_data_with_timestamp(ts);
652+
653+
payload = cmt_encode_opentelemetry_create_with_cutoff_opts(cmt, &opts);
631654
TEST_CHECK(NULL == payload);
632655

633656
cmt_encode_opentelemetry_destroy(payload);
@@ -1195,6 +1218,7 @@ TEST_LIST = {
11951218
{"cmt_msgpack", test_cmt_to_msgpack},
11961219
{"opentelemetry", test_opentelemetry},
11971220
{"opentelemetry_old_context", test_opentelemetry_outdated},
1221+
{"opentelemetry_cutoff_opts", test_opentelemetry_outdated_with_cutoff_opts},
11981222
{"cloudwatch_emf", test_cloudwatch_emf},
11991223
{"prometheus", test_prometheus},
12001224
{"text", test_text},

0 commit comments

Comments
 (0)