-
Notifications
You must be signed in to change notification settings - Fork 121
/
semconv.go
113 lines (92 loc) · 3.09 KB
/
semconv.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
package otelfiber
import (
"encoding/base64"
"strings"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/utils"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.12.0"
)
func httpServerMetricAttributesFromRequest(c *fiber.Ctx, cfg config) []attribute.KeyValue {
attrs := []attribute.KeyValue{
httpFlavorAttribute(c),
semconv.HTTPMethodKey.String(utils.CopyString(c.Method())),
semconv.HTTPSchemeKey.String(utils.CopyString(c.Protocol())),
semconv.NetHostNameKey.String(utils.CopyString(c.Hostname())),
}
if cfg.Port != nil {
attrs = append(attrs, semconv.NetHostPortKey.Int(*cfg.Port))
}
if cfg.ServerName != nil {
attrs = append(attrs, semconv.HTTPServerNameKey.String(*cfg.ServerName))
}
if cfg.CustomMetricAttributes != nil {
attrs = append(attrs, cfg.CustomMetricAttributes(c)...)
}
return attrs
}
func httpServerTraceAttributesFromRequest(c *fiber.Ctx, cfg config) []attribute.KeyValue {
attrs := []attribute.KeyValue{
httpFlavorAttribute(c),
// utils.CopyString: we need to copy the string as fasthttp strings are by default
// mutable so it will be unsafe to use in this middleware as it might be used after
// the handler returns.
semconv.HTTPMethodKey.String(utils.CopyString(c.Method())),
semconv.HTTPRequestContentLengthKey.Int(c.Request().Header.ContentLength()),
semconv.HTTPSchemeKey.String(utils.CopyString(c.Protocol())),
semconv.HTTPTargetKey.String(string(utils.CopyBytes(c.Request().RequestURI()))),
semconv.HTTPURLKey.String(utils.CopyString(c.OriginalURL())),
semconv.HTTPUserAgentKey.String(string(utils.CopyBytes(c.Request().Header.UserAgent()))),
semconv.NetHostNameKey.String(utils.CopyString(c.Hostname())),
semconv.NetTransportTCP,
}
if cfg.Port != nil {
attrs = append(attrs, semconv.NetHostPortKey.Int(*cfg.Port))
}
if cfg.ServerName != nil {
attrs = append(attrs, semconv.HTTPServerNameKey.String(*cfg.ServerName))
}
if username, ok := HasBasicAuth(c.Get(fiber.HeaderAuthorization)); ok {
attrs = append(attrs, semconv.EnduserIDKey.String(utils.CopyString(username)))
}
if cfg.collectClientIP {
clientIP := c.IP()
if len(clientIP) > 0 {
attrs = append(attrs, semconv.HTTPClientIPKey.String(utils.CopyString(clientIP)))
}
}
if cfg.CustomAttributes != nil {
attrs = append(attrs, cfg.CustomAttributes(c)...)
}
return attrs
}
func httpFlavorAttribute(c *fiber.Ctx) attribute.KeyValue {
if c.Request().Header.IsHTTP11() {
return semconv.HTTPFlavorHTTP11
}
return semconv.HTTPFlavorHTTP10
}
func HasBasicAuth(auth string) (string, bool) {
if auth == "" {
return "", false
}
// Check if the Authorization header is Basic
if !strings.HasPrefix(auth, "Basic ") {
return "", false
}
// Decode the header contents
raw, err := base64.StdEncoding.DecodeString(auth[6:])
if err != nil {
return "", false
}
// Get the credentials
creds := utils.UnsafeString(raw)
// Check if the credentials are in the correct form
// which is "username:password".
index := strings.Index(creds, ":")
if index == -1 {
return "", false
}
// Get the username
return creds[:index], true
}