Custom Instrumentation
On this page you will learn how to manually propagate trace information into and out of your Native application.
To obtain trace headers from a transaction, use sentry_transaction_iter_headers()
. For a span, use sentry_span_iter_headers()
. Pass the returned value to the downstream service. If communication happens over HTTP, we recommend you attach all headers to the outgoing HTTP request.
Continuing a trace from an upstream service requires using sentry_transaction_context_update_from_header()
. Before starting a transaction, pass its transaction context into the previous function along with the sentry-trace
header. The transaction started with the transaction context will contain everything needed to continue the trace.
To obtain headers from a transaction so it can be continued from a downstream service, define a function which merges the headers into some aggregate object. Use the function in sentry_transaction_iter_headers()
as a callback. The following example uses sentry_value_t
as the aggregate object:
static void
copy_headers_to(const char *key, const char *value, void *userdata)
{
sentry_value_t *headers
= (sentry_value_t *)userdata;
sentry_value_set_by_key(*headers, key, value);
}
int main(int argc, char **argv) {
...
// Transaction to continue off of
sentry_transaction_context_t *tx_ctx = sentry_transaction_context_new(
"honk",
NULL,
);
sentry_transaction_t *tx = sentry_transaction_start(tx_ctx, sentry_value_new_null());
sentry_value_t *headers = sentry_value_new_object();
sentry_transaction_iter_headers(tx, copy_headers_to, (void *)headers);
}
To create a transaction as a continuation of a trace retrieved from an upstream service, pass an iterator of the incoming headers to the transaction context:
sentry_transaction_context_t *tx_ctx = sentry_transaction_context_new(
"honk",
NULL,
);
sentry_transaction_context_update_from_header(
tx_ctx,
"sentry-trace",
"atraceid-aspanid-issampled",
);
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").
- Package:
- github:getsentry/sentry-native
- Version:
- 0.7.2
- Repository:
- https://github.com/getsentry/sentry-native