| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package internal |
| 5 |
|
| 6 |
import ( |
| 7 |
"crypto/tls" |
| 8 |
"net/http" |
| 9 |
"net/url" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/google/go-cmp/cmp" |
| 14 |
"github.com/stretchr/testify/assert" |
| 15 |
|
| 16 |
"go.opentelemetry.io/otel/attribute" |
| 17 |
"go.opentelemetry.io/otel/codes" |
| 18 |
"go.opentelemetry.io/otel/trace" |
| 19 |
) |
| 20 |
|
| 21 |
type tlsOption int |
| 22 |
|
| 23 |
const ( |
| 24 |
noTLS tlsOption = iota |
| 25 |
withTLS |
| 26 |
) |
| 27 |
|
| 28 |
var sc = &SemanticConventions{ |
| 29 |
EnduserIDKey: attribute.Key("enduser.id"), |
| 30 |
HTTPClientIPKey: attribute.Key("http.client_ip"), |
| 31 |
HTTPFlavorKey: attribute.Key("http.flavor"), |
| 32 |
HTTPHostKey: attribute.Key("http.host"), |
| 33 |
HTTPMethodKey: attribute.Key("http.method"), |
| 34 |
HTTPRequestContentLengthKey: attribute.Key("http.request_content_length"), |
| 35 |
HTTPRouteKey: attribute.Key("http.route"), |
| 36 |
HTTPSchemeHTTP: attribute.String("http.scheme", "http"), |
| 37 |
HTTPSchemeHTTPS: attribute.String("http.scheme", "https"), |
| 38 |
HTTPServerNameKey: attribute.Key("http.server_name"), |
| 39 |
HTTPStatusCodeKey: attribute.Key("http.status_code"), |
| 40 |
HTTPTargetKey: attribute.Key("http.target"), |
| 41 |
HTTPURLKey: attribute.Key("http.url"), |
| 42 |
HTTPUserAgentKey: attribute.Key("http.user_agent"), |
| 43 |
NetHostIPKey: attribute.Key("net.host.ip"), |
| 44 |
NetHostNameKey: attribute.Key("net.host.name"), |
| 45 |
NetHostPortKey: attribute.Key("net.host.port"), |
| 46 |
NetPeerIPKey: attribute.Key("net.peer.ip"), |
| 47 |
NetPeerNameKey: attribute.Key("net.peer.name"), |
| 48 |
NetPeerPortKey: attribute.Key("net.peer.port"), |
| 49 |
NetTransportIP: attribute.String("net.transport", "ip"), |
| 50 |
NetTransportOther: attribute.String("net.transport", "other"), |
| 51 |
NetTransportTCP: attribute.String("net.transport", "ip_tcp"), |
| 52 |
NetTransportUDP: attribute.String("net.transport", "ip_udp"), |
| 53 |
NetTransportUnix: attribute.String("net.transport", "unix"), |
| 54 |
} |
| 55 |
|
| 56 |
func TestNetAttributesFromHTTPRequest(t *testing.T) { |
| 57 |
type testcase struct { |
| 58 |
name string |
| 59 |
|
| 60 |
network string |
| 61 |
|
| 62 |
method string |
| 63 |
requestURI string |
| 64 |
proto string |
| 65 |
remoteAddr string |
| 66 |
host string |
| 67 |
url *url.URL |
| 68 |
header http.Header |
| 69 |
|
| 70 |
expected []attribute.KeyValue |
| 71 |
} |
| 72 |
testcases := []testcase{ |
| 73 |
{ |
| 74 |
name: "stripped, tcp", |
| 75 |
network: "tcp", |
| 76 |
method: http.MethodGet, |
| 77 |
requestURI: "/user/123", |
| 78 |
proto: "HTTP/1.0", |
| 79 |
remoteAddr: "", |
| 80 |
host: "", |
| 81 |
url: &url.URL{ |
| 82 |
Path: "/user/123", |
| 83 |
}, |
| 84 |
header: nil, |
| 85 |
expected: []attribute.KeyValue{ |
| 86 |
attribute.String("net.transport", "ip_tcp"), |
| 87 |
}, |
| 88 |
}, |
| 89 |
{ |
| 90 |
name: "stripped, udp", |
| 91 |
network: "udp", |
| 92 |
method: http.MethodGet, |
| 93 |
requestURI: "/user/123", |
| 94 |
proto: "HTTP/1.0", |
| 95 |
remoteAddr: "", |
| 96 |
host: "", |
| 97 |
url: &url.URL{ |
| 98 |
Path: "/user/123", |
| 99 |
}, |
| 100 |
header: nil, |
| 101 |
expected: []attribute.KeyValue{ |
| 102 |
attribute.String("net.transport", "ip_udp"), |
| 103 |
}, |
| 104 |
}, |
| 105 |
{ |
| 106 |
name: "stripped, ip", |
| 107 |
network: "ip", |
| 108 |
method: http.MethodGet, |
| 109 |
requestURI: "/user/123", |
| 110 |
proto: "HTTP/1.0", |
| 111 |
remoteAddr: "", |
| 112 |
host: "", |
| 113 |
url: &url.URL{ |
| 114 |
Path: "/user/123", |
| 115 |
}, |
| 116 |
header: nil, |
| 117 |
expected: []attribute.KeyValue{ |
| 118 |
attribute.String("net.transport", "ip"), |
| 119 |
}, |
| 120 |
}, |
| 121 |
{ |
| 122 |
name: "stripped, unix", |
| 123 |
network: "unix", |
| 124 |
method: http.MethodGet, |
| 125 |
requestURI: "/user/123", |
| 126 |
proto: "HTTP/1.0", |
| 127 |
remoteAddr: "", |
| 128 |
host: "", |
| 129 |
url: &url.URL{ |
| 130 |
Path: "/user/123", |
| 131 |
}, |
| 132 |
header: nil, |
| 133 |
expected: []attribute.KeyValue{ |
| 134 |
attribute.String("net.transport", "unix"), |
| 135 |
}, |
| 136 |
}, |
| 137 |
{ |
| 138 |
name: "stripped, other", |
| 139 |
network: "nih", |
| 140 |
method: http.MethodGet, |
| 141 |
requestURI: "/user/123", |
| 142 |
proto: "HTTP/1.0", |
| 143 |
remoteAddr: "", |
| 144 |
host: "", |
| 145 |
url: &url.URL{ |
| 146 |
Path: "/user/123", |
| 147 |
}, |
| 148 |
header: nil, |
| 149 |
expected: []attribute.KeyValue{ |
| 150 |
attribute.String("net.transport", "other"), |
| 151 |
}, |
| 152 |
}, |
| 153 |
{ |
| 154 |
name: "with remote ipv4 and port", |
| 155 |
network: "tcp", |
| 156 |
method: http.MethodGet, |
| 157 |
requestURI: "/user/123", |
| 158 |
proto: "HTTP/1.0", |
| 159 |
remoteAddr: "1.2.3.4:56", |
| 160 |
host: "", |
| 161 |
url: &url.URL{ |
| 162 |
Path: "/user/123", |
| 163 |
}, |
| 164 |
header: nil, |
| 165 |
expected: []attribute.KeyValue{ |
| 166 |
attribute.String("net.transport", "ip_tcp"), |
| 167 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 168 |
attribute.Int("net.peer.port", 56), |
| 169 |
}, |
| 170 |
}, |
| 171 |
{ |
| 172 |
name: "with remote ipv6 and port", |
| 173 |
network: "tcp", |
| 174 |
method: http.MethodGet, |
| 175 |
requestURI: "/user/123", |
| 176 |
proto: "HTTP/1.0", |
| 177 |
remoteAddr: "[fe80::0202:b3ff:fe1e:8329]:56", |
| 178 |
host: "", |
| 179 |
url: &url.URL{ |
| 180 |
Path: "/user/123", |
| 181 |
}, |
| 182 |
header: nil, |
| 183 |
expected: []attribute.KeyValue{ |
| 184 |
attribute.String("net.transport", "ip_tcp"), |
| 185 |
attribute.String("net.peer.ip", "fe80::202:b3ff:fe1e:8329"), |
| 186 |
attribute.Int("net.peer.port", 56), |
| 187 |
}, |
| 188 |
}, |
| 189 |
{ |
| 190 |
name: "with remote ipv4-in-v6 and port", |
| 191 |
network: "tcp", |
| 192 |
method: http.MethodGet, |
| 193 |
requestURI: "/user/123", |
| 194 |
proto: "HTTP/1.0", |
| 195 |
remoteAddr: "[::ffff:192.168.0.1]:56", |
| 196 |
host: "", |
| 197 |
url: &url.URL{ |
| 198 |
Path: "/user/123", |
| 199 |
}, |
| 200 |
header: nil, |
| 201 |
expected: []attribute.KeyValue{ |
| 202 |
attribute.String("net.transport", "ip_tcp"), |
| 203 |
attribute.String("net.peer.ip", "192.168.0.1"), |
| 204 |
attribute.Int("net.peer.port", 56), |
| 205 |
}, |
| 206 |
}, |
| 207 |
{ |
| 208 |
name: "with remote name and port", |
| 209 |
network: "tcp", |
| 210 |
method: http.MethodGet, |
| 211 |
requestURI: "/user/123", |
| 212 |
proto: "HTTP/1.0", |
| 213 |
remoteAddr: "example.com:56", |
| 214 |
host: "", |
| 215 |
url: &url.URL{ |
| 216 |
Path: "/user/123", |
| 217 |
}, |
| 218 |
header: nil, |
| 219 |
expected: []attribute.KeyValue{ |
| 220 |
attribute.String("net.transport", "ip_tcp"), |
| 221 |
attribute.String("net.peer.name", "example.com"), |
| 222 |
attribute.Int("net.peer.port", 56), |
| 223 |
}, |
| 224 |
}, |
| 225 |
{ |
| 226 |
name: "with remote ipv4 only", |
| 227 |
network: "tcp", |
| 228 |
method: http.MethodGet, |
| 229 |
requestURI: "/user/123", |
| 230 |
proto: "HTTP/1.0", |
| 231 |
remoteAddr: "1.2.3.4", |
| 232 |
host: "", |
| 233 |
url: &url.URL{ |
| 234 |
Path: "/user/123", |
| 235 |
}, |
| 236 |
header: nil, |
| 237 |
expected: []attribute.KeyValue{ |
| 238 |
attribute.String("net.transport", "ip_tcp"), |
| 239 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 240 |
}, |
| 241 |
}, |
| 242 |
{ |
| 243 |
name: "with remote ipv6 only", |
| 244 |
network: "tcp", |
| 245 |
method: http.MethodGet, |
| 246 |
requestURI: "/user/123", |
| 247 |
proto: "HTTP/1.0", |
| 248 |
remoteAddr: "fe80::0202:b3ff:fe1e:8329", |
| 249 |
host: "", |
| 250 |
url: &url.URL{ |
| 251 |
Path: "/user/123", |
| 252 |
}, |
| 253 |
header: nil, |
| 254 |
expected: []attribute.KeyValue{ |
| 255 |
attribute.String("net.transport", "ip_tcp"), |
| 256 |
attribute.String("net.peer.ip", "fe80::202:b3ff:fe1e:8329"), |
| 257 |
}, |
| 258 |
}, |
| 259 |
{ |
| 260 |
name: "with remote ipv4_in_v6 only", |
| 261 |
network: "tcp", |
| 262 |
method: http.MethodGet, |
| 263 |
requestURI: "/user/123", |
| 264 |
proto: "HTTP/1.0", |
| 265 |
remoteAddr: "::ffff:192.168.0.1", // section 2.5.5.2 of RFC4291 |
| 266 |
host: "", |
| 267 |
url: &url.URL{ |
| 268 |
Path: "/user/123", |
| 269 |
}, |
| 270 |
header: nil, |
| 271 |
expected: []attribute.KeyValue{ |
| 272 |
attribute.String("net.transport", "ip_tcp"), |
| 273 |
attribute.String("net.peer.ip", "192.168.0.1"), |
| 274 |
}, |
| 275 |
}, |
| 276 |
{ |
| 277 |
name: "with remote name only", |
| 278 |
network: "tcp", |
| 279 |
method: http.MethodGet, |
| 280 |
requestURI: "/user/123", |
| 281 |
proto: "HTTP/1.0", |
| 282 |
remoteAddr: "example.com", |
| 283 |
host: "", |
| 284 |
url: &url.URL{ |
| 285 |
Path: "/user/123", |
| 286 |
}, |
| 287 |
header: nil, |
| 288 |
expected: []attribute.KeyValue{ |
| 289 |
attribute.String("net.transport", "ip_tcp"), |
| 290 |
attribute.String("net.peer.name", "example.com"), |
| 291 |
}, |
| 292 |
}, |
| 293 |
{ |
| 294 |
name: "with remote port only", |
| 295 |
network: "tcp", |
| 296 |
method: http.MethodGet, |
| 297 |
requestURI: "/user/123", |
| 298 |
proto: "HTTP/1.0", |
| 299 |
remoteAddr: ":56", |
| 300 |
host: "", |
| 301 |
url: &url.URL{ |
| 302 |
Path: "/user/123", |
| 303 |
}, |
| 304 |
header: nil, |
| 305 |
expected: []attribute.KeyValue{ |
| 306 |
attribute.String("net.transport", "ip_tcp"), |
| 307 |
attribute.Int("net.peer.port", 56), |
| 308 |
}, |
| 309 |
}, |
| 310 |
{ |
| 311 |
name: "with host name only", |
| 312 |
network: "tcp", |
| 313 |
method: http.MethodGet, |
| 314 |
requestURI: "/user/123", |
| 315 |
proto: "HTTP/1.0", |
| 316 |
remoteAddr: "1.2.3.4:56", |
| 317 |
host: "example.com", |
| 318 |
url: &url.URL{ |
| 319 |
Path: "/user/123", |
| 320 |
}, |
| 321 |
header: nil, |
| 322 |
expected: []attribute.KeyValue{ |
| 323 |
attribute.String("net.transport", "ip_tcp"), |
| 324 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 325 |
attribute.Int("net.peer.port", 56), |
| 326 |
attribute.String("net.host.name", "example.com"), |
| 327 |
}, |
| 328 |
}, |
| 329 |
{ |
| 330 |
name: "with host ipv4 only", |
| 331 |
network: "tcp", |
| 332 |
method: http.MethodGet, |
| 333 |
requestURI: "/user/123", |
| 334 |
proto: "HTTP/1.0", |
| 335 |
remoteAddr: "1.2.3.4:56", |
| 336 |
host: "4.3.2.1", |
| 337 |
url: &url.URL{ |
| 338 |
Path: "/user/123", |
| 339 |
}, |
| 340 |
header: nil, |
| 341 |
expected: []attribute.KeyValue{ |
| 342 |
attribute.String("net.transport", "ip_tcp"), |
| 343 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 344 |
attribute.Int("net.peer.port", 56), |
| 345 |
attribute.String("net.host.ip", "4.3.2.1"), |
| 346 |
}, |
| 347 |
}, |
| 348 |
{ |
| 349 |
name: "with host ipv6 only", |
| 350 |
network: "tcp", |
| 351 |
method: http.MethodGet, |
| 352 |
requestURI: "/user/123", |
| 353 |
proto: "HTTP/1.0", |
| 354 |
remoteAddr: "1.2.3.4:56", |
| 355 |
host: "fe80::0202:b3ff:fe1e:8329", |
| 356 |
url: &url.URL{ |
| 357 |
Path: "/user/123", |
| 358 |
}, |
| 359 |
header: nil, |
| 360 |
expected: []attribute.KeyValue{ |
| 361 |
attribute.String("net.transport", "ip_tcp"), |
| 362 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 363 |
attribute.Int("net.peer.port", 56), |
| 364 |
attribute.String("net.host.ip", "fe80::202:b3ff:fe1e:8329"), |
| 365 |
}, |
| 366 |
}, |
| 367 |
{ |
| 368 |
name: "with host name and port", |
| 369 |
network: "tcp", |
| 370 |
method: http.MethodGet, |
| 371 |
requestURI: "/user/123", |
| 372 |
proto: "HTTP/1.0", |
| 373 |
remoteAddr: "1.2.3.4:56", |
| 374 |
host: "example.com:78", |
| 375 |
url: &url.URL{ |
| 376 |
Path: "/user/123", |
| 377 |
}, |
| 378 |
header: nil, |
| 379 |
expected: []attribute.KeyValue{ |
| 380 |
attribute.String("net.transport", "ip_tcp"), |
| 381 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 382 |
attribute.Int("net.peer.port", 56), |
| 383 |
attribute.String("net.host.name", "example.com"), |
| 384 |
attribute.Int("net.host.port", 78), |
| 385 |
}, |
| 386 |
}, |
| 387 |
{ |
| 388 |
name: "with host ipv4 and port", |
| 389 |
network: "tcp", |
| 390 |
method: http.MethodGet, |
| 391 |
requestURI: "/user/123", |
| 392 |
proto: "HTTP/1.0", |
| 393 |
remoteAddr: "1.2.3.4:56", |
| 394 |
host: "4.3.2.1:78", |
| 395 |
url: &url.URL{ |
| 396 |
Path: "/user/123", |
| 397 |
}, |
| 398 |
header: nil, |
| 399 |
expected: []attribute.KeyValue{ |
| 400 |
attribute.String("net.transport", "ip_tcp"), |
| 401 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 402 |
attribute.Int("net.peer.port", 56), |
| 403 |
attribute.String("net.host.ip", "4.3.2.1"), |
| 404 |
attribute.Int("net.host.port", 78), |
| 405 |
}, |
| 406 |
}, |
| 407 |
{ |
| 408 |
name: "with host ipv6 and port", |
| 409 |
network: "tcp", |
| 410 |
method: http.MethodGet, |
| 411 |
requestURI: "/user/123", |
| 412 |
proto: "HTTP/1.0", |
| 413 |
remoteAddr: "1.2.3.4:56", |
| 414 |
host: "[fe80::202:b3ff:fe1e:8329]:78", |
| 415 |
url: &url.URL{ |
| 416 |
Path: "/user/123", |
| 417 |
}, |
| 418 |
header: nil, |
| 419 |
expected: []attribute.KeyValue{ |
| 420 |
attribute.String("net.transport", "ip_tcp"), |
| 421 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 422 |
attribute.Int("net.peer.port", 56), |
| 423 |
attribute.String("net.host.ip", "fe80::202:b3ff:fe1e:8329"), |
| 424 |
attribute.Int("net.host.port", 78), |
| 425 |
}, |
| 426 |
}, |
| 427 |
{ |
| 428 |
name: "with host name and bogus port", |
| 429 |
network: "tcp", |
| 430 |
method: http.MethodGet, |
| 431 |
requestURI: "/user/123", |
| 432 |
proto: "HTTP/1.0", |
| 433 |
remoteAddr: "1.2.3.4:56", |
| 434 |
host: "example.com:qwerty", |
| 435 |
url: &url.URL{ |
| 436 |
Path: "/user/123", |
| 437 |
}, |
| 438 |
header: nil, |
| 439 |
expected: []attribute.KeyValue{ |
| 440 |
attribute.String("net.transport", "ip_tcp"), |
| 441 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 442 |
attribute.Int("net.peer.port", 56), |
| 443 |
attribute.String("net.host.name", "example.com"), |
| 444 |
}, |
| 445 |
}, |
| 446 |
{ |
| 447 |
name: "with host ipv4 and bogus port", |
| 448 |
network: "tcp", |
| 449 |
method: http.MethodGet, |
| 450 |
requestURI: "/user/123", |
| 451 |
proto: "HTTP/1.0", |
| 452 |
remoteAddr: "1.2.3.4:56", |
| 453 |
host: "4.3.2.1:qwerty", |
| 454 |
url: &url.URL{ |
| 455 |
Path: "/user/123", |
| 456 |
}, |
| 457 |
header: nil, |
| 458 |
expected: []attribute.KeyValue{ |
| 459 |
attribute.String("net.transport", "ip_tcp"), |
| 460 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 461 |
attribute.Int("net.peer.port", 56), |
| 462 |
attribute.String("net.host.ip", "4.3.2.1"), |
| 463 |
}, |
| 464 |
}, |
| 465 |
{ |
| 466 |
name: "with host ipv6 and bogus port", |
| 467 |
network: "tcp", |
| 468 |
method: http.MethodGet, |
| 469 |
requestURI: "/user/123", |
| 470 |
proto: "HTTP/1.0", |
| 471 |
remoteAddr: "1.2.3.4:56", |
| 472 |
host: "[fe80::202:b3ff:fe1e:8329]:qwerty", |
| 473 |
url: &url.URL{ |
| 474 |
Path: "/user/123", |
| 475 |
}, |
| 476 |
header: nil, |
| 477 |
expected: []attribute.KeyValue{ |
| 478 |
attribute.String("net.transport", "ip_tcp"), |
| 479 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 480 |
attribute.Int("net.peer.port", 56), |
| 481 |
attribute.String("net.host.ip", "fe80::202:b3ff:fe1e:8329"), |
| 482 |
}, |
| 483 |
}, |
| 484 |
{ |
| 485 |
name: "with empty host and port", |
| 486 |
network: "tcp", |
| 487 |
method: http.MethodGet, |
| 488 |
requestURI: "/user/123", |
| 489 |
proto: "HTTP/1.0", |
| 490 |
remoteAddr: "1.2.3.4:56", |
| 491 |
host: ":80", |
| 492 |
url: &url.URL{ |
| 493 |
Path: "/user/123", |
| 494 |
}, |
| 495 |
header: nil, |
| 496 |
expected: []attribute.KeyValue{ |
| 497 |
attribute.String("net.transport", "ip_tcp"), |
| 498 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 499 |
attribute.Int("net.peer.port", 56), |
| 500 |
attribute.Int("net.host.port", 80), |
| 501 |
}, |
| 502 |
}, |
| 503 |
{ |
| 504 |
name: "with host ip and port in headers", |
| 505 |
network: "tcp", |
| 506 |
method: http.MethodGet, |
| 507 |
requestURI: "/user/123", |
| 508 |
proto: "HTTP/1.0", |
| 509 |
remoteAddr: "1.2.3.4:56", |
| 510 |
host: "", |
| 511 |
url: &url.URL{ |
| 512 |
Path: "/user/123", |
| 513 |
}, |
| 514 |
header: http.Header{ |
| 515 |
"Host": []string{"4.3.2.1:78"}, |
| 516 |
}, |
| 517 |
expected: []attribute.KeyValue{ |
| 518 |
attribute.String("net.transport", "ip_tcp"), |
| 519 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 520 |
attribute.Int("net.peer.port", 56), |
| 521 |
attribute.String("net.host.ip", "4.3.2.1"), |
| 522 |
attribute.Int("net.host.port", 78), |
| 523 |
}, |
| 524 |
}, |
| 525 |
{ |
| 526 |
name: "with host ipv4 and port in url", |
| 527 |
network: "tcp", |
| 528 |
method: http.MethodGet, |
| 529 |
requestURI: "http://4.3.2.1:78/user/123", |
| 530 |
proto: "HTTP/1.0", |
| 531 |
remoteAddr: "1.2.3.4:56", |
| 532 |
host: "", |
| 533 |
url: &url.URL{ |
| 534 |
Host: "4.3.2.1:78", |
| 535 |
Path: "/user/123", |
| 536 |
}, |
| 537 |
header: nil, |
| 538 |
expected: []attribute.KeyValue{ |
| 539 |
attribute.String("net.transport", "ip_tcp"), |
| 540 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 541 |
attribute.Int("net.peer.port", 56), |
| 542 |
attribute.String("net.host.ip", "4.3.2.1"), |
| 543 |
attribute.Int("net.host.port", 78), |
| 544 |
}, |
| 545 |
}, |
| 546 |
{ |
| 547 |
name: "with host ipv6 and port in url", |
| 548 |
network: "tcp", |
| 549 |
method: http.MethodGet, |
| 550 |
requestURI: "http://4.3.2.1:78/user/123", |
| 551 |
proto: "HTTP/1.0", |
| 552 |
remoteAddr: "1.2.3.4:56", |
| 553 |
host: "", |
| 554 |
url: &url.URL{ |
| 555 |
Host: "[fe80::202:b3ff:fe1e:8329]:78", |
| 556 |
Path: "/user/123", |
| 557 |
}, |
| 558 |
header: nil, |
| 559 |
expected: []attribute.KeyValue{ |
| 560 |
attribute.String("net.transport", "ip_tcp"), |
| 561 |
attribute.String("net.peer.ip", "1.2.3.4"), |
| 562 |
attribute.Int("net.peer.port", 56), |
| 563 |
attribute.String("net.host.ip", "fe80::202:b3ff:fe1e:8329"), |
| 564 |
attribute.Int("net.host.port", 78), |
| 565 |
}, |
| 566 |
}, |
| 567 |
} |
| 568 |
for _, tc := range testcases { |
| 569 |
t.Run(tc.name, func(t *testing.T) { |
| 570 |
r := testRequest(tc.method, tc.requestURI, tc.proto, tc.remoteAddr, tc.host, tc.url, tc.header, noTLS) |
| 571 |
got := sc.NetAttributesFromHTTPRequest(tc.network, r) |
| 572 |
if diff := cmp.Diff( |
| 573 |
tc.expected, |
| 574 |
got, |
| 575 |
cmp.AllowUnexported(attribute.Value{})); diff != "" { |
| 576 |
t.Fatalf("attributes differ: diff %+v,", diff) |
| 577 |
} |
| 578 |
}) |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
func TestEndUserAttributesFromHTTPRequest(t *testing.T) { |
| 583 |
r := testRequest(http.MethodGet, "/user/123", "HTTP/1.1", "", "", nil, http.Header{}, withTLS) |
| 584 |
var expected []attribute.KeyValue |
| 585 |
got := sc.EndUserAttributesFromHTTPRequest(r) |
| 586 |
assert.ElementsMatch(t, expected, got) |
| 587 |
r.SetBasicAuth("admin", "password") |
| 588 |
expected = []attribute.KeyValue{attribute.String("enduser.id", "admin")} |
| 589 |
got = sc.EndUserAttributesFromHTTPRequest(r) |
| 590 |
assert.ElementsMatch(t, expected, got) |
| 591 |
} |
| 592 |
|
| 593 |
func TestHTTPServerAttributesFromHTTPRequest(t *testing.T) { |
| 594 |
type testcase struct { |
| 595 |
name string |
| 596 |
|
| 597 |
serverName string |
| 598 |
route string |
| 599 |
|
| 600 |
method string |
| 601 |
requestURI string |
| 602 |
proto string |
| 603 |
remoteAddr string |
| 604 |
host string |
| 605 |
url *url.URL |
| 606 |
header http.Header |
| 607 |
tls tlsOption |
| 608 |
contentLength int64 |
| 609 |
|
| 610 |
expected []attribute.KeyValue |
| 611 |
} |
| 612 |
testcases := []testcase{ |
| 613 |
{ |
| 614 |
name: "stripped", |
| 615 |
serverName: "", |
| 616 |
route: "", |
| 617 |
method: http.MethodGet, |
| 618 |
requestURI: "/user/123", |
| 619 |
proto: "HTTP/1.0", |
| 620 |
remoteAddr: "", |
| 621 |
host: "", |
| 622 |
url: &url.URL{ |
| 623 |
Path: "/user/123", |
| 624 |
}, |
| 625 |
header: nil, |
| 626 |
tls: noTLS, |
| 627 |
expected: []attribute.KeyValue{ |
| 628 |
attribute.String("http.method", http.MethodGet), |
| 629 |
attribute.String("http.target", "/user/123"), |
| 630 |
attribute.String("http.scheme", "http"), |
| 631 |
attribute.String("http.flavor", "1.0"), |
| 632 |
}, |
| 633 |
}, |
| 634 |
{ |
| 635 |
name: "with server name", |
| 636 |
serverName: "my-server-name", |
| 637 |
route: "", |
| 638 |
method: http.MethodGet, |
| 639 |
requestURI: "/user/123", |
| 640 |
proto: "HTTP/1.0", |
| 641 |
remoteAddr: "", |
| 642 |
host: "", |
| 643 |
url: &url.URL{ |
| 644 |
Path: "/user/123", |
| 645 |
}, |
| 646 |
header: nil, |
| 647 |
tls: noTLS, |
| 648 |
expected: []attribute.KeyValue{ |
| 649 |
attribute.String("http.method", http.MethodGet), |
| 650 |
attribute.String("http.target", "/user/123"), |
| 651 |
attribute.String("http.scheme", "http"), |
| 652 |
attribute.String("http.flavor", "1.0"), |
| 653 |
attribute.String("http.server_name", "my-server-name"), |
| 654 |
}, |
| 655 |
}, |
| 656 |
{ |
| 657 |
name: "with tls", |
| 658 |
serverName: "my-server-name", |
| 659 |
route: "", |
| 660 |
method: http.MethodGet, |
| 661 |
requestURI: "/user/123", |
| 662 |
proto: "HTTP/1.0", |
| 663 |
remoteAddr: "", |
| 664 |
host: "", |
| 665 |
url: &url.URL{ |
| 666 |
Path: "/user/123", |
| 667 |
}, |
| 668 |
header: nil, |
| 669 |
tls: withTLS, |
| 670 |
expected: []attribute.KeyValue{ |
| 671 |
attribute.String("http.method", http.MethodGet), |
| 672 |
attribute.String("http.target", "/user/123"), |
| 673 |
attribute.String("http.scheme", "https"), |
| 674 |
attribute.String("http.flavor", "1.0"), |
| 675 |
attribute.String("http.server_name", "my-server-name"), |
| 676 |
}, |
| 677 |
}, |
| 678 |
{ |
| 679 |
name: "with route", |
| 680 |
serverName: "my-server-name", |
| 681 |
route: "/user/:id", |
| 682 |
method: http.MethodGet, |
| 683 |
requestURI: "/user/123", |
| 684 |
proto: "HTTP/1.0", |
| 685 |
remoteAddr: "", |
| 686 |
host: "", |
| 687 |
url: &url.URL{ |
| 688 |
Path: "/user/123", |
| 689 |
}, |
| 690 |
header: nil, |
| 691 |
tls: withTLS, |
| 692 |
expected: []attribute.KeyValue{ |
| 693 |
attribute.String("http.method", http.MethodGet), |
| 694 |
attribute.String("http.target", "/user/123"), |
| 695 |
attribute.String("http.scheme", "https"), |
| 696 |
attribute.String("http.flavor", "1.0"), |
| 697 |
attribute.String("http.server_name", "my-server-name"), |
| 698 |
attribute.String("http.route", "/user/:id"), |
| 699 |
}, |
| 700 |
}, |
| 701 |
{ |
| 702 |
name: "with host", |
| 703 |
serverName: "my-server-name", |
| 704 |
route: "/user/:id", |
| 705 |
method: http.MethodGet, |
| 706 |
requestURI: "/user/123", |
| 707 |
proto: "HTTP/1.0", |
| 708 |
remoteAddr: "", |
| 709 |
host: "example.com", |
| 710 |
url: &url.URL{ |
| 711 |
Path: "/user/123", |
| 712 |
}, |
| 713 |
header: nil, |
| 714 |
tls: withTLS, |
| 715 |
expected: []attribute.KeyValue{ |
| 716 |
attribute.String("http.method", http.MethodGet), |
| 717 |
attribute.String("http.target", "/user/123"), |
| 718 |
attribute.String("http.scheme", "https"), |
| 719 |
attribute.String("http.flavor", "1.0"), |
| 720 |
attribute.String("http.server_name", "my-server-name"), |
| 721 |
attribute.String("http.route", "/user/:id"), |
| 722 |
attribute.String("http.host", "example.com"), |
| 723 |
}, |
| 724 |
}, |
| 725 |
{ |
| 726 |
name: "with host fallback", |
| 727 |
serverName: "my-server-name", |
| 728 |
route: "/user/:id", |
| 729 |
method: http.MethodGet, |
| 730 |
requestURI: "/user/123", |
| 731 |
proto: "HTTP/1.0", |
| 732 |
remoteAddr: "", |
| 733 |
host: "", |
| 734 |
url: &url.URL{ |
| 735 |
Host: "example.com", |
| 736 |
Path: "/user/123", |
| 737 |
}, |
| 738 |
header: nil, |
| 739 |
tls: withTLS, |
| 740 |
expected: []attribute.KeyValue{ |
| 741 |
attribute.String("http.method", http.MethodGet), |
| 742 |
attribute.String("http.target", "/user/123"), |
| 743 |
attribute.String("http.scheme", "https"), |
| 744 |
attribute.String("http.flavor", "1.0"), |
| 745 |
attribute.String("http.server_name", "my-server-name"), |
| 746 |
attribute.String("http.route", "/user/:id"), |
| 747 |
attribute.String("http.host", "example.com"), |
| 748 |
}, |
| 749 |
}, |
| 750 |
{ |
| 751 |
name: "with user agent", |
| 752 |
serverName: "my-server-name", |
| 753 |
route: "/user/:id", |
| 754 |
method: http.MethodGet, |
| 755 |
requestURI: "/user/123", |
| 756 |
proto: "HTTP/1.0", |
| 757 |
remoteAddr: "", |
| 758 |
host: "example.com", |
| 759 |
url: &url.URL{ |
| 760 |
Path: "/user/123", |
| 761 |
}, |
| 762 |
header: http.Header{ |
| 763 |
"User-Agent": []string{"foodownloader"}, |
| 764 |
}, |
| 765 |
tls: withTLS, |
| 766 |
expected: []attribute.KeyValue{ |
| 767 |
attribute.String("http.method", http.MethodGet), |
| 768 |
attribute.String("http.target", "/user/123"), |
| 769 |
attribute.String("http.scheme", "https"), |
| 770 |
attribute.String("http.flavor", "1.0"), |
| 771 |
attribute.String("http.server_name", "my-server-name"), |
| 772 |
attribute.String("http.route", "/user/:id"), |
| 773 |
attribute.String("http.host", "example.com"), |
| 774 |
attribute.String("http.user_agent", "foodownloader"), |
| 775 |
}, |
| 776 |
}, |
| 777 |
{ |
| 778 |
name: "with proxy info", |
| 779 |
serverName: "my-server-name", |
| 780 |
route: "/user/:id", |
| 781 |
method: http.MethodGet, |
| 782 |
requestURI: "/user/123", |
| 783 |
proto: "HTTP/1.0", |
| 784 |
remoteAddr: "", |
| 785 |
host: "example.com", |
| 786 |
url: &url.URL{ |
| 787 |
Path: "/user/123", |
| 788 |
}, |
| 789 |
header: http.Header{ |
| 790 |
"User-Agent": []string{"foodownloader"}, |
| 791 |
"X-Forwarded-For": []string{"203.0.113.195, 70.41.3.18, 150.172.238.178"}, |
| 792 |
}, |
| 793 |
tls: withTLS, |
| 794 |
expected: []attribute.KeyValue{ |
| 795 |
attribute.String("http.method", http.MethodGet), |
| 796 |
attribute.String("http.target", "/user/123"), |
| 797 |
attribute.String("http.scheme", "https"), |
| 798 |
attribute.String("http.flavor", "1.0"), |
| 799 |
attribute.String("http.server_name", "my-server-name"), |
| 800 |
attribute.String("http.route", "/user/:id"), |
| 801 |
attribute.String("http.host", "example.com"), |
| 802 |
attribute.String("http.user_agent", "foodownloader"), |
| 803 |
attribute.String("http.client_ip", "203.0.113.195"), |
| 804 |
}, |
| 805 |
}, |
| 806 |
{ |
| 807 |
name: "with http 1.1", |
| 808 |
serverName: "my-server-name", |
| 809 |
route: "/user/:id", |
| 810 |
method: http.MethodGet, |
| 811 |
requestURI: "/user/123", |
| 812 |
proto: "HTTP/1.1", |
| 813 |
remoteAddr: "", |
| 814 |
host: "example.com", |
| 815 |
url: &url.URL{ |
| 816 |
Path: "/user/123", |
| 817 |
}, |
| 818 |
header: http.Header{ |
| 819 |
"User-Agent": []string{"foodownloader"}, |
| 820 |
"X-Forwarded-For": []string{"1.2.3.4"}, |
| 821 |
}, |
| 822 |
tls: withTLS, |
| 823 |
expected: []attribute.KeyValue{ |
| 824 |
attribute.String("http.method", http.MethodGet), |
| 825 |
attribute.String("http.target", "/user/123"), |
| 826 |
attribute.String("http.scheme", "https"), |
| 827 |
attribute.String("http.flavor", "1.1"), |
| 828 |
attribute.String("http.server_name", "my-server-name"), |
| 829 |
attribute.String("http.route", "/user/:id"), |
| 830 |
attribute.String("http.host", "example.com"), |
| 831 |
attribute.String("http.user_agent", "foodownloader"), |
| 832 |
attribute.String("http.client_ip", "1.2.3.4"), |
| 833 |
}, |
| 834 |
}, |
| 835 |
{ |
| 836 |
name: "with http 2", |
| 837 |
serverName: "my-server-name", |
| 838 |
route: "/user/:id", |
| 839 |
method: http.MethodGet, |
| 840 |
requestURI: "/user/123", |
| 841 |
proto: "HTTP/2.0", |
| 842 |
remoteAddr: "", |
| 843 |
host: "example.com", |
| 844 |
url: &url.URL{ |
| 845 |
Path: "/user/123", |
| 846 |
}, |
| 847 |
header: http.Header{ |
| 848 |
"User-Agent": []string{"foodownloader"}, |
| 849 |
"X-Forwarded-For": []string{"1.2.3.4"}, |
| 850 |
}, |
| 851 |
tls: withTLS, |
| 852 |
expected: []attribute.KeyValue{ |
| 853 |
attribute.String("http.method", http.MethodGet), |
| 854 |
attribute.String("http.target", "/user/123"), |
| 855 |
attribute.String("http.scheme", "https"), |
| 856 |
attribute.String("http.flavor", "2"), |
| 857 |
attribute.String("http.server_name", "my-server-name"), |
| 858 |
attribute.String("http.route", "/user/:id"), |
| 859 |
attribute.String("http.host", "example.com"), |
| 860 |
attribute.String("http.user_agent", "foodownloader"), |
| 861 |
attribute.String("http.client_ip", "1.2.3.4"), |
| 862 |
}, |
| 863 |
}, |
| 864 |
{ |
| 865 |
name: "with content length", |
| 866 |
method: http.MethodGet, |
| 867 |
requestURI: "/user/123", |
| 868 |
contentLength: 100, |
| 869 |
expected: []attribute.KeyValue{ |
| 870 |
attribute.String("http.method", http.MethodGet), |
| 871 |
attribute.String("http.target", "/user/123"), |
| 872 |
attribute.String("http.scheme", "http"), |
| 873 |
attribute.Int64("http.request_content_length", 100), |
| 874 |
}, |
| 875 |
}, |
| 876 |
} |
| 877 |
for idx, tc := range testcases { |
| 878 |
r := testRequest(tc.method, tc.requestURI, tc.proto, tc.remoteAddr, tc.host, tc.url, tc.header, tc.tls) |
| 879 |
r.ContentLength = tc.contentLength |
| 880 |
got := sc.HTTPServerAttributesFromHTTPRequest(tc.serverName, tc.route, r) |
| 881 |
assertElementsMatch(t, tc.expected, got, "testcase %d - %s", idx, tc.name) |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
func TestHTTPAttributesFromHTTPStatusCode(t *testing.T) { |
| 886 |
expected := []attribute.KeyValue{ |
| 887 |
attribute.Int("http.status_code", 404), |
| 888 |
} |
| 889 |
got := sc.HTTPAttributesFromHTTPStatusCode(http.StatusNotFound) |
| 890 |
assertElementsMatch(t, expected, got, "with valid HTTP status code") |
| 891 |
assert.ElementsMatch(t, expected, got) |
| 892 |
expected = []attribute.KeyValue{ |
| 893 |
attribute.Int("http.status_code", 499), |
| 894 |
} |
| 895 |
got = sc.HTTPAttributesFromHTTPStatusCode(499) |
| 896 |
assertElementsMatch(t, expected, got, "with invalid HTTP status code") |
| 897 |
} |
| 898 |
|
| 899 |
func TestSpanStatusFromHTTPStatusCode(t *testing.T) { |
| 900 |
for code := range 1000 { |
| 901 |
expected := getExpectedCodeForHTTPCode(code, trace.SpanKindClient) |
| 902 |
got, msg := SpanStatusFromHTTPStatusCode(code) |
| 903 |
assert.Equalf(t, expected, got, "%s vs %s", expected, got) |
| 904 |
|
| 905 |
_, valid := validateHTTPStatusCode(code) |
| 906 |
if !valid { |
| 907 |
assert.NotEmpty(t, msg, "message should be set if error cannot be inferred from code") |
| 908 |
} else { |
| 909 |
assert.Empty(t, msg, "message should not be set if error can be inferred from code") |
| 910 |
} |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
func TestSpanStatusFromHTTPStatusCodeAndSpanKind(t *testing.T) { |
| 915 |
for code := range 1000 { |
| 916 |
expected := getExpectedCodeForHTTPCode(code, trace.SpanKindClient) |
| 917 |
got, msg := SpanStatusFromHTTPStatusCodeAndSpanKind(code, trace.SpanKindClient) |
| 918 |
assert.Equalf(t, expected, got, "%s vs %s", expected, got) |
| 919 |
|
| 920 |
_, valid := validateHTTPStatusCode(code) |
| 921 |
if !valid { |
| 922 |
assert.NotEmpty(t, msg, "message should be set if error cannot be inferred from code") |
| 923 |
} else { |
| 924 |
assert.Empty(t, msg, "message should not be set if error can be inferred from code") |
| 925 |
} |
| 926 |
} |
| 927 |
code, _ := SpanStatusFromHTTPStatusCodeAndSpanKind(400, trace.SpanKindServer) |
| 928 |
assert.Equalf(t, codes.Unset, code, "message should be set if error cannot be inferred from code") |
| 929 |
} |
| 930 |
|
| 931 |
func getExpectedCodeForHTTPCode(code int, spanKind trace.SpanKind) codes.Code { |
| 932 |
if http.StatusText(code) == "" { |
| 933 |
return codes.Error |
| 934 |
} |
| 935 |
switch code { |
| 936 |
case |
| 937 |
http.StatusUnauthorized, |
| 938 |
http.StatusForbidden, |
| 939 |
http.StatusNotFound, |
| 940 |
http.StatusTooManyRequests, |
| 941 |
http.StatusNotImplemented, |
| 942 |
http.StatusServiceUnavailable, |
| 943 |
http.StatusGatewayTimeout: |
| 944 |
return codes.Error |
| 945 |
} |
| 946 |
category := code / 100 |
| 947 |
if category > 0 && category < 4 { |
| 948 |
return codes.Unset |
| 949 |
} |
| 950 |
if spanKind == trace.SpanKindServer && category == 4 { |
| 951 |
return codes.Unset |
| 952 |
} |
| 953 |
return codes.Error |
| 954 |
} |
| 955 |
|
| 956 |
func assertElementsMatch(t *testing.T, expected, got []attribute.KeyValue, format string, args ...any) { |
| 957 |
if !assert.ElementsMatchf(t, expected, got, format, args...) { |
| 958 |
t.Log("expected:", kvStr(expected)) |
| 959 |
t.Log("got:", kvStr(got)) |
| 960 |
} |
| 961 |
} |
| 962 |
|
| 963 |
func testRequest( |
| 964 |
method, requestURI, proto, remoteAddr, host string, |
| 965 |
u *url.URL, |
| 966 |
header http.Header, |
| 967 |
tlsopt tlsOption, |
| 968 |
) *http.Request { |
| 969 |
major, minor := protoToInts(proto) |
| 970 |
var tlsConn *tls.ConnectionState |
| 971 |
switch tlsopt { |
| 972 |
case noTLS: |
| 973 |
case withTLS: |
| 974 |
tlsConn = &tls.ConnectionState{} |
| 975 |
} |
| 976 |
return &http.Request{ |
| 977 |
Method: method, |
| 978 |
URL: u, |
| 979 |
Proto: proto, |
| 980 |
ProtoMajor: major, |
| 981 |
ProtoMinor: minor, |
| 982 |
Header: header, |
| 983 |
Host: host, |
| 984 |
RemoteAddr: remoteAddr, |
| 985 |
RequestURI: requestURI, |
| 986 |
TLS: tlsConn, |
| 987 |
} |
| 988 |
} |
| 989 |
|
| 990 |
func protoToInts(proto string) (int, int) { |
| 991 |
switch proto { |
| 992 |
case "HTTP/1.0": |
| 993 |
return 1, 0 |
| 994 |
case "HTTP/1.1": |
| 995 |
return 1, 1 |
| 996 |
case "HTTP/2.0": |
| 997 |
return 2, 0 |
| 998 |
} |
| 999 |
// invalid proto |
| 1000 |
return 13, 42 |
| 1001 |
} |
| 1002 |
|
| 1003 |
func kvStr(kvs []attribute.KeyValue) string { |
| 1004 |
sb := strings.Builder{} |
| 1005 |
_, _ = sb.WriteRune('[') |
| 1006 |
for idx, attr := range kvs { |
| 1007 |
if idx > 0 { |
| 1008 |
_, _ = sb.WriteString(", ") |
| 1009 |
} |
| 1010 |
_, _ = sb.WriteString(string(attr.Key)) |
| 1011 |
_, _ = sb.WriteString(": ") |
| 1012 |
_, _ = sb.WriteString(attr.Value.Emit()) |
| 1013 |
} |
| 1014 |
_, _ = sb.WriteRune(']') |
| 1015 |
return sb.String() |
| 1016 |
} |
| 1017 |
|
| 1018 |
func TestHTTPClientAttributesFromHTTPRequest(t *testing.T) { |
| 1019 |
testCases := []struct { |
| 1020 |
name string |
| 1021 |
|
| 1022 |
method string |
| 1023 |
requestURI string |
| 1024 |
proto string |
| 1025 |
remoteAddr string |
| 1026 |
host string |
| 1027 |
url *url.URL |
| 1028 |
header http.Header |
| 1029 |
tls tlsOption |
| 1030 |
contentLength int64 |
| 1031 |
|
| 1032 |
expected []attribute.KeyValue |
| 1033 |
}{ |
| 1034 |
{ |
| 1035 |
name: "stripped", |
| 1036 |
method: http.MethodGet, |
| 1037 |
requestURI: "/user/123", |
| 1038 |
proto: "HTTP/1.0", |
| 1039 |
remoteAddr: "", |
| 1040 |
host: "", |
| 1041 |
url: &url.URL{ |
| 1042 |
Path: "/user/123", |
| 1043 |
}, |
| 1044 |
header: nil, |
| 1045 |
tls: noTLS, |
| 1046 |
expected: []attribute.KeyValue{ |
| 1047 |
attribute.String("http.method", http.MethodGet), |
| 1048 |
attribute.String("http.url", "/user/123"), |
| 1049 |
attribute.String("http.scheme", "http"), |
| 1050 |
attribute.String("http.flavor", "1.0"), |
| 1051 |
}, |
| 1052 |
}, |
| 1053 |
{ |
| 1054 |
name: "with tls", |
| 1055 |
method: http.MethodGet, |
| 1056 |
requestURI: "/user/123", |
| 1057 |
proto: "HTTP/1.0", |
| 1058 |
remoteAddr: "", |
| 1059 |
host: "", |
| 1060 |
url: &url.URL{ |
| 1061 |
Path: "/user/123", |
| 1062 |
}, |
| 1063 |
header: nil, |
| 1064 |
tls: withTLS, |
| 1065 |
expected: []attribute.KeyValue{ |
| 1066 |
attribute.String("http.method", http.MethodGet), |
| 1067 |
attribute.String("http.url", "/user/123"), |
| 1068 |
attribute.String("http.scheme", "https"), |
| 1069 |
attribute.String("http.flavor", "1.0"), |
| 1070 |
}, |
| 1071 |
}, |
| 1072 |
{ |
| 1073 |
name: "with host", |
| 1074 |
method: http.MethodGet, |
| 1075 |
requestURI: "/user/123", |
| 1076 |
proto: "HTTP/1.0", |
| 1077 |
remoteAddr: "", |
| 1078 |
host: "example.com", |
| 1079 |
url: &url.URL{ |
| 1080 |
Path: "/user/123", |
| 1081 |
}, |
| 1082 |
header: nil, |
| 1083 |
tls: withTLS, |
| 1084 |
expected: []attribute.KeyValue{ |
| 1085 |
attribute.String("http.method", http.MethodGet), |
| 1086 |
attribute.String("http.url", "/user/123"), |
| 1087 |
attribute.String("http.scheme", "https"), |
| 1088 |
attribute.String("http.flavor", "1.0"), |
| 1089 |
attribute.String("http.host", "example.com"), |
| 1090 |
}, |
| 1091 |
}, |
| 1092 |
{ |
| 1093 |
name: "with host fallback", |
| 1094 |
method: http.MethodGet, |
| 1095 |
requestURI: "/user/123", |
| 1096 |
proto: "HTTP/1.0", |
| 1097 |
remoteAddr: "", |
| 1098 |
host: "", |
| 1099 |
url: &url.URL{ |
| 1100 |
Scheme: "https", |
| 1101 |
Host: "example.com", |
| 1102 |
Path: "/user/123", |
| 1103 |
}, |
| 1104 |
header: nil, |
| 1105 |
tls: withTLS, |
| 1106 |
expected: []attribute.KeyValue{ |
| 1107 |
attribute.String("http.method", http.MethodGet), |
| 1108 |
attribute.String("http.url", "https://example.com/user/123"), |
| 1109 |
attribute.String("http.scheme", "https"), |
| 1110 |
attribute.String("http.flavor", "1.0"), |
| 1111 |
attribute.String("http.host", "example.com"), |
| 1112 |
}, |
| 1113 |
}, |
| 1114 |
{ |
| 1115 |
name: "with user agent", |
| 1116 |
method: http.MethodGet, |
| 1117 |
requestURI: "/user/123", |
| 1118 |
proto: "HTTP/1.0", |
| 1119 |
remoteAddr: "", |
| 1120 |
host: "example.com", |
| 1121 |
url: &url.URL{ |
| 1122 |
Path: "/user/123", |
| 1123 |
}, |
| 1124 |
header: http.Header{ |
| 1125 |
"User-Agent": []string{"foodownloader"}, |
| 1126 |
}, |
| 1127 |
tls: withTLS, |
| 1128 |
expected: []attribute.KeyValue{ |
| 1129 |
attribute.String("http.method", http.MethodGet), |
| 1130 |
attribute.String("http.url", "/user/123"), |
| 1131 |
attribute.String("http.scheme", "https"), |
| 1132 |
attribute.String("http.flavor", "1.0"), |
| 1133 |
attribute.String("http.host", "example.com"), |
| 1134 |
attribute.String("http.user_agent", "foodownloader"), |
| 1135 |
}, |
| 1136 |
}, |
| 1137 |
{ |
| 1138 |
name: "with http 1.1", |
| 1139 |
method: http.MethodGet, |
| 1140 |
requestURI: "/user/123", |
| 1141 |
proto: "HTTP/1.1", |
| 1142 |
remoteAddr: "", |
| 1143 |
host: "example.com", |
| 1144 |
url: &url.URL{ |
| 1145 |
Path: "/user/123", |
| 1146 |
}, |
| 1147 |
header: http.Header{ |
| 1148 |
"User-Agent": []string{"foodownloader"}, |
| 1149 |
}, |
| 1150 |
tls: withTLS, |
| 1151 |
expected: []attribute.KeyValue{ |
| 1152 |
attribute.String("http.method", http.MethodGet), |
| 1153 |
attribute.String("http.url", "/user/123"), |
| 1154 |
attribute.String("http.scheme", "https"), |
| 1155 |
attribute.String("http.flavor", "1.1"), |
| 1156 |
attribute.String("http.host", "example.com"), |
| 1157 |
attribute.String("http.user_agent", "foodownloader"), |
| 1158 |
}, |
| 1159 |
}, |
| 1160 |
{ |
| 1161 |
name: "with http 2", |
| 1162 |
method: http.MethodGet, |
| 1163 |
requestURI: "/user/123", |
| 1164 |
proto: "HTTP/2.0", |
| 1165 |
remoteAddr: "", |
| 1166 |
host: "example.com", |
| 1167 |
url: &url.URL{ |
| 1168 |
Path: "/user/123", |
| 1169 |
}, |
| 1170 |
header: http.Header{ |
| 1171 |
"User-Agent": []string{"foodownloader"}, |
| 1172 |
}, |
| 1173 |
tls: withTLS, |
| 1174 |
expected: []attribute.KeyValue{ |
| 1175 |
attribute.String("http.method", http.MethodGet), |
| 1176 |
attribute.String("http.url", "/user/123"), |
| 1177 |
attribute.String("http.scheme", "https"), |
| 1178 |
attribute.String("http.flavor", "2"), |
| 1179 |
attribute.String("http.host", "example.com"), |
| 1180 |
attribute.String("http.user_agent", "foodownloader"), |
| 1181 |
}, |
| 1182 |
}, |
| 1183 |
{ |
| 1184 |
name: "with content length", |
| 1185 |
method: http.MethodGet, |
| 1186 |
url: &url.URL{ |
| 1187 |
Path: "/user/123", |
| 1188 |
}, |
| 1189 |
contentLength: 100, |
| 1190 |
expected: []attribute.KeyValue{ |
| 1191 |
attribute.String("http.method", http.MethodGet), |
| 1192 |
attribute.String("http.url", "/user/123"), |
| 1193 |
attribute.String("http.scheme", "http"), |
| 1194 |
attribute.Int64("http.request_content_length", 100), |
| 1195 |
}, |
| 1196 |
}, |
| 1197 |
{ |
| 1198 |
name: "with empty method (fallback to GET)", |
| 1199 |
method: "", |
| 1200 |
url: &url.URL{ |
| 1201 |
Path: "/user/123", |
| 1202 |
}, |
| 1203 |
expected: []attribute.KeyValue{ |
| 1204 |
attribute.String("http.method", http.MethodGet), |
| 1205 |
attribute.String("http.url", "/user/123"), |
| 1206 |
attribute.String("http.scheme", "http"), |
| 1207 |
}, |
| 1208 |
}, |
| 1209 |
{ |
| 1210 |
name: "authentication information is stripped", |
| 1211 |
method: "", |
| 1212 |
url: &url.URL{ |
| 1213 |
Path: "/user/123", |
| 1214 |
User: url.UserPassword("foo", "bar"), |
| 1215 |
}, |
| 1216 |
expected: []attribute.KeyValue{ |
| 1217 |
attribute.String("http.method", http.MethodGet), |
| 1218 |
attribute.String("http.url", "/user/123"), |
| 1219 |
attribute.String("http.scheme", "http"), |
| 1220 |
}, |
| 1221 |
}, |
| 1222 |
} |
| 1223 |
|
| 1224 |
for _, tc := range testCases { |
| 1225 |
t.Run(tc.name, func(t *testing.T) { |
| 1226 |
r := testRequest(tc.method, tc.requestURI, tc.proto, tc.remoteAddr, tc.host, tc.url, tc.header, tc.tls) |
| 1227 |
r.ContentLength = tc.contentLength |
| 1228 |
got := sc.HTTPClientAttributesFromHTTPRequest(r) |
| 1229 |
assert.ElementsMatch(t, tc.expected, got) |
| 1230 |
}) |
| 1231 |
} |
| 1232 |
} |
| 1233 |
|
| 1234 |
func TestHTTPServerMetricAttributesFromHTTPRequest(t *testing.T) { |
| 1235 |
type testcase struct { |
| 1236 |
name string |
| 1237 |
serverName string |
| 1238 |
method string |
| 1239 |
requestURI string |
| 1240 |
proto string |
| 1241 |
remoteAddr string |
| 1242 |
host string |
| 1243 |
url *url.URL |
| 1244 |
header http.Header |
| 1245 |
tls tlsOption |
| 1246 |
contentLength int64 |
| 1247 |
expected []attribute.KeyValue |
| 1248 |
} |
| 1249 |
testcases := []testcase{ |
| 1250 |
{ |
| 1251 |
name: "stripped", |
| 1252 |
serverName: "", |
| 1253 |
method: http.MethodGet, |
| 1254 |
requestURI: "/user/123", |
| 1255 |
proto: "HTTP/1.0", |
| 1256 |
remoteAddr: "", |
| 1257 |
host: "", |
| 1258 |
url: &url.URL{ |
| 1259 |
Path: "/user/123", |
| 1260 |
}, |
| 1261 |
header: nil, |
| 1262 |
tls: noTLS, |
| 1263 |
expected: []attribute.KeyValue{ |
| 1264 |
attribute.String("http.method", http.MethodGet), |
| 1265 |
attribute.String("http.scheme", "http"), |
| 1266 |
attribute.String("http.flavor", "1.0"), |
| 1267 |
}, |
| 1268 |
}, |
| 1269 |
{ |
| 1270 |
name: "with server name", |
| 1271 |
serverName: "my-server-name", |
| 1272 |
method: http.MethodGet, |
| 1273 |
requestURI: "/user/123", |
| 1274 |
proto: "HTTP/1.0", |
| 1275 |
remoteAddr: "", |
| 1276 |
host: "", |
| 1277 |
url: &url.URL{ |
| 1278 |
Path: "/user/123", |
| 1279 |
}, |
| 1280 |
header: nil, |
| 1281 |
tls: noTLS, |
| 1282 |
expected: []attribute.KeyValue{ |
| 1283 |
attribute.String("http.method", http.MethodGet), |
| 1284 |
attribute.String("http.scheme", "http"), |
| 1285 |
attribute.String("http.flavor", "1.0"), |
| 1286 |
attribute.String("http.server_name", "my-server-name"), |
| 1287 |
}, |
| 1288 |
}, |
| 1289 |
{ |
| 1290 |
name: "with tls", |
| 1291 |
serverName: "my-server-name", |
| 1292 |
method: http.MethodGet, |
| 1293 |
requestURI: "/user/123", |
| 1294 |
proto: "HTTP/1.0", |
| 1295 |
remoteAddr: "", |
| 1296 |
host: "", |
| 1297 |
url: &url.URL{ |
| 1298 |
Path: "/user/123", |
| 1299 |
}, |
| 1300 |
header: nil, |
| 1301 |
tls: withTLS, |
| 1302 |
expected: []attribute.KeyValue{ |
| 1303 |
attribute.String("http.method", http.MethodGet), |
| 1304 |
attribute.String("http.scheme", "https"), |
| 1305 |
attribute.String("http.flavor", "1.0"), |
| 1306 |
attribute.String("http.server_name", "my-server-name"), |
| 1307 |
}, |
| 1308 |
}, |
| 1309 |
{ |
| 1310 |
name: "with route", |
| 1311 |
serverName: "my-server-name", |
| 1312 |
method: http.MethodGet, |
| 1313 |
requestURI: "/user/123", |
| 1314 |
proto: "HTTP/1.0", |
| 1315 |
remoteAddr: "", |
| 1316 |
host: "", |
| 1317 |
url: &url.URL{ |
| 1318 |
Path: "/user/123", |
| 1319 |
}, |
| 1320 |
header: nil, |
| 1321 |
tls: withTLS, |
| 1322 |
expected: []attribute.KeyValue{ |
| 1323 |
attribute.String("http.method", http.MethodGet), |
| 1324 |
attribute.String("http.scheme", "https"), |
| 1325 |
attribute.String("http.flavor", "1.0"), |
| 1326 |
attribute.String("http.server_name", "my-server-name"), |
| 1327 |
}, |
| 1328 |
}, |
| 1329 |
{ |
| 1330 |
name: "with host", |
| 1331 |
serverName: "my-server-name", |
| 1332 |
method: http.MethodGet, |
| 1333 |
requestURI: "/user/123", |
| 1334 |
proto: "HTTP/1.0", |
| 1335 |
remoteAddr: "", |
| 1336 |
host: "example.com", |
| 1337 |
url: &url.URL{ |
| 1338 |
Path: "/user/123", |
| 1339 |
}, |
| 1340 |
header: nil, |
| 1341 |
tls: withTLS, |
| 1342 |
expected: []attribute.KeyValue{ |
| 1343 |
attribute.String("http.method", http.MethodGet), |
| 1344 |
attribute.String("http.scheme", "https"), |
| 1345 |
attribute.String("http.flavor", "1.0"), |
| 1346 |
attribute.String("http.server_name", "my-server-name"), |
| 1347 |
attribute.String("http.host", "example.com"), |
| 1348 |
}, |
| 1349 |
}, |
| 1350 |
{ |
| 1351 |
name: "with host fallback", |
| 1352 |
serverName: "my-server-name", |
| 1353 |
method: http.MethodGet, |
| 1354 |
requestURI: "/user/123", |
| 1355 |
proto: "HTTP/1.0", |
| 1356 |
remoteAddr: "", |
| 1357 |
host: "", |
| 1358 |
url: &url.URL{ |
| 1359 |
Host: "example.com", |
| 1360 |
Path: "/user/123", |
| 1361 |
}, |
| 1362 |
header: nil, |
| 1363 |
tls: withTLS, |
| 1364 |
expected: []attribute.KeyValue{ |
| 1365 |
attribute.String("http.method", http.MethodGet), |
| 1366 |
attribute.String("http.scheme", "https"), |
| 1367 |
attribute.String("http.flavor", "1.0"), |
| 1368 |
attribute.String("http.server_name", "my-server-name"), |
| 1369 |
attribute.String("http.host", "example.com"), |
| 1370 |
}, |
| 1371 |
}, |
| 1372 |
{ |
| 1373 |
name: "with user agent", |
| 1374 |
serverName: "my-server-name", |
| 1375 |
method: http.MethodGet, |
| 1376 |
requestURI: "/user/123", |
| 1377 |
proto: "HTTP/1.0", |
| 1378 |
remoteAddr: "", |
| 1379 |
host: "example.com", |
| 1380 |
url: &url.URL{ |
| 1381 |
Path: "/user/123", |
| 1382 |
}, |
| 1383 |
header: http.Header{ |
| 1384 |
"User-Agent": []string{"foodownloader"}, |
| 1385 |
}, |
| 1386 |
tls: withTLS, |
| 1387 |
expected: []attribute.KeyValue{ |
| 1388 |
attribute.String("http.method", http.MethodGet), |
| 1389 |
attribute.String("http.scheme", "https"), |
| 1390 |
attribute.String("http.flavor", "1.0"), |
| 1391 |
attribute.String("http.server_name", "my-server-name"), |
| 1392 |
attribute.String("http.host", "example.com"), |
| 1393 |
}, |
| 1394 |
}, |
| 1395 |
{ |
| 1396 |
name: "with proxy info", |
| 1397 |
serverName: "my-server-name", |
| 1398 |
method: http.MethodGet, |
| 1399 |
requestURI: "/user/123", |
| 1400 |
proto: "HTTP/1.0", |
| 1401 |
remoteAddr: "", |
| 1402 |
host: "example.com", |
| 1403 |
url: &url.URL{ |
| 1404 |
Path: "/user/123", |
| 1405 |
}, |
| 1406 |
header: http.Header{ |
| 1407 |
"User-Agent": []string{"foodownloader"}, |
| 1408 |
"X-Forwarded-For": []string{"203.0.113.195, 70.41.3.18, 150.172.238.178"}, |
| 1409 |
}, |
| 1410 |
tls: withTLS, |
| 1411 |
expected: []attribute.KeyValue{ |
| 1412 |
attribute.String("http.method", http.MethodGet), |
| 1413 |
attribute.String("http.scheme", "https"), |
| 1414 |
attribute.String("http.flavor", "1.0"), |
| 1415 |
attribute.String("http.server_name", "my-server-name"), |
| 1416 |
attribute.String("http.host", "example.com"), |
| 1417 |
}, |
| 1418 |
}, |
| 1419 |
{ |
| 1420 |
name: "with http 1.1", |
| 1421 |
serverName: "my-server-name", |
| 1422 |
method: http.MethodGet, |
| 1423 |
requestURI: "/user/123", |
| 1424 |
proto: "HTTP/1.1", |
| 1425 |
remoteAddr: "", |
| 1426 |
host: "example.com", |
| 1427 |
url: &url.URL{ |
| 1428 |
Path: "/user/123", |
| 1429 |
}, |
| 1430 |
header: http.Header{ |
| 1431 |
"User-Agent": []string{"foodownloader"}, |
| 1432 |
"X-Forwarded-For": []string{"1.2.3.4"}, |
| 1433 |
}, |
| 1434 |
tls: withTLS, |
| 1435 |
expected: []attribute.KeyValue{ |
| 1436 |
attribute.String("http.method", http.MethodGet), |
| 1437 |
attribute.String("http.scheme", "https"), |
| 1438 |
attribute.String("http.flavor", "1.1"), |
| 1439 |
attribute.String("http.server_name", "my-server-name"), |
| 1440 |
attribute.String("http.host", "example.com"), |
| 1441 |
}, |
| 1442 |
}, |
| 1443 |
{ |
| 1444 |
name: "with http 2", |
| 1445 |
serverName: "my-server-name", |
| 1446 |
method: http.MethodGet, |
| 1447 |
requestURI: "/user/123", |
| 1448 |
proto: "HTTP/2.0", |
| 1449 |
remoteAddr: "", |
| 1450 |
host: "example.com", |
| 1451 |
url: &url.URL{ |
| 1452 |
Path: "/user/123", |
| 1453 |
}, |
| 1454 |
header: http.Header{ |
| 1455 |
"User-Agent": []string{"foodownloader"}, |
| 1456 |
"X-Forwarded-For": []string{"1.2.3.4"}, |
| 1457 |
}, |
| 1458 |
tls: withTLS, |
| 1459 |
expected: []attribute.KeyValue{ |
| 1460 |
attribute.String("http.method", http.MethodGet), |
| 1461 |
attribute.String("http.scheme", "https"), |
| 1462 |
attribute.String("http.flavor", "2"), |
| 1463 |
attribute.String("http.server_name", "my-server-name"), |
| 1464 |
attribute.String("http.host", "example.com"), |
| 1465 |
}, |
| 1466 |
}, |
| 1467 |
} |
| 1468 |
for idx, tc := range testcases { |
| 1469 |
r := testRequest(tc.method, tc.requestURI, tc.proto, tc.remoteAddr, tc.host, tc.url, tc.header, tc.tls) |
| 1470 |
r.ContentLength = tc.contentLength |
| 1471 |
got := sc.HTTPServerMetricAttributesFromHTTPRequest(tc.serverName, r) |
| 1472 |
assertElementsMatch(t, tc.expected, got, "testcase %d - %s", idx, tc.name) |
| 1473 |
} |
| 1474 |
} |
| 1475 |
|
| 1476 |
func TestHttpBasicAttributesFromHTTPRequest(t *testing.T) { |
| 1477 |
type testcase struct { |
| 1478 |
name string |
| 1479 |
method string |
| 1480 |
requestURI string |
| 1481 |
proto string |
| 1482 |
remoteAddr string |
| 1483 |
host string |
| 1484 |
url *url.URL |
| 1485 |
header http.Header |
| 1486 |
tls tlsOption |
| 1487 |
contentLength int64 |
| 1488 |
expected []attribute.KeyValue |
| 1489 |
} |
| 1490 |
testcases := []testcase{ |
| 1491 |
{ |
| 1492 |
name: "stripped", |
| 1493 |
method: http.MethodGet, |
| 1494 |
requestURI: "/user/123", |
| 1495 |
proto: "HTTP/1.0", |
| 1496 |
remoteAddr: "", |
| 1497 |
host: "example.com", |
| 1498 |
url: &url.URL{ |
| 1499 |
Path: "/user/123", |
| 1500 |
}, |
| 1501 |
header: nil, |
| 1502 |
tls: noTLS, |
| 1503 |
expected: []attribute.KeyValue{ |
| 1504 |
attribute.String("http.method", http.MethodGet), |
| 1505 |
attribute.String("http.scheme", "http"), |
| 1506 |
attribute.String("http.flavor", "1.0"), |
| 1507 |
attribute.String("http.host", "example.com"), |
| 1508 |
}, |
| 1509 |
}, |
| 1510 |
} |
| 1511 |
for idx, tc := range testcases { |
| 1512 |
r := testRequest(tc.method, tc.requestURI, tc.proto, tc.remoteAddr, tc.host, tc.url, tc.header, tc.tls) |
| 1513 |
r.ContentLength = tc.contentLength |
| 1514 |
got := sc.httpBasicAttributesFromHTTPRequest(r) |
| 1515 |
assertElementsMatch(t, tc.expected, got, "testcase %d - %s", idx, tc.name) |
| 1516 |
} |
| 1517 |
} |