diff --git a/tracing/example_test.go b/tracing/example_test.go index 62ed8b4..24e3338 100644 --- a/tracing/example_test.go +++ b/tracing/example_test.go @@ -59,6 +59,10 @@ func TestMetricsExample(t *testing.T) { require.NoError(t, err) assert.NotNil(t, body) + // test client returns error + _, _, err = c.Get(context.Background(), nil, "http://localhost:39887/ping?foo=bar") + assert.NotNil(t, err) + // diff metrics assert.NoError(t, testutil.GatherAndCompare( registry, "testdata/hertz_request_metrics.txt", diff --git a/tracing/middleware.go b/tracing/middleware.go index 6206c15..bfb78ca 100644 --- a/tracing/middleware.go +++ b/tracing/middleware.go @@ -18,16 +18,16 @@ import ( "context" "time" - "github.com/cloudwego/hertz/pkg/common/tracer/stats" - "go.opentelemetry.io/otel/attribute" - "github.com/cloudwego/hertz/pkg/app" "github.com/cloudwego/hertz/pkg/app/client" "github.com/cloudwego/hertz/pkg/common/adaptor" "github.com/cloudwego/hertz/pkg/common/hlog" + "github.com/cloudwego/hertz/pkg/common/tracer/stats" "github.com/cloudwego/hertz/pkg/protocol" "github.com/hertz-contrib/obs-opentelemetry/tracing/internal" + "go.opentelemetry.io/otel/attribute" "go.opentelemetry.io/otel/baggage" + "go.opentelemetry.io/otel/codes" "go.opentelemetry.io/otel/metric" "go.opentelemetry.io/otel/sdk/trace" semconv "go.opentelemetry.io/otel/semconv/v1.10.0" @@ -104,12 +104,16 @@ func ClientMiddleware(opts ...Option) client.Middleware { // span attributes attrs := []attribute.KeyValue{ semconv.HTTPURLKey.String(req.URI().String()), - semconv.HTTPStatusCodeKey.Int(resp.StatusCode()), } - span.SetAttributes(attrs...) - // set span status with resp status code - span.SetStatus(semconv.SpanStatusFromHTTPStatusCode(resp.StatusCode())) + if err == nil { + // set span status with resp status code + span.SetStatus(semconv.SpanStatusFromHTTPStatusCode(resp.StatusCode())) + attrs = append(attrs, semconv.HTTPStatusCodeKey.Int(resp.StatusCode())) + } else { // resp.StatusCode() is not valid when client returns error + span.SetStatus(codes.Error, err.Error()) + } + span.SetAttributes(attrs...) // extract metrics attr metricsAttributes := extractMetricsAttributesFromSpan(span) diff --git a/tracing/testdata/hertz_request_metrics.txt b/tracing/testdata/hertz_request_metrics.txt index de1ea65..5a6d9a9 100755 --- a/tracing/testdata/hertz_request_metrics.txt +++ b/tracing/testdata/hertz_request_metrics.txt @@ -1,5 +1,6 @@ # HELP http_client_request_count_total measures the client request count total # TYPE http_client_request_count_total counter +http_client_request_count_total{deployment_environment="test-env",http_host="localhost:39887",http_method="GET",http_route="/ping",otel_scope_name="github.com/hertz-contrib/obs-opentelemetry",otel_scope_version="semver:0.39.0",service_name="test-server",service_namespace="test-ns",status_code="Error"} 1 http_client_request_count_total{deployment_environment="test-env",http_host="localhost:39888",http_method="GET",http_route="/ping",http_status_code="200",otel_scope_name="github.com/hertz-contrib/obs-opentelemetry",otel_scope_version="semver:0.39.0",service_name="test-server",service_namespace="test-ns",status_code="Unset"} 1 # HELP http_server_request_count_total measures Incoming request count total # TYPE http_server_request_count_total counter