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
26 changes: 26 additions & 0 deletions include/sentry.h
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,32 @@ SENTRY_EXPERIMENTAL_API void sentry_transaction_set_sampled(
SENTRY_EXPERIMENTAL_API void sentry_transaction_remove_sampled(
sentry_value_t transaction);

/**
* Sets a tag on a transaction to the given string value.
*/
SENTRY_EXPERIMENTAL_API void sentry_transaction_set_tag(
sentry_value_t transaction, const char *tag, const char *value);

/**
* Removes a tag from a transaction. Returns 0 if the tag was successfully
* removed, 1 otherwise.
*/
SENTRY_EXPERIMENTAL_API int 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. Returns 0 if the key was successfully
* removed, 1 otherwise.
*/
loewenheim marked this conversation as resolved.
Show resolved Hide resolved
SENTRY_EXPERIMENTAL_API int sentry_transaction_remove_data(
sentry_value_t transaction, const char *key);

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

sentry_event_add_thread(event, thread);
}

void
sentry_transaction_set_tag(sentry_value_t transaction, const char *tag, const char *value)
{
sentry_value_t tags = sentry_value_get_by_key(transaction, "tags");
if (sentry_value_is_null(tags)) {
tags = sentry_value_new_object();
sentry_value_set_by_key(transaction, "tags", tags);
}
sentry_value_t value_str = sentry_value_new_string(value);
sentry_value_set_by_key(tags, tag, value_str);
}

int
sentry_transaction_remove_tag(sentry_value_t transaction, const char *tag)
{
sentry_value_t tags = sentry_value_get_by_key(transaction, "tags");
if (!sentry_value_is_null(tags)) {
return sentry_value_remove_by_key(tags, tag);
}

return 1;
}

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

int
sentry_transaction_remove_data(sentry_value_t transaction, const char *key)
{
sentry_value_t data = sentry_value_get_by_key(transaction, "data");
if (!sentry_value_is_null(data)) {
return sentry_value_remove_by_key(data, key);
}

return 1;
}