Skip to content

Commit a3aaad0

Browse files
committed
tests(agent): Adds unit tests for log label creation
1 parent 67c5348 commit a3aaad0

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

agent/php_txn.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ static nr_status_t nr_php_txn_collect_label_keys_iter(const char* key,
563563
*
564564
*/
565565

566-
static nrobj_t* nr_php_txn_get_log_forwarding_labels(nrobj_t* labels) {
566+
nrobj_t* nr_php_txn_get_log_forwarding_labels(nrobj_t* labels) {
567567
nrobj_t* label_keys = NULL;
568568
nrobj_t* exclude_labels_list = NULL;
569569
nrobj_t* exclude_labels_hash = NULL;

agent/php_txn_private.h

+13
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,16 @@ extern void nr_php_txn_php_package_create_major_metric(void* value,
8686
* Params : 1. The current transaction.
8787
*/
8888
extern void nr_php_txn_create_packages_major_metrics(nrtxn_t* txn);
89+
90+
/*
91+
* Purpose : Filter the labels hash to exclude any labels that are in the
92+
* newrelic.application_logging.forwarding.labels.exclude list.
93+
*
94+
* Params : 1. The labels hash to filter.
95+
*
96+
* Returns : A new hash containing the filtered labels.
97+
* If no labels exist or all labels are excluded, then return NULL.
98+
*
99+
*/
100+
101+
extern nrobj_t* nr_php_txn_get_log_forwarding_labels(nrobj_t* labels);

agent/tests/test_txn.c

+66-1
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,69 @@ static void test_create_agent_php_version_metrics() {
288288
#undef PHP_VERSION_METRIC_BASE
289289
#undef AGENT_VERSION_METRIC_BASE
290290

291+
static void test_create_log_forwarding_labels(TSRMLS_D) {
292+
nrobj_t* labels = NULL;
293+
nrobj_t* log_labels = NULL;
294+
char* json = NULL;
295+
296+
/* Test : Create log forwarding labels with valid key/value pairs */
297+
labels = nro_new_hash();
298+
nro_set_hash_string(labels, "key1", "value1");
299+
nro_set_hash_string(labels, "key2", "value2");
300+
nro_set_hash_string(labels, "key3", "value3");
301+
302+
log_labels = nr_php_txn_get_log_forwarding_labels(labels);
303+
304+
json = nro_to_json(labels);
305+
tlib_pass_if_str_equal(
306+
"valid log label creation test",
307+
"{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}", json);
308+
309+
nr_free(json);
310+
nro_delete(labels);
311+
nro_delete(log_labels);
312+
313+
/* Test : Create log forwarding labels with empty key/value pairs */
314+
labels = nro_new_hash();
315+
nro_set_hash_string(labels, "", "");
316+
nro_set_hash_string(labels, "key", "");
317+
nro_set_hash_string(labels, "", "value");
318+
319+
log_labels = nr_php_txn_get_log_forwarding_labels(labels);
320+
321+
json = nro_to_json(labels);
322+
tlib_pass_if_str_equal("empty string log label creation test",
323+
"{\"key\":\"\"}", json);
324+
325+
nr_free(json);
326+
nro_delete(labels);
327+
nro_delete(log_labels);
328+
329+
/* Test : Create log forwarding labels with NULL key/value pairs */
330+
labels = nro_new_hash();
331+
nro_set_hash_string(labels, NULL, NULL);
332+
nro_set_hash_string(labels, "key", NULL);
333+
nro_set_hash_string(labels, NULL, "value");
334+
335+
log_labels = nr_php_txn_get_log_forwarding_labels(labels);
336+
337+
json = nro_to_json(labels);
338+
tlib_pass_if_str_equal("NULL value log label creation test", "{\"key\":\"\"}",
339+
json);
340+
341+
nr_free(json);
342+
nro_delete(labels);
343+
nro_delete(log_labels);
344+
345+
/* Test : Create log forwarding labels NULL labels object */
346+
log_labels = nr_php_txn_get_log_forwarding_labels(NULL);
347+
json = nro_to_json(labels);
348+
tlib_pass_if_str_equal("NULL object log label creation test", "null", json);
349+
350+
nr_free(json);
351+
nro_delete(log_labels);
352+
}
353+
291354
tlib_parallel_info_t parallel_info = {.suggested_nthreads = 1, .state_size = 0};
292355

293356
void test_main(void* p NRUNUSED) {
@@ -300,13 +363,15 @@ void test_main(void* p NRUNUSED) {
300363
* attribute configuration.
301364
*/
302365
tlib_php_engine_create(
303-
"newrelic.transaction_events.attributes.include=request.uri" PTSRMLS_CC);
366+
"newrelic.transaction_events.attributes.include=request."
367+
"uri" PTSRMLS_CC);
304368

305369
test_handle_fpm_error();
306370
test_max_segments_config_values();
307371
test_create_php_version_metric();
308372
test_create_agent_version_metric();
309373
test_create_agent_php_version_metrics();
374+
test_create_log_forwarding_labels();
310375

311376
tlib_php_engine_destroy();
312377
}

0 commit comments

Comments
 (0)