| 1 |
// Code generated by gotmpl. DO NOT MODIFY. |
| 2 |
// source: internal/shared/semconv/server_test.go.tmpl |
| 3 |
|
| 4 |
// Copyright The OpenTelemetry Authors |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
package semconv |
| 8 |
|
| 9 |
import ( |
| 10 |
"net/http" |
| 11 |
"net/http/httptest" |
| 12 |
"testing" |
| 13 |
|
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
"github.com/stretchr/testify/require" |
| 16 |
|
| 17 |
"go.opentelemetry.io/otel/attribute" |
| 18 |
) |
| 19 |
|
| 20 |
func TestHTTPServer_MetricAttributes(t *testing.T) { |
| 21 |
defaultRequest := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "http://example.com/path?query=test", http.NoBody) |
| 22 |
reqWithPattern := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/path/abc123", http.NoBody) |
| 23 |
reqWithPattern.Pattern = "/path/abc123" |
| 24 |
|
| 25 |
tests := []struct { |
| 26 |
name string |
| 27 |
server string |
| 28 |
req *http.Request |
| 29 |
statusCode int |
| 30 |
route string |
| 31 |
additionalAttributes []attribute.KeyValue |
| 32 |
wantFunc func(t *testing.T, attrs []attribute.KeyValue) |
| 33 |
}{ |
| 34 |
{ |
| 35 |
name: "routine testing", |
| 36 |
server: "", |
| 37 |
req: defaultRequest, |
| 38 |
statusCode: 200, |
| 39 |
route: "", |
| 40 |
additionalAttributes: []attribute.KeyValue{attribute.String("test", "test")}, |
| 41 |
wantFunc: func(t *testing.T, attrs []attribute.KeyValue) { |
| 42 |
require.Len(t, attrs, 7) |
| 43 |
assert.ElementsMatch(t, []attribute.KeyValue{ |
| 44 |
attribute.String("http.request.method", "GET"), |
| 45 |
attribute.String("url.scheme", "http"), |
| 46 |
attribute.String("server.address", "example.com"), |
| 47 |
attribute.String("network.protocol.name", "http"), |
| 48 |
attribute.String("network.protocol.version", "1.1"), |
| 49 |
attribute.Int64("http.response.status_code", 200), |
| 50 |
attribute.String("test", "test"), |
| 51 |
}, attrs) |
| 52 |
}, |
| 53 |
}, |
| 54 |
{ |
| 55 |
name: "use server address", |
| 56 |
server: "example.com:9999", |
| 57 |
req: defaultRequest, |
| 58 |
statusCode: 200, |
| 59 |
route: "/path/${id}", |
| 60 |
additionalAttributes: nil, |
| 61 |
wantFunc: func(t *testing.T, attrs []attribute.KeyValue) { |
| 62 |
require.Len(t, attrs, 8) |
| 63 |
assert.ElementsMatch(t, []attribute.KeyValue{ |
| 64 |
attribute.String("http.request.method", "GET"), |
| 65 |
attribute.String("url.scheme", "http"), |
| 66 |
attribute.String("server.address", "example.com"), |
| 67 |
attribute.Int("server.port", 9999), |
| 68 |
attribute.String("network.protocol.name", "http"), |
| 69 |
attribute.String("network.protocol.version", "1.1"), |
| 70 |
attribute.Int64("http.response.status_code", 200), |
| 71 |
attribute.String("http.route", "/path/${id}"), |
| 72 |
}, attrs) |
| 73 |
}, |
| 74 |
}, |
| 75 |
{ |
| 76 |
name: "use route from request pattern", |
| 77 |
req: reqWithPattern, |
| 78 |
wantFunc: func(t *testing.T, attrs []attribute.KeyValue) { |
| 79 |
require.Len(t, attrs, 6) |
| 80 |
assert.ElementsMatch(t, []attribute.KeyValue{ |
| 81 |
attribute.String("http.request.method", "GET"), |
| 82 |
attribute.String("url.scheme", "http"), |
| 83 |
attribute.String("server.address", "example.com"), |
| 84 |
attribute.String("network.protocol.name", "http"), |
| 85 |
attribute.String("network.protocol.version", "1.1"), |
| 86 |
attribute.String("http.route", "/path/abc123"), |
| 87 |
}, attrs) |
| 88 |
}, |
| 89 |
}, |
| 90 |
} |
| 91 |
|
| 92 |
for _, tt := range tests { |
| 93 |
t.Run(tt.name, func(t *testing.T) { |
| 94 |
got := HTTPServer{}.MetricAttributes(tt.server, tt.req, tt.statusCode, tt.route, tt.additionalAttributes) |
| 95 |
tt.wantFunc(t, got) |
| 96 |
}) |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
func TestNewMethod(t *testing.T) { |
| 101 |
testCases := []struct { |
| 102 |
method string |
| 103 |
n int |
| 104 |
want attribute.KeyValue |
| 105 |
wantOrig attribute.KeyValue |
| 106 |
}{ |
| 107 |
{ |
| 108 |
method: http.MethodPost, |
| 109 |
n: 1, |
| 110 |
want: attribute.String("http.request.method", "POST"), |
| 111 |
}, |
| 112 |
{ |
| 113 |
method: "Put", |
| 114 |
n: 2, |
| 115 |
want: attribute.String("http.request.method", "PUT"), |
| 116 |
wantOrig: attribute.String("http.request.method_original", "Put"), |
| 117 |
}, |
| 118 |
{ |
| 119 |
method: "Unknown", |
| 120 |
n: 2, |
| 121 |
want: attribute.String("http.request.method", "GET"), |
| 122 |
wantOrig: attribute.String("http.request.method_original", "Unknown"), |
| 123 |
}, |
| 124 |
} |
| 125 |
|
| 126 |
for _, tt := range testCases { |
| 127 |
t.Run(tt.method, func(t *testing.T) { |
| 128 |
got, gotOrig := HTTPServer{}.method(tt.method) |
| 129 |
assert.Equal(t, tt.want, got) |
| 130 |
assert.Equal(t, tt.wantOrig, gotOrig) |
| 131 |
}) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func TestRequestTraceAttrs_HTTPRoute(t *testing.T) { |
| 136 |
tests := []struct { |
| 137 |
name string |
| 138 |
pattern string |
| 139 |
wantRoute string |
| 140 |
}{ |
| 141 |
{ |
| 142 |
name: "only path", |
| 143 |
pattern: "/path/{id}", |
| 144 |
wantRoute: "/path/{id}", |
| 145 |
}, |
| 146 |
{ |
| 147 |
name: "with method", |
| 148 |
pattern: "GET /path/{id}", |
| 149 |
wantRoute: "/path/{id}", |
| 150 |
}, |
| 151 |
{ |
| 152 |
name: "with domain", |
| 153 |
pattern: "example.com/path/{id}", |
| 154 |
wantRoute: "/path/{id}", |
| 155 |
}, |
| 156 |
} |
| 157 |
|
| 158 |
for _, tt := range tests { |
| 159 |
t.Run(tt.name, func(t *testing.T) { |
| 160 |
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/path/abc123", http.NoBody) |
| 161 |
req.Pattern = tt.pattern |
| 162 |
|
| 163 |
attrs := (HTTPServer{}).RequestTraceAttrs("", req, RequestTraceAttrsOpts{}) |
| 164 |
|
| 165 |
var gotRoute string |
| 166 |
for _, attr := range attrs { |
| 167 |
if attr.Key == "http.route" { |
| 168 |
gotRoute = attr.Value.AsString() |
| 169 |
break |
| 170 |
} |
| 171 |
} |
| 172 |
require.Equal(t, tt.wantRoute, gotRoute) |
| 173 |
}) |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
func TestRequestTraceAttrs_ClientIP(t *testing.T) { |
| 178 |
for _, tt := range []struct { |
| 179 |
name string |
| 180 |
requestModifierFn func(r *http.Request) |
| 181 |
requestTraceOpts RequestTraceAttrsOpts |
| 182 |
|
| 183 |
wantClientIP string |
| 184 |
}{ |
| 185 |
{ |
| 186 |
name: "with a client IP from the network", |
| 187 |
wantClientIP: "1.2.3.4", |
| 188 |
}, |
| 189 |
{ |
| 190 |
name: "with a client IP from x-forwarded-for header", |
| 191 |
requestModifierFn: func(r *http.Request) { |
| 192 |
r.Header.Add("X-Forwarded-For", "5.6.7.8") |
| 193 |
}, |
| 194 |
wantClientIP: "5.6.7.8", |
| 195 |
}, |
| 196 |
{ |
| 197 |
name: "with a client IP in options", |
| 198 |
requestModifierFn: func(r *http.Request) { |
| 199 |
r.Header.Add("X-Forwarded-For", "5.6.7.8") |
| 200 |
}, |
| 201 |
requestTraceOpts: RequestTraceAttrsOpts{ |
| 202 |
HTTPClientIP: "9.8.7.6", |
| 203 |
}, |
| 204 |
wantClientIP: "9.8.7.6", |
| 205 |
}, |
| 206 |
} { |
| 207 |
t.Run(tt.name, func(t *testing.T) { |
| 208 |
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/example", http.NoBody) |
| 209 |
req.RemoteAddr = "1.2.3.4:5678" |
| 210 |
|
| 211 |
if tt.requestModifierFn != nil { |
| 212 |
tt.requestModifierFn(req) |
| 213 |
} |
| 214 |
|
| 215 |
var found bool |
| 216 |
for _, attr := range (HTTPServer{}).RequestTraceAttrs("", req, tt.requestTraceOpts) { |
| 217 |
if attr.Key != "client.address" { |
| 218 |
continue |
| 219 |
} |
| 220 |
found = true |
| 221 |
assert.Equal(t, tt.wantClientIP, attr.Value.AsString()) |
| 222 |
} |
| 223 |
require.True(t, found) |
| 224 |
}) |
| 225 |
} |
| 226 |
} |