diff --git a/google/cloud/internal/opentelemetry.cc b/google/cloud/internal/opentelemetry.cc index 8505e8f01b99d..65cc2abb1cf44 100644 --- a/google/cloud/internal/opentelemetry.cc +++ b/google/cloud/internal/opentelemetry.cc @@ -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(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()); diff --git a/google/cloud/internal/opentelemetry_test.cc b/google/cloud/internal/opentelemetry_test.cc index 7940455cfa095..784f8b44a6491 100644 --- a/google/cloud/internal/opentelemetry_test.cc +++ b/google/cloud/internal/opentelemetry_test.cc @@ -184,7 +184,9 @@ TEST(OpenTelemetry, EndSpanImplFail) { spans, ElementsAre(AllOf( SpanWithStatus(opentelemetry::trace::StatusCode::kError, "not good"), - SpanHasAttributes(OTelAttribute("gl-cpp.status_code", code))))); + SpanHasAttributes(OTelAttribute("gl-cpp.status_code", code), + OTelAttribute("gl-cpp.error.message", + "not good"))))); } TEST(OpenTelemetry, EndSpanImplErrorInfo) { @@ -201,6 +203,7 @@ TEST(OpenTelemetry, EndSpanImplErrorInfo) { SpanWithStatus(opentelemetry::trace::StatusCode::kError, "not good"), SpanHasAttributes( OTelAttribute("gl-cpp.status_code", code), + OTelAttribute("gl-cpp.error.message", "not good"), OTelAttribute("gcloud.error.reason", "reason"))))); span = MakeSpan("domain"); @@ -213,6 +216,7 @@ TEST(OpenTelemetry, EndSpanImplErrorInfo) { SpanWithStatus(opentelemetry::trace::StatusCode::kError, "not good"), SpanHasAttributes( OTelAttribute("gl-cpp.status_code", code), + OTelAttribute("gl-cpp.error.message", "not good"), OTelAttribute("gcloud.error.domain", "domain"))))); span = MakeSpan("metadata"); @@ -225,6 +229,7 @@ TEST(OpenTelemetry, EndSpanImplErrorInfo) { SpanWithStatus(opentelemetry::trace::StatusCode::kError, "not good"), SpanHasAttributes( OTelAttribute("gl-cpp.status_code", code), + OTelAttribute("gl-cpp.error.message", "not good"), OTelAttribute("gcloud.error.metadata.k1", "v1"), OTelAttribute("gcloud.error.metadata.k2", "v2"))))); }