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

refactor(agent): expose trace id and span id with app metadata #933

Open
wants to merge 3 commits into
base: refactor/security-agent-support
Choose a base branch
from
Open
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
22 changes: 19 additions & 3 deletions agent/csec_metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,18 @@
#include "php_includes.h"
#include "php_compat.h"
#include "php_newrelic.h"
#include "util_strings.h"

int nr_php_csec_get_metadata(const nr_php_csec_metadata_key_t key, char** p) {
const char* value = NULL;
char* id_value = NULL;
bool is_allocated = false;

if (NULL == p) {
return -1;
}

if (NULL == NRPRG(app)) {
if (NULL == NRPRG(app) || NULL == NRPRG(txn)) {
return -2;
}

Expand Down Expand Up @@ -57,15 +60,28 @@ int nr_php_csec_get_metadata(const nr_php_csec_metadata_key_t key, char** p) {
case NR_PHP_CSEC_METADATA_PLICENSE:
value = NRPRG(app)->plicense;
break;
case NR_PHP_CSEC_METADATA_TRACE_ID:
id_value = nr_txn_get_current_trace_id(NRPRG(txn));
is_allocated = true;
break;
case NR_PHP_CSEC_METADATA_SPAN_ID:
id_value = nr_txn_get_current_span_id(NRPRG(txn));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The span id will be different depending on which agent's instrumentation is called first with OAPI

is_allocated = true;
break;
default:
return -4;
}

if (NULL == value) {
if (!is_allocated && NULL == value) {
return -5;
}

*p = nr_strdup(value);
if (is_allocated) {
*p = id_value;
} else {
*p = nr_strdup(value);
}

if (NULL == *p) {
return -3;
}
Expand Down
26 changes: 15 additions & 11 deletions agent/csec_metadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,17 @@
#define CSEC_METADATA_H

typedef enum {
NR_PHP_CSEC_METADATA_HIGH_SECURITY = 1,
NR_PHP_CSEC_METADATA_ENTITY_NAME,
NR_PHP_CSEC_METADATA_ENTITY_TYPE,
NR_PHP_CSEC_METADATA_ENTITY_GUID,
NR_PHP_CSEC_METADATA_HOST_NAME,
NR_PHP_CSEC_METADATA_AGENT_RUN_ID,
NR_PHP_CSEC_METADATA_ACCOUNT_ID,
NR_PHP_CSEC_METADATA_LICENSE,
NR_PHP_CSEC_METADATA_PLICENSE
NR_PHP_CSEC_METADATA_HIGH_SECURITY = 1,
NR_PHP_CSEC_METADATA_ENTITY_NAME,
NR_PHP_CSEC_METADATA_ENTITY_TYPE,
NR_PHP_CSEC_METADATA_ENTITY_GUID,
NR_PHP_CSEC_METADATA_HOST_NAME,
NR_PHP_CSEC_METADATA_AGENT_RUN_ID,
NR_PHP_CSEC_METADATA_ACCOUNT_ID,
NR_PHP_CSEC_METADATA_LICENSE,
NR_PHP_CSEC_METADATA_PLICENSE,
NR_PHP_CSEC_METADATA_TRACE_ID,
NR_PHP_CSEC_METADATA_SPAN_ID
} nr_php_csec_metadata_key_t;

/*
Expand All @@ -33,7 +35,9 @@ typedef enum {
* -4 for invalid metadata key
* -5 for inability to retrieve metadata value
*/
extern int nr_php_csec_get_metadata(const nr_php_csec_metadata_key_t k, char** value);
typedef int (*nr_php_csec_get_metadata_t)(const nr_php_csec_metadata_key_t k, char** value);
extern int nr_php_csec_get_metadata(const nr_php_csec_metadata_key_t k,
char** value);
typedef int (*nr_php_csec_get_metadata_t)(const nr_php_csec_metadata_key_t k,
char** value);
#define NR_PHP_CSEC_GET_METADATA "nr_php_csec_get_metadata"
#endif
1 change: 0 additions & 1 deletion axiom/nr_txn.c
Original file line number Diff line number Diff line change
Expand Up @@ -3250,7 +3250,6 @@ char* nr_txn_get_current_trace_id(nrtxn_t* txn) {
if ((NULL == trace_id) || (!txn->options.distributed_tracing_enabled)) {
return NULL;
}

return nr_strdup(trace_id);
}

Expand Down
Loading