From dc13acf1e0b3ed091797a288f1f1c0f18e2b33a6 Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Tue, 11 Jan 2022 16:19:09 +0100 Subject: [PATCH 01/10] feat(tracing): Allow setting custom span status --- include/sentry.h | 29 +++++++++++++++++++++++++++ src/sentry_value.c | 49 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 77 insertions(+), 1 deletion(-) diff --git a/include/sentry.h b/include/sentry.h index f8867c704..61bf42f85 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -1440,6 +1440,35 @@ SENTRY_EXPERIMENTAL_API void sentry_span_set_data( SENTRY_EXPERIMENTAL_API void sentry_span_remove_data( sentry_value_t span, const char *key); +typedef enum { + SENTRY_SPAN_STATUS_OK, + SENTRY_SPAN_STATUS_CANCELLED, + SENTRY_SPAN_STATUS_UNKNOWN, + SENTRY_SPAN_STATUS_INVALID_ARGUMENT, + SENTRY_SPAN_STATUS_DEADLINE_EXCEEDED, + SENTRY_SPAN_STATUS_NOT_FOUND, + SENTRY_SPAN_STATUS_ALREADY_EXISTS, + SENTRY_SPAN_STATUS_PERMISSION_DENIED, + SENTRY_SPAN_STATUS_RESOURCE_EXHAUSTED, + SENTRY_SPAN_STATUS_FAILED_PRECONDITION, + SENTRY_SPAN_STATUS_ABORTED, + SENTRY_SPAN_STATUS_OUT_OF_RANGE, + SENTRY_SPAN_STATUS_UNIMPLEMENTED, + SENTRY_SPAN_STATUS_INTERNAL_ERROR, + SENTRY_SPAN_STATUS_UNAVAILABLE, + SENTRY_SPAN_STATUS_DATA_LOSS, + SENTRY_SPAN_STATUS_UNAUTHENTICATED +} sentry_span_status_t; + +SENTRY_EXPERIMENTAL_API sentry_value_t sentry_status_to_string( + sentry_span_status_t status); + +/** + * Sets the span's status. + */ +SENTRY_EXPERIMENTAL_API void sentry_span_set_status( + sentry_value_t span, sentry_span_status_t status); + #endif #ifdef __cplusplus diff --git a/src/sentry_value.c b/src/sentry_value.c index 7a22b91f9..b1fa8f666 100644 --- a/src/sentry_value.c +++ b/src/sentry_value.c @@ -1138,7 +1138,7 @@ sentry__value_new_span(sentry_value_t parent, const char *operation) sentry_value_set_by_key( span, "span_id", sentry__value_new_span_uuid(&span_id)); - sentry_value_set_by_key(span, "status", sentry_value_new_string("ok")); + sentry_span_set_status(span, SENTRY_SPAN_STATUS_OK); // Span creation is currently aggressively pruned prior to this function so // once we're in here we definitely know that the span and its parent @@ -1326,3 +1326,50 @@ sentry_transaction_remove_data(sentry_value_t transaction, const char *key) { sentry_span_remove_data(transaction, key); } + +sentry_value_t +sentry_status_to_string(sentry_span_status_t status) +{ + switch (status) { + case SENTRY_SPAN_STATUS_OK: + return sentry_value_new_string("ok"); + case SENTRY_SPAN_STATUS_CANCELLED: + return sentry_value_new_string("cancelled"); + case SENTRY_SPAN_STATUS_UNKNOWN: + return sentry_value_new_string("unknown"); + case SENTRY_SPAN_STATUS_INVALID_ARGUMENT: + return sentry_value_new_string("invalid_argument"); + case SENTRY_SPAN_STATUS_DEADLINE_EXCEEDED: + return sentry_value_new_string("deadline_exceeded"); + case SENTRY_SPAN_STATUS_NOT_FOUND: + return sentry_value_new_string("not_found"); + case SENTRY_SPAN_STATUS_ALREADY_EXISTS: + return sentry_value_new_string("already_exists"); + case SENTRY_SPAN_STATUS_PERMISSION_DENIED: + return sentry_value_new_string("permission_denied"); + case SENTRY_SPAN_STATUS_RESOURCE_EXHAUSTED: + return sentry_value_new_string("resource_exhausted"); + case SENTRY_SPAN_STATUS_FAILED_PRECONDITION: + return sentry_value_new_string("failed_precondition"); + case SENTRY_SPAN_STATUS_ABORTED: + return sentry_value_new_string("aborted"); + case SENTRY_SPAN_STATUS_OUT_OF_RANGE: + return sentry_value_new_string("out_of_range"); + case SENTRY_SPAN_STATUS_UNIMPLEMENTED: + return sentry_value_new_string("unimplemented"); + case SENTRY_SPAN_STATUS_INTERNAL_ERROR: + return sentry_value_new_string("internal_error"); + case SENTRY_SPAN_STATUS_UNAVAILABLE: + return sentry_value_new_string("unavailable"); + case SENTRY_SPAN_STATUS_DATA_LOSS: + return sentry_value_new_string("data_loss"); + case SENTRY_SPAN_STATUS_UNAUTHENTICATED: + return sentry_value_new_string("unauthenticated"); + } +} + +void +sentry_span_set_status(sentry_value_t span, sentry_span_status_t status) +{ + sentry_value_set_by_key(span, "status", sentry_status_to_string(status)); +} From e6163fbe94b7d884c3f30e8642301badc5638bd1 Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Wed, 12 Jan 2022 11:31:13 +0100 Subject: [PATCH 02/10] Remove sentry_status_to_string from sentry.h --- include/sentry.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/include/sentry.h b/include/sentry.h index 61bf42f85..8e06d7d8d 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -1460,9 +1460,6 @@ typedef enum { SENTRY_SPAN_STATUS_UNAUTHENTICATED } sentry_span_status_t; -SENTRY_EXPERIMENTAL_API sentry_value_t sentry_status_to_string( - sentry_span_status_t status); - /** * Sets the span's status. */ From 4784e4550edbb19ca80990fa0a0d9c5647098d3a Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Wed, 12 Jan 2022 11:43:16 +0100 Subject: [PATCH 03/10] Add sentry_transaction_set_status --- include/sentry.h | 8 +++++++- src/sentry_value.c | 6 ++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/include/sentry.h b/include/sentry.h index 8e06d7d8d..49a977495 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -1461,11 +1461,17 @@ typedef enum { } sentry_span_status_t; /** - * Sets the span's status. + * Sets a span's status. */ SENTRY_EXPERIMENTAL_API void sentry_span_set_status( sentry_value_t span, sentry_span_status_t status); +/** + * Sets a transaction's status. + */ +SENTRY_EXPERIMENTAL_API void sentry_transaction_set_status( + sentry_value_t tx, sentry_span_status_t status); + #endif #ifdef __cplusplus diff --git a/src/sentry_value.c b/src/sentry_value.c index b1fa8f666..11f3c38e3 100644 --- a/src/sentry_value.c +++ b/src/sentry_value.c @@ -1373,3 +1373,9 @@ sentry_span_set_status(sentry_value_t span, sentry_span_status_t status) { sentry_value_set_by_key(span, "status", sentry_status_to_string(status)); } + +void +sentry_transaction_set_status(sentry_value_t tx, sentry_span_status_t status) +{ + sentry_span_set_status(tx, status); +} From 3543923acbb59f1caa5ac08bfaf28855cef62ab9 Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Wed, 12 Jan 2022 11:44:11 +0100 Subject: [PATCH 04/10] Extend example.c with error status --- examples/example.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/examples/example.c b/examples/example.c index 8d9a4d686..a0eabfa01 100644 --- a/examples/example.c +++ b/examples/example.c @@ -229,14 +229,23 @@ main(int argc, char **argv) } sentry_value_t tx = sentry_transaction_start(tx_ctx); + if (has_arg(argc, argv, "error-status")) { + sentry_transaction_set_status(tx, SENTRY_SPAN_STATUS_INTERNAL_ERROR); + } + if (has_arg(argc, argv, "child-spans")) { - sentry_value_t child_ctx + sentry_value_t child = sentry_span_start_child(tx, "littler.teapot", NULL); - sentry_value_t grandchild_ctx - = sentry_span_start_child(child_ctx, "littlest.teapot", NULL); + sentry_value_t grandchild + = sentry_span_start_child(child, "littlest.teapot", NULL); + + if (has_arg(argc, argv, "error-status")) { + sentry_span_set_status(child, SENTRY_SPAN_STATUS_NOT_FOUND); + sentry_span_set_status(grandchild, SENTRY_SPAN_STATUS_ALREADY_EXISTS); + } - sentry_span_finish(tx, grandchild_ctx); - sentry_span_finish(tx, child_ctx); + sentry_span_finish(tx, grandchild); + sentry_span_finish(tx, child); } sentry_transaction_finish(tx); From 9d5d581fe07c38fbcff7e102b676acefb06d0b56 Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Wed, 12 Jan 2022 12:10:11 +0100 Subject: [PATCH 05/10] Add documentation to sentry_span_status_t enum --- include/sentry.h | 40 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) diff --git a/include/sentry.h b/include/sentry.h index 49a977495..53592c5e9 100644 --- a/include/sentry.h +++ b/include/sentry.h @@ -1440,24 +1440,62 @@ SENTRY_EXPERIMENTAL_API void sentry_span_set_data( SENTRY_EXPERIMENTAL_API void sentry_span_remove_data( sentry_value_t span, const char *key); +/** + * The status of a span or transaction. + * + * See https://develop.sentry.dev/sdk/event-payloads/span/ for documentation. + */ typedef enum { + // The operation completed successfully. + // HTTP status 100..299 + successful redirects from the 3xx range. SENTRY_SPAN_STATUS_OK, + // The operation was cancelled (typically by the user). SENTRY_SPAN_STATUS_CANCELLED, + // Unknown. Any non-standard HTTP status code. + // "We do not know whether the transaction failed or succeeded." SENTRY_SPAN_STATUS_UNKNOWN, + // Client specified an invalid argument. 4xx. + // Note that this differs from FailedPrecondition. InvalidArgument + // indicates arguments that are problematic regardless of the + // state of the system. SENTRY_SPAN_STATUS_INVALID_ARGUMENT, + // Deadline expired before operation could complete. + // For operations that change the state of the system, this error may be + // returned even if the operation has been completed successfully. + // HTTP redirect loops and 504 Gateway Timeout. SENTRY_SPAN_STATUS_DEADLINE_EXCEEDED, + // 404 Not Found. Some requested entity (file or directory) was not found. SENTRY_SPAN_STATUS_NOT_FOUND, + // Already exists (409) + // Some entity that we attempted to create already exists. SENTRY_SPAN_STATUS_ALREADY_EXISTS, + // 403 Forbidden + // The caller does not have permission to execute the specified operation. SENTRY_SPAN_STATUS_PERMISSION_DENIED, + // 429 Too Many Requests + // Some resource has been exhausted, perhaps a per-user quota or perhaps + // the entire file system is out of space. SENTRY_SPAN_STATUS_RESOURCE_EXHAUSTED, + // Operation was rejected because the system is not in a state required for + // the operation's execution. SENTRY_SPAN_STATUS_FAILED_PRECONDITION, + // The operation was aborted, typically due to a concurrency issue. SENTRY_SPAN_STATUS_ABORTED, + // Operation was attempted past the valid range. SENTRY_SPAN_STATUS_OUT_OF_RANGE, + // 501 Not Implemented + // Operation is not implemented or not enabled. SENTRY_SPAN_STATUS_UNIMPLEMENTED, + // Other/generic 5xx SENTRY_SPAN_STATUS_INTERNAL_ERROR, + // 503 Service Unavailable SENTRY_SPAN_STATUS_UNAVAILABLE, + // Unrecoverable data loss or corruption SENTRY_SPAN_STATUS_DATA_LOSS, - SENTRY_SPAN_STATUS_UNAUTHENTICATED + // 401 Unauthorized (actually does mean unauthenticated according to RFC + // 7235) + // Prefer PermissionDenied if a user is logged in. + SENTRY_SPAN_STATUS_UNAUTHENTICATED, } sentry_span_status_t; /** From 929920d4ddec23d4c2d1fd02ecbe9c762de863b6 Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Wed, 12 Jan 2022 12:13:18 +0100 Subject: [PATCH 06/10] Formatting --- examples/example.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/example.c b/examples/example.c index a0eabfa01..5a75ac25b 100644 --- a/examples/example.c +++ b/examples/example.c @@ -230,7 +230,8 @@ main(int argc, char **argv) sentry_value_t tx = sentry_transaction_start(tx_ctx); if (has_arg(argc, argv, "error-status")) { - sentry_transaction_set_status(tx, SENTRY_SPAN_STATUS_INTERNAL_ERROR); + sentry_transaction_set_status( + tx, SENTRY_SPAN_STATUS_INTERNAL_ERROR); } if (has_arg(argc, argv, "child-spans")) { @@ -241,7 +242,8 @@ main(int argc, char **argv) if (has_arg(argc, argv, "error-status")) { sentry_span_set_status(child, SENTRY_SPAN_STATUS_NOT_FOUND); - sentry_span_set_status(grandchild, SENTRY_SPAN_STATUS_ALREADY_EXISTS); + sentry_span_set_status( + grandchild, SENTRY_SPAN_STATUS_ALREADY_EXISTS); } sentry_span_finish(tx, grandchild); From 5a9c5ec1265a5b1e4b4dadf23e7fe80ef97942bf Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Wed, 12 Jan 2022 13:42:43 +0100 Subject: [PATCH 07/10] Add default to switch --- src/sentry_value.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sentry_value.c b/src/sentry_value.c index 11f3c38e3..70d8846b3 100644 --- a/src/sentry_value.c +++ b/src/sentry_value.c @@ -1365,6 +1365,8 @@ sentry_status_to_string(sentry_span_status_t status) return sentry_value_new_string("data_loss"); case SENTRY_SPAN_STATUS_UNAUTHENTICATED: return sentry_value_new_string("unauthenticated"); + default: + return sentry_value_new_null(); } } From 4c938cbaee77dae3f82ca1bccc91fc27f5c54321 Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Thu, 13 Jan 2022 13:33:31 +0100 Subject: [PATCH 08/10] Fix error --- src/sentry_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sentry_core.c b/src/sentry_core.c index 99985b596..8bfaa2d5d 100644 --- a/src/sentry_core.c +++ b/src/sentry_core.c @@ -714,7 +714,7 @@ sentry_set_transaction(const char *transaction) scope->transaction = sentry__string_clone(transaction); #ifdef SENTRY_PERFORMANCE_MONITORING - if (!sentry_value_is_null(scope->span)) { + if (scope->span) { sentry_transaction_set_name(scope->span, transaction); } #endif From af49ef905a8c9a55acf76cd40cb35756f223ff1f Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Thu, 13 Jan 2022 13:34:36 +0100 Subject: [PATCH 09/10] Formatting --- src/sentry_tracing.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/sentry_tracing.c b/src/sentry_tracing.c index 13dfae398..92d16620b 100644 --- a/src/sentry_tracing.c +++ b/src/sentry_tracing.c @@ -426,7 +426,8 @@ sentry_span_set_status(sentry_span_t *span, sentry_span_status_t status) } void -sentry_transaction_set_status(sentry_transaction_t *tx, sentry_span_status_t status) +sentry_transaction_set_status( + sentry_transaction_t *tx, sentry_span_status_t status) { set_status(tx->inner, status); } From 35bdfc0295787decad21927c336b48666e015437 Mon Sep 17 00:00:00 2001 From: Sebastian Zivota Date: Thu, 13 Jan 2022 13:47:58 +0100 Subject: [PATCH 10/10] Fix another error --- examples/example.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/example.c b/examples/example.c index 10ec04c35..4d76aeede 100644 --- a/examples/example.c +++ b/examples/example.c @@ -236,7 +236,7 @@ main(int argc, char **argv) if (has_arg(argc, argv, "child-spans")) { sentry_span_t *child - = sentry_span_start_child(tx, "littler.teapot", NULL); + = sentry_transaction_start_child(tx, "littler.teapot", NULL); sentry_span_t *grandchild = sentry_span_start_child(child, "littlest.teapot", NULL);