-
Notifications
You must be signed in to change notification settings - Fork 22
/
tracer_server.go
144 lines (118 loc) · 4.33 KB
/
tracer_server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2022 CloudWeGo Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package tracing
import (
"context"
"time"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/adaptor"
serverconfig "github.com/cloudwego/hertz/pkg/common/config"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/common/tracer"
"github.com/cloudwego/hertz/pkg/common/tracer/stats"
"github.com/hertz-contrib/obs-opentelemetry/tracing/internal"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
oteltrace "go.opentelemetry.io/otel/trace"
)
var _ tracer.Tracer = (*serverTracer)(nil)
type serverTracer struct {
config *Config
counters map[string]metric.Int64Counter
histogramRecorder map[string]metric.Float64Histogram
}
func NewServerTracer(opts ...Option) (serverconfig.Option, *Config) {
cfg := newConfig(opts)
st := &serverTracer{
config: cfg,
counters: make(map[string]metric.Int64Counter),
histogramRecorder: make(map[string]metric.Float64Histogram),
}
st.createMeasures()
return server.WithTracer(st), cfg
}
func (s *serverTracer) createMeasures() {
serverRequestCountMeasure, err := s.config.meter.Int64Counter(
ServerRequestCount,
metric.WithUnit("count"),
metric.WithDescription("measures Incoming request count total"),
)
handleErr(err)
serverLatencyMeasure, err := s.config.meter.Float64Histogram(
ServerLatency,
metric.WithUnit("ms"),
metric.WithDescription("measures th incoming end to end duration"),
)
handleErr(err)
s.counters[ServerRequestCount] = serverRequestCountMeasure
s.histogramRecorder[ServerLatency] = serverLatencyMeasure
}
func (s *serverTracer) Start(ctx context.Context, c *app.RequestContext) context.Context {
if s.config.shouldIgnore(ctx, c) {
return ctx
}
tc := &internal.TraceCarrier{}
tc.SetTracer(s.config.tracer)
return internal.WithTraceCarrier(ctx, tc)
}
func (s *serverTracer) Finish(ctx context.Context, c *app.RequestContext) {
if s.config.shouldIgnore(ctx, c) {
return
}
// trace carrier from context
tc := internal.TraceCarrierFromContext(ctx)
if tc == nil {
hlog.Debugf("get tracer container failed")
return
}
ti := c.GetTraceInfo()
st := ti.Stats()
if st.Level() == stats.LevelDisabled {
return
}
httpStart := st.GetEvent(stats.HTTPStart)
if httpStart == nil {
return
}
elapsedTime := float64(st.GetEvent(stats.HTTPFinish).Time().Sub(httpStart.Time())) / float64(time.Millisecond)
// span
span := tc.Span()
if span == nil || !span.IsRecording() {
return
}
// span attributes from original http request
if httpReq, err := adaptor.GetCompatRequest(c.GetRequest()); err == nil {
span.SetAttributes(semconv.NetAttributesFromHTTPRequest("tcp", httpReq)...)
span.SetAttributes(semconv.EndUserAttributesFromHTTPRequest(httpReq)...)
span.SetAttributes(semconv.HTTPServerAttributesFromHTTPRequest("", s.config.serverHttpRouteFormatter(c), httpReq)...)
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 {
recordErrorSpanWithStack(span, httpErr, panicMsg, panicStack)
}
span.End(oteltrace.WithTimestamp(getEndTimeOrNow(ti)))
metricsAttributes := extractMetricsAttributesFromSpan(span)
s.counters[ServerRequestCount].Add(ctx, 1, metric.WithAttributes(metricsAttributes...))
s.histogramRecorder[ServerLatency].Record(ctx, elapsedTime, metric.WithAttributes(metricsAttributes...))
}