Skip to content

Commit

Permalink
feat(attributes): add attributes for span and metrics (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
CoderPoet authored Dec 1, 2023
1 parent 1cee951 commit f459e1d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
8 changes: 8 additions & 0 deletions tracing/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"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"
Expand Down Expand Up @@ -100,6 +101,13 @@ func ClientMiddleware(opts ...Option) client.Middleware {
span.SetAttributes(semconv.HTTPServerAttributesFromHTTPRequest("", cfg.clientHttpRouteFormatter(req), httpReq)...)
}

// 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()))

Expand Down
4 changes: 2 additions & 2 deletions tracing/testdata/hertz_request_metrics.txt
Original file line number Diff line number Diff line change
@@ -1,6 +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:39888",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="Unset"} 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
http_server_request_count_total{deployment_environment="test-env",http_host="localhost:39888",http_method="GET",http_route="/ping",otel_scope_name="github.com/hertz-contrib/obs-opentelemetry",otel_scope_version="semver:0.39.0",peer_deployment_environment="test-env",peer_service="test-server",peer_service_namespace="test-ns",service_name="test-server",service_namespace="test-ns",status_code="Unset"} 1
http_server_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",peer_deployment_environment="test-env",peer_service="test-server",peer_service_namespace="test-ns",service_name="test-server",service_namespace="test-ns",status_code="Unset"} 1
9 changes: 9 additions & 0 deletions tracing/tracer_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"time"

"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"

"github.com/cloudwego/hertz/pkg/app"
Expand Down Expand Up @@ -115,6 +116,14 @@ func (s *serverTracer) Finish(ctx context.Context, c *app.RequestContext) {
span.SetStatus(semconv.SpanStatusFromHTTPStatusCode(c.Response.StatusCode()))
}

// span attributes
attrs := []attribute.KeyValue{
semconv.HTTPURLKey.String(c.URI().String()),
semconv.NetPeerIPKey.String(c.ClientIP()),
semconv.HTTPStatusCodeKey.Int(c.Response.StatusCode()),
}
span.SetAttributes(attrs...)

injectStatsEventsToSpan(span, st)

if panicMsg, panicStack, httpErr := parseHTTPError(ti); httpErr != nil || len(panicMsg) > 0 {
Expand Down

0 comments on commit f459e1d

Please sign in to comment.