-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathattributes.go
45 lines (38 loc) · 1.18 KB
/
attributes.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
package http
import (
"net/http"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/semconv/v1.21.0"
)
// TraceRequestAttrs returns a list of attributes to be set
// for a given http.Request (only useful for traces, as
// it reports the url string with any variable parameter
// that it might contain).
func TraceRequestAttrs(r *http.Request) []attribute.KeyValue {
attrs := make([]attribute.KeyValue, 0, 5)
attrs = append(attrs,
semconv.URLFull(r.URL.String()),
semconv.ServerAddress(r.Host),
semconv.ClientAddress(r.RemoteAddr),
semconv.HTTPRequestMethodKey.String(r.Method),
)
if r.ContentLength >= 0 {
attrs = append(attrs, semconv.HTTPRequestBodySize(int(r.ContentLength)))
}
userAgent := r.UserAgent()
if userAgent != "" {
attrs = append(attrs, semconv.UserAgentOriginal(userAgent))
}
return attrs
}
// TraceResponseAttrs returns a list of attributes to be set
// for a given http.Response.
func TraceResponseAttrs(resp *http.Response) []attribute.KeyValue {
if resp == nil {
return []attribute.KeyValue{}
}
return []attribute.KeyValue{
semconv.HTTPResponseStatusCode(int(resp.StatusCode)),
semconv.HTTPResponseBodySize(int(resp.ContentLength)),
}
}