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

tracing: Add transaction tag methods #626

Merged
merged 13 commits into from
Jan 11, 2022
53 changes: 53 additions & 0 deletions include/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -1387,6 +1387,59 @@ SENTRY_EXPERIMENTAL_API sentry_value_t sentry_span_start_child(
*/
SENTRY_EXPERIMENTAL_API void sentry_span_finish(
sentry_value_t root_transaction, sentry_value_t span);

/**
* Sets a tag on a transaction to the given string value.
*
* Tags longer than 200 bytes will be truncated.
*/
SENTRY_EXPERIMENTAL_API void sentry_transaction_set_tag(
sentry_value_t transaction, const char *tag, const char *value);

/**
* Removes a tag from a transaction.
*/
SENTRY_EXPERIMENTAL_API void sentry_transaction_remove_tag(
sentry_value_t transaction, const char *tag);

/**
* Sets the given key in a transaction's "data" section to the given value.
*/
SENTRY_EXPERIMENTAL_API void sentry_transaction_set_data(
sentry_value_t transaction, const char *key, sentry_value_t value);

/**
* Removes a key from a transaction's "data" section.
*/
SENTRY_EXPERIMENTAL_API void sentry_transaction_remove_data(
sentry_value_t transaction, const char *key);

/**
* Sets a tag on a span to the given string value.
*
* Tags longer than 200 bytes will be truncated.
*/
SENTRY_EXPERIMENTAL_API void sentry_span_set_tag(
sentry_value_t span, const char *tag, const char *value);

/**
* Removes a tag from a span.
*/
SENTRY_EXPERIMENTAL_API void sentry_span_remove_tag(
sentry_value_t span, const char *tag);

/**
* Sets the given key in a span's "data" section to the given value.
*/
SENTRY_EXPERIMENTAL_API void sentry_span_set_data(
sentry_value_t span, const char *key, sentry_value_t value);

/**
* Removes a key from a span's "data" section.
*/
SENTRY_EXPERIMENTAL_API void sentry_span_remove_data(
sentry_value_t span, const char *key);

#endif

#ifdef __cplusplus
Expand Down
72 changes: 72 additions & 0 deletions src/sentry_value.c
Original file line number Diff line number Diff line change
Expand Up @@ -1254,3 +1254,75 @@ sentry_event_value_add_stacktrace(sentry_value_t event, void **ips, size_t len)

sentry_event_add_thread(event, thread);
}

void
sentry_span_set_tag(sentry_value_t span, const char *tag, const char *value)
{
sentry_value_t tags = sentry_value_get_by_key(span, "tags");
if (sentry_value_is_null(tags)) {
tags = sentry_value_new_object();
sentry_value_set_by_key(span, "tags", tags);
}

char *s = sentry__string_clonen(value, 200);
if (s) {
sentry_value_set_by_key(tags, tag, sentry__value_new_string_owned(s));
} else {
sentry_value_set_by_key(tags, tag, sentry_value_new_null());
}
}

void
sentry_span_remove_tag(sentry_value_t span, const char *tag)
{
sentry_value_t tags = sentry_value_get_by_key(span, "tags");
if (!sentry_value_is_null(tags)) {
sentry_value_remove_by_key(tags, tag);
}
}

void
sentry_span_set_data(sentry_value_t span, const char *key, sentry_value_t value)
{
sentry_value_t data = sentry_value_get_by_key(span, "data");
if (sentry_value_is_null(data)) {
data = sentry_value_new_object();
sentry_value_set_by_key(span, "data", data);
}
sentry_value_set_by_key(data, key, value);
}

void
sentry_span_remove_data(sentry_value_t span, const char *key)
{
sentry_value_t data = sentry_value_get_by_key(span, "data");
if (!sentry_value_is_null(data)) {
sentry_value_remove_by_key(data, key);
}
}

void
sentry_transaction_set_tag(
sentry_value_t transaction, const char *tag, const char *value)
{
sentry_span_set_tag(transaction, tag, value);
}

void
sentry_transaction_remove_tag(sentry_value_t transaction, const char *tag)
{
sentry_span_remove_tag(transaction, tag);
}

void
sentry_transaction_set_data(
sentry_value_t transaction, const char *key, sentry_value_t value)
{
sentry_span_set_data(transaction, key, value);
Comment on lines +1318 to +1321
Copy link
Member

Choose a reason for hiding this comment

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

I just came across this as well as I added these to the Rust SDK (as part of getsentry/sentry-rust#400)

Do transactions have data? Or do they rather have extra as inherited from the Event?

https://develop.sentry.dev/sdk/event-payloads/transaction/ does not mention either as a matter of fact.

I’m confused and don’t know what to believe anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

From discussions with @relaxolotl: Every transaction is a span and spans have data, so transactions must as well. We're also pretty sure that data and extras are the same thing.

}

void
sentry_transaction_remove_data(sentry_value_t transaction, const char *key)
{
sentry_span_remove_data(transaction, key);
}