| 1 |
// Code generated by gotmpl. DO NOT MODIFY. |
| 2 |
// source: internal/shared/semconv/httpconvtest_test.go.tmpl |
| 3 |
|
| 4 |
// Copyright The OpenTelemetry Authors |
| 5 |
// SPDX-License-Identifier: Apache-2.0 |
| 6 |
|
| 7 |
package semconv_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"net/http" |
| 12 |
"net/http/httptest" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
"time" |
| 16 |
|
| 17 |
"github.com/stretchr/testify/assert" |
| 18 |
"github.com/stretchr/testify/require" |
| 19 |
|
| 20 |
"go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux/internal/semconv" |
| 21 |
"go.opentelemetry.io/otel/attribute" |
| 22 |
"go.opentelemetry.io/otel/metric" |
| 23 |
"go.opentelemetry.io/otel/sdk/instrumentation" |
| 24 |
sdkmetric "go.opentelemetry.io/otel/sdk/metric" |
| 25 |
"go.opentelemetry.io/otel/sdk/metric/metricdata" |
| 26 |
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" |
| 27 |
) |
| 28 |
|
| 29 |
func TestNewTraceRequest(t *testing.T) { |
| 30 |
serv := semconv.NewHTTPServer(nil) |
| 31 |
want := func(req testServerReq) []attribute.KeyValue { |
| 32 |
return []attribute.KeyValue{ |
| 33 |
attribute.String("http.request.method", "GET"), |
| 34 |
attribute.String("url.scheme", "http"), |
| 35 |
attribute.String("server.address", req.hostname), |
| 36 |
attribute.Int("server.port", req.serverPort), |
| 37 |
attribute.String("network.peer.address", req.peerAddr), |
| 38 |
attribute.Int("network.peer.port", req.peerPort), |
| 39 |
attribute.String("user_agent.original", "Go-http-client/1.1"), |
| 40 |
attribute.String("client.address", req.clientIP), |
| 41 |
attribute.String("network.protocol.version", "1.1"), |
| 42 |
attribute.String("url.path", "/"), |
| 43 |
} |
| 44 |
} |
| 45 |
testTraceRequest(t, serv, want) |
| 46 |
} |
| 47 |
|
| 48 |
func TestNewServerRecordMetrics(t *testing.T) { |
| 49 |
oldAttrs := attribute.NewSet( |
| 50 |
attribute.String("http.scheme", "http"), |
| 51 |
attribute.String("http.method", "POST"), |
| 52 |
attribute.Int64("http.status_code", 301), |
| 53 |
attribute.String("key", "value"), |
| 54 |
attribute.String("net.host.name", "stuff"), |
| 55 |
attribute.String("net.protocol.name", "http"), |
| 56 |
attribute.String("net.protocol.version", "1.1"), |
| 57 |
) |
| 58 |
|
| 59 |
currAttrs := attribute.NewSet( |
| 60 |
attribute.String("http.request.method", "POST"), |
| 61 |
attribute.Int64("http.response.status_code", 301), |
| 62 |
attribute.String("key", "value"), |
| 63 |
attribute.String("network.protocol.name", "http"), |
| 64 |
attribute.String("network.protocol.version", "1.1"), |
| 65 |
attribute.String("server.address", "stuff"), |
| 66 |
attribute.String("url.scheme", "http"), |
| 67 |
) |
| 68 |
|
| 69 |
// the HTTPServer version |
| 70 |
expectedCurrentScopeMetric := metricdata.ScopeMetrics{ |
| 71 |
Scope: instrumentation.Scope{ |
| 72 |
Name: "test", |
| 73 |
}, |
| 74 |
Metrics: []metricdata.Metrics{ |
| 75 |
{ |
| 76 |
Name: "http.server.request.body.size", |
| 77 |
Description: "Size of HTTP server request bodies.", |
| 78 |
Unit: "By", |
| 79 |
Data: metricdata.Histogram[int64]{ |
| 80 |
Temporality: metricdata.CumulativeTemporality, |
| 81 |
DataPoints: []metricdata.HistogramDataPoint[int64]{ |
| 82 |
{ |
| 83 |
Attributes: currAttrs, |
| 84 |
}, |
| 85 |
}, |
| 86 |
}, |
| 87 |
}, |
| 88 |
{ |
| 89 |
Name: "http.server.response.body.size", |
| 90 |
Description: "Size of HTTP server response bodies.", |
| 91 |
Unit: "By", |
| 92 |
Data: metricdata.Histogram[int64]{ |
| 93 |
Temporality: metricdata.CumulativeTemporality, |
| 94 |
DataPoints: []metricdata.HistogramDataPoint[int64]{ |
| 95 |
{ |
| 96 |
Attributes: currAttrs, |
| 97 |
}, |
| 98 |
}, |
| 99 |
}, |
| 100 |
}, |
| 101 |
{ |
| 102 |
Name: "http.server.request.duration", |
| 103 |
Description: "Duration of HTTP server requests.", |
| 104 |
Unit: "s", |
| 105 |
Data: metricdata.Histogram[float64]{ |
| 106 |
Temporality: metricdata.CumulativeTemporality, |
| 107 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 108 |
{ |
| 109 |
Attributes: currAttrs, |
| 110 |
}, |
| 111 |
}, |
| 112 |
}, |
| 113 |
}, |
| 114 |
}, |
| 115 |
} |
| 116 |
|
| 117 |
// The OldHTTPServer version |
| 118 |
expectedOldScopeMetric := expectedCurrentScopeMetric |
| 119 |
expectedOldScopeMetric.Metrics = append(expectedOldScopeMetric.Metrics, []metricdata.Metrics{ |
| 120 |
{ |
| 121 |
Name: "http.server.request.size", |
| 122 |
Description: "Measures the size of HTTP request messages.", |
| 123 |
Unit: "By", |
| 124 |
Data: metricdata.Sum[int64]{ |
| 125 |
Temporality: metricdata.CumulativeTemporality, |
| 126 |
IsMonotonic: true, |
| 127 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 128 |
{ |
| 129 |
Attributes: oldAttrs, |
| 130 |
}, |
| 131 |
}, |
| 132 |
}, |
| 133 |
}, |
| 134 |
{ |
| 135 |
Name: "http.server.response.size", |
| 136 |
Description: "Measures the size of HTTP response messages.", |
| 137 |
Unit: "By", |
| 138 |
Data: metricdata.Sum[int64]{ |
| 139 |
Temporality: metricdata.CumulativeTemporality, |
| 140 |
IsMonotonic: true, |
| 141 |
DataPoints: []metricdata.DataPoint[int64]{ |
| 142 |
{ |
| 143 |
Attributes: oldAttrs, |
| 144 |
}, |
| 145 |
}, |
| 146 |
}, |
| 147 |
}, |
| 148 |
{ |
| 149 |
Name: "http.server.duration", |
| 150 |
Description: "Measures the duration of inbound HTTP requests.", |
| 151 |
Unit: "ms", |
| 152 |
Data: metricdata.Histogram[float64]{ |
| 153 |
Temporality: metricdata.CumulativeTemporality, |
| 154 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 155 |
{ |
| 156 |
Attributes: oldAttrs, |
| 157 |
}, |
| 158 |
}, |
| 159 |
}, |
| 160 |
}, |
| 161 |
}...) |
| 162 |
|
| 163 |
tests := []struct { |
| 164 |
name string |
| 165 |
serverFunc func(metric.MeterProvider) semconv.HTTPServer |
| 166 |
wantFunc func(t *testing.T, rm metricdata.ResourceMetrics) |
| 167 |
}{ |
| 168 |
{ |
| 169 |
name: "No Meter", |
| 170 |
serverFunc: func(metric.MeterProvider) semconv.HTTPServer { |
| 171 |
return semconv.NewHTTPServer(nil) |
| 172 |
}, |
| 173 |
wantFunc: func(t *testing.T, rm metricdata.ResourceMetrics) { |
| 174 |
assert.Empty(t, rm.ScopeMetrics) |
| 175 |
}, |
| 176 |
}, |
| 177 |
{ |
| 178 |
name: "With Meter", |
| 179 |
serverFunc: func(mp metric.MeterProvider) semconv.HTTPServer { |
| 180 |
return semconv.NewHTTPServer(mp.Meter("test")) |
| 181 |
}, |
| 182 |
wantFunc: func(t *testing.T, rm metricdata.ResourceMetrics) { |
| 183 |
require.Len(t, rm.ScopeMetrics, 1) |
| 184 |
|
| 185 |
// because of OldHTTPServer |
| 186 |
require.Len(t, rm.ScopeMetrics[0].Metrics, 3) |
| 187 |
metricdatatest.AssertEqual(t, expectedCurrentScopeMetric, rm.ScopeMetrics[0], metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue()) |
| 188 |
}, |
| 189 |
}, |
| 190 |
} |
| 191 |
|
| 192 |
for _, tt := range tests { |
| 193 |
t.Run(tt.name, func(t *testing.T) { |
| 194 |
reader := sdkmetric.NewManualReader() |
| 195 |
mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) |
| 196 |
|
| 197 |
server := tt.serverFunc(mp) |
| 198 |
req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "http://example.com", http.NoBody) |
| 199 |
assert.NoError(t, err) |
| 200 |
|
| 201 |
server.RecordMetrics(t.Context(), semconv.ServerMetricData{ |
| 202 |
ServerName: "stuff", |
| 203 |
ResponseSize: 200, |
| 204 |
MetricAttributes: semconv.MetricAttributes{ |
| 205 |
Req: req, |
| 206 |
StatusCode: 301, |
| 207 |
AdditionalAttributes: []attribute.KeyValue{ |
| 208 |
attribute.String("key", "value"), |
| 209 |
}, |
| 210 |
}, |
| 211 |
MetricData: semconv.MetricData{ |
| 212 |
RequestSize: 100, |
| 213 |
RequestDuration: 300 * time.Millisecond, |
| 214 |
}, |
| 215 |
}) |
| 216 |
|
| 217 |
rm := metricdata.ResourceMetrics{} |
| 218 |
require.NoError(t, reader.Collect(t.Context(), &rm)) |
| 219 |
tt.wantFunc(t, rm) |
| 220 |
}) |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
func TestNewTraceResponse(t *testing.T) { |
| 225 |
testCases := []struct { |
| 226 |
name string |
| 227 |
resp semconv.ResponseTelemetry |
| 228 |
want []attribute.KeyValue |
| 229 |
}{ |
| 230 |
{ |
| 231 |
name: "empty", |
| 232 |
resp: semconv.ResponseTelemetry{}, |
| 233 |
want: nil, |
| 234 |
}, |
| 235 |
{ |
| 236 |
name: "no errors", |
| 237 |
resp: semconv.ResponseTelemetry{ |
| 238 |
StatusCode: 200, |
| 239 |
ReadBytes: 701, |
| 240 |
WriteBytes: 802, |
| 241 |
}, |
| 242 |
want: []attribute.KeyValue{ |
| 243 |
attribute.Int("http.request.body.size", 701), |
| 244 |
attribute.Int("http.response.body.size", 802), |
| 245 |
attribute.Int("http.response.status_code", 200), |
| 246 |
}, |
| 247 |
}, |
| 248 |
{ |
| 249 |
name: "with errors", |
| 250 |
resp: semconv.ResponseTelemetry{ |
| 251 |
StatusCode: 200, |
| 252 |
ReadBytes: 701, |
| 253 |
ReadError: fmt.Errorf("read error"), |
| 254 |
WriteBytes: 802, |
| 255 |
WriteError: fmt.Errorf("write error"), |
| 256 |
}, |
| 257 |
want: []attribute.KeyValue{ |
| 258 |
attribute.Int("http.request.body.size", 701), |
| 259 |
attribute.Int("http.response.body.size", 802), |
| 260 |
attribute.Int("http.response.status_code", 200), |
| 261 |
}, |
| 262 |
}, |
| 263 |
} |
| 264 |
|
| 265 |
for _, tt := range testCases { |
| 266 |
t.Run(tt.name, func(t *testing.T) { |
| 267 |
got := semconv.HTTPServer{}.ResponseTraceAttrs(tt.resp) |
| 268 |
assert.ElementsMatch(t, tt.want, got) |
| 269 |
}) |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
func TestNewTraceRequest_Client(t *testing.T) { |
| 274 |
body := strings.NewReader("Hello, world!") |
| 275 |
url := "https://example.com:8888/foo/bar?stuff=morestuff" |
| 276 |
req := httptest.NewRequestWithContext(t.Context(), "pOST", url, body) |
| 277 |
req.Header.Set("User-Agent", "go-test-agent") |
| 278 |
|
| 279 |
want := []attribute.KeyValue{ |
| 280 |
attribute.String("http.request.method", "POST"), |
| 281 |
attribute.String("http.request.method_original", "pOST"), |
| 282 |
attribute.String("url.full", url), |
| 283 |
attribute.String("server.address", "example.com"), |
| 284 |
attribute.Int("server.port", 8888), |
| 285 |
attribute.String("network.protocol.version", "1.1"), |
| 286 |
} |
| 287 |
client := semconv.NewHTTPClient(nil) |
| 288 |
assert.ElementsMatch(t, want, client.RequestTraceAttrs(req)) |
| 289 |
} |
| 290 |
|
| 291 |
func TestNewTraceResponse_Client(t *testing.T) { |
| 292 |
testcases := []struct { |
| 293 |
resp http.Response |
| 294 |
want []attribute.KeyValue |
| 295 |
}{ |
| 296 |
{resp: http.Response{StatusCode: 200, ContentLength: 123}, want: []attribute.KeyValue{attribute.Int("http.response.status_code", 200)}}, |
| 297 |
{resp: http.Response{StatusCode: 404, ContentLength: 0}, want: []attribute.KeyValue{attribute.Int("http.response.status_code", 404), attribute.String("error.type", "404")}}, |
| 298 |
} |
| 299 |
|
| 300 |
for _, tt := range testcases { |
| 301 |
client := semconv.NewHTTPClient(nil) |
| 302 |
assert.ElementsMatch(t, tt.want, client.ResponseTraceAttrs(&tt.resp)) |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
func TestClientRequest(t *testing.T) { |
| 307 |
body := strings.NewReader("Hello, world!") |
| 308 |
url := "https://example.com:8888/foo/bar?stuff=morestuff" |
| 309 |
req := httptest.NewRequestWithContext(t.Context(), "pOST", url, body) |
| 310 |
req.Header.Set("User-Agent", "go-test-agent") |
| 311 |
|
| 312 |
want := []attribute.KeyValue{ |
| 313 |
attribute.String("http.request.method", "POST"), |
| 314 |
attribute.String("http.request.method_original", "pOST"), |
| 315 |
attribute.String("url.full", url), |
| 316 |
attribute.String("server.address", "example.com"), |
| 317 |
attribute.Int("server.port", 8888), |
| 318 |
attribute.String("network.protocol.version", "1.1"), |
| 319 |
} |
| 320 |
got := semconv.HTTPClient{}.RequestTraceAttrs(req) |
| 321 |
assert.ElementsMatch(t, want, got) |
| 322 |
} |
| 323 |
|
| 324 |
func TestClientResponse(t *testing.T) { |
| 325 |
testcases := []struct { |
| 326 |
resp http.Response |
| 327 |
want []attribute.KeyValue |
| 328 |
}{ |
| 329 |
{resp: http.Response{StatusCode: 200, ContentLength: 123}, want: []attribute.KeyValue{attribute.Int("http.response.status_code", 200)}}, |
| 330 |
{resp: http.Response{StatusCode: 404, ContentLength: 0}, want: []attribute.KeyValue{attribute.Int("http.response.status_code", 404), attribute.String("error.type", "404")}}, |
| 331 |
} |
| 332 |
|
| 333 |
for _, tt := range testcases { |
| 334 |
got := semconv.HTTPClient{}.ResponseTraceAttrs(&tt.resp) |
| 335 |
assert.ElementsMatch(t, tt.want, got) |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
func TestNewClientRecordMetrics(t *testing.T) { |
| 340 |
currAttrs := attribute.NewSet( |
| 341 |
attribute.String("http.request.method", "POST"), |
| 342 |
attribute.Int64("http.response.status_code", 301), |
| 343 |
attribute.String("network.protocol.name", "http"), |
| 344 |
attribute.String("network.protocol.version", "1.1"), |
| 345 |
attribute.String("server.address", "example.com"), |
| 346 |
attribute.String("url.scheme", "http"), |
| 347 |
) |
| 348 |
|
| 349 |
// the HTTPClient version |
| 350 |
expectedCurrentScopeMetric := metricdata.ScopeMetrics{ |
| 351 |
Scope: instrumentation.Scope{ |
| 352 |
Name: "test", |
| 353 |
}, |
| 354 |
Metrics: []metricdata.Metrics{ |
| 355 |
{ |
| 356 |
Name: "http.client.request.body.size", |
| 357 |
Description: "Size of HTTP client request bodies.", |
| 358 |
Unit: "By", |
| 359 |
Data: metricdata.Histogram[int64]{ |
| 360 |
Temporality: metricdata.CumulativeTemporality, |
| 361 |
DataPoints: []metricdata.HistogramDataPoint[int64]{ |
| 362 |
{ |
| 363 |
Attributes: currAttrs, |
| 364 |
}, |
| 365 |
}, |
| 366 |
}, |
| 367 |
}, |
| 368 |
{ |
| 369 |
Name: "http.client.request.duration", |
| 370 |
Description: "Duration of HTTP client requests.", |
| 371 |
Unit: "s", |
| 372 |
Data: metricdata.Histogram[float64]{ |
| 373 |
Temporality: metricdata.CumulativeTemporality, |
| 374 |
DataPoints: []metricdata.HistogramDataPoint[float64]{ |
| 375 |
{ |
| 376 |
Attributes: currAttrs, |
| 377 |
}, |
| 378 |
}, |
| 379 |
}, |
| 380 |
}, |
| 381 |
}, |
| 382 |
} |
| 383 |
|
| 384 |
tests := []struct { |
| 385 |
name string |
| 386 |
clientFunc func(metric.MeterProvider) semconv.HTTPClient |
| 387 |
wantFunc func(t *testing.T, rm metricdata.ResourceMetrics) |
| 388 |
}{ |
| 389 |
{ |
| 390 |
name: "No environment variable set, and no Meter", |
| 391 |
clientFunc: func(metric.MeterProvider) semconv.HTTPClient { |
| 392 |
return semconv.NewHTTPClient(nil) |
| 393 |
}, |
| 394 |
wantFunc: func(t *testing.T, rm metricdata.ResourceMetrics) { |
| 395 |
assert.Empty(t, rm.ScopeMetrics) |
| 396 |
}, |
| 397 |
}, |
| 398 |
{ |
| 399 |
name: "With Meter", |
| 400 |
clientFunc: func(mp metric.MeterProvider) semconv.HTTPClient { |
| 401 |
return semconv.NewHTTPClient(mp.Meter("test")) |
| 402 |
}, |
| 403 |
wantFunc: func(t *testing.T, rm metricdata.ResourceMetrics) { |
| 404 |
require.Len(t, rm.ScopeMetrics, 1) |
| 405 |
|
| 406 |
require.Len(t, rm.ScopeMetrics[0].Metrics, 2) |
| 407 |
metricdatatest.AssertEqual(t, expectedCurrentScopeMetric, rm.ScopeMetrics[0], metricdatatest.IgnoreTimestamp(), metricdatatest.IgnoreValue()) |
| 408 |
}, |
| 409 |
}, |
| 410 |
} |
| 411 |
|
| 412 |
for _, tt := range tests { |
| 413 |
t.Run(tt.name, func(t *testing.T) { |
| 414 |
reader := sdkmetric.NewManualReader() |
| 415 |
mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) |
| 416 |
|
| 417 |
client := tt.clientFunc(mp) |
| 418 |
req, err := http.NewRequestWithContext(t.Context(), http.MethodPost, "http://example.com", http.NoBody) |
| 419 |
assert.NoError(t, err) |
| 420 |
|
| 421 |
client.RecordMetrics(t.Context(), semconv.MetricData{ |
| 422 |
RequestSize: 100, |
| 423 |
RequestDuration: 300 * time.Millisecond, |
| 424 |
}, client.MetricOptions(semconv.MetricAttributes{ |
| 425 |
Req: req, |
| 426 |
StatusCode: 301, |
| 427 |
})) |
| 428 |
|
| 429 |
rm := metricdata.ResourceMetrics{} |
| 430 |
require.NoError(t, reader.Collect(t.Context(), &rm)) |
| 431 |
tt.wantFunc(t, rm) |
| 432 |
}) |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
type customError struct{} |
| 437 |
|
| 438 |
func (customError) Error() string { |
| 439 |
return "custom error" |
| 440 |
} |