Skip to content

Commit

Permalink
fix(metrics): not report status code 200 when http client returns err…
Browse files Browse the repository at this point in the history
…or (#50)

* remove status_code in metrics report when http client returns error and no response received

* add test case

* fix lint check
  • Loading branch information
2011aad authored Mar 15, 2024
1 parent a4af131 commit 34c24d2
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
4 changes: 4 additions & 0 deletions tracing/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
18 changes: 11 additions & 7 deletions tracing/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tracing/testdata/hertz_request_metrics.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 34c24d2

Please sign in to comment.