Skip to content

Commit

Permalink
feat(otel): capture error message in traces (again) (#14389)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbolduc committed Jun 27, 2024
1 parent 113730d commit 36f6bd4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
3 changes: 3 additions & 0 deletions google/cloud/internal/opentelemetry.cc
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,11 @@ void EndSpanImpl(opentelemetry::trace::Span& span, Status const& status) {
span.End();
return;
}
// Note that the Cloud Trace UI drops the span's status, so we also write it
// as an attribute.
span.SetStatus(opentelemetry::trace::StatusCode::kError, status.message());
span.SetAttribute("gl-cpp.status_code", static_cast<int>(status.code()));
span.SetAttribute("gl-cpp.error.message", status.message());
auto const& ei = status.error_info();
if (!ei.reason().empty()) {
span.SetAttribute("gcloud.error.reason", ei.reason());
Expand Down
7 changes: 6 additions & 1 deletion google/cloud/internal/opentelemetry_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,9 @@ TEST(OpenTelemetry, EndSpanImplFail) {
spans,
ElementsAre(AllOf(
SpanWithStatus(opentelemetry::trace::StatusCode::kError, "not good"),
SpanHasAttributes(OTelAttribute<int>("gl-cpp.status_code", code)))));
SpanHasAttributes(OTelAttribute<int>("gl-cpp.status_code", code),
OTelAttribute<std::string>("gl-cpp.error.message",
"not good")))));
}

TEST(OpenTelemetry, EndSpanImplErrorInfo) {
Expand All @@ -201,6 +203,7 @@ TEST(OpenTelemetry, EndSpanImplErrorInfo) {
SpanWithStatus(opentelemetry::trace::StatusCode::kError, "not good"),
SpanHasAttributes(
OTelAttribute<int>("gl-cpp.status_code", code),
OTelAttribute<std::string>("gl-cpp.error.message", "not good"),
OTelAttribute<std::string>("gcloud.error.reason", "reason")))));

span = MakeSpan("domain");
Expand All @@ -213,6 +216,7 @@ TEST(OpenTelemetry, EndSpanImplErrorInfo) {
SpanWithStatus(opentelemetry::trace::StatusCode::kError, "not good"),
SpanHasAttributes(
OTelAttribute<int>("gl-cpp.status_code", code),
OTelAttribute<std::string>("gl-cpp.error.message", "not good"),
OTelAttribute<std::string>("gcloud.error.domain", "domain")))));

span = MakeSpan("metadata");
Expand All @@ -225,6 +229,7 @@ TEST(OpenTelemetry, EndSpanImplErrorInfo) {
SpanWithStatus(opentelemetry::trace::StatusCode::kError, "not good"),
SpanHasAttributes(
OTelAttribute<int>("gl-cpp.status_code", code),
OTelAttribute<std::string>("gl-cpp.error.message", "not good"),
OTelAttribute<std::string>("gcloud.error.metadata.k1", "v1"),
OTelAttribute<std::string>("gcloud.error.metadata.k2", "v2")))));
}
Expand Down

0 comments on commit 36f6bd4

Please sign in to comment.