-
Notifications
You must be signed in to change notification settings - Fork 142
Add more span attributes (URL, network) #881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
candiun
wants to merge
14
commits into
swift-server:main
Choose a base branch
from
candiun:additional-span-attributes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
6dc5dfa
Adds some span attributes mentioned in https://github.com/swift-serve…
candiun f096a44
Add and use OTelSemanticConventions
candiun 52ee55b
Add tests. Add redactions to query, path and headers.
candiun 16b4984
Remove unnecessary changes to DeconstructedURL, ConnectionTarget and …
candiun c580487
Remove unnecessary changes to DeconstructedURL, ConnectionTarget and …
candiun 25dfc39
Remove OTelSemanticConventions dependency
candiun 762f1c8
Group keys
candiun c64e162
Change complexity
candiun fbb95a4
Merge remote-tracking branch 'origin/main' into additional-span-attri…
candiun fe63763
Merge remote-tracking branch 'upstream/main' into additional-span-att…
candiun 4d287d8
Reduce complexity
candiun 26ff5f5
Swift Testing
candiun 8263ad1
Swift Testing
candiun 17ed657
Replace Foundation with FoundationEssentials
candiun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
212 changes: 212 additions & 0 deletions
212
Tests/AsyncHTTPClientTests/HTTPClientTracingAttributeTests.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,212 @@ | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This source file is part of the AsyncHTTPClient open source project | ||
| // | ||
| // Copyright (c) 2026 Apple Inc. and the AsyncHTTPClient project authors | ||
| // Licensed under Apache License v2.0 | ||
| // | ||
| // See LICENSE.txt for license information | ||
| // See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
| // | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| @_spi(Tracing) import AsyncHTTPClient // NOT @testable - tests that need @testable go into HTTPClientTracingInternalTests.swift | ||
| import InMemoryTracing | ||
| import NIOCore | ||
| import NIOHTTP1 | ||
| import Testing | ||
| import Tracing | ||
|
|
||
| @Suite("HTTPClient tracing attributes") | ||
| struct HTTPClientTracingAttributeTests { | ||
| @Test func traceAttributesURL() async throws { | ||
| let httpBin = HTTPBin() | ||
| defer { #expect(throws: Never.self) { try httpBin.shutdown() } } | ||
|
|
||
| let tracer = InMemoryTracer() | ||
| let httpClient = HTTPClient( | ||
| eventLoopGroupProvider: .singleton, | ||
| configuration: makeConfiguration(tracer: tracer) | ||
| ) | ||
| defer { #expect(throws: Never.self) { try httpClient.syncShutdown() } } | ||
|
|
||
| let request = makeRequest(url: httpBin.baseURL + "echo-method?foo=bar&Signature=secretSignature") | ||
| _ = try await httpClient.execute(request, deadline: .distantFuture) | ||
|
|
||
| #expect( | ||
| tracer.activeSpans.isEmpty, | ||
| "Still active spans which were not finished (\(tracer.activeSpans.count))! \(tracer.activeSpans)" | ||
| ) | ||
| let span = try #require(tracer.finishedSpans.first) | ||
| let keys = HTTPClient.TracingConfiguration.AttributeKeys() | ||
|
|
||
| #expect(span.attributes.get(keys.urlPath) == "/echo-method") | ||
| #expect(span.attributes.get(keys.urlScheme) == "http") | ||
| #expect(span.attributes.get(keys.urlQuery) == "foo=bar&Signature=REDACTED") | ||
| } | ||
|
|
||
| @Test func traceAttributesServer() async throws { | ||
| let httpBin = HTTPBin() | ||
| defer { #expect(throws: Never.self) { try httpBin.shutdown() } } | ||
|
|
||
| let tracer = InMemoryTracer() | ||
| let httpClient = HTTPClient( | ||
| eventLoopGroupProvider: .singleton, | ||
| configuration: makeConfiguration(tracer: tracer) | ||
| ) | ||
| defer { #expect(throws: Never.self) { try httpClient.syncShutdown() } } | ||
|
|
||
| let request = makeRequest(url: httpBin.baseURL + "echo-method?foo=bar&Signature=secretSignature") | ||
| _ = try await httpClient.execute(request, deadline: .distantFuture) | ||
|
|
||
| #expect( | ||
| tracer.activeSpans.isEmpty, | ||
| "Still active spans which were not finished (\(tracer.activeSpans.count))! \(tracer.activeSpans)" | ||
| ) | ||
| let span = try #require(tracer.finishedSpans.first) | ||
| let defaultHTTPBinPort = try #require(httpBin.socketAddress.port) | ||
| let defaultHTTPBinAddress = try #require(httpBin.socketAddress.ipAddress) | ||
| let keys = HTTPClient.TracingConfiguration.AttributeKeys() | ||
|
|
||
| #expect(span.attributes.get(keys.serverHostname) == .string(defaultHTTPBinAddress.description)) | ||
| #expect(span.attributes.get(keys.serverPort) == .int64(Int64(defaultHTTPBinPort))) | ||
| } | ||
|
|
||
| @Test func traceAttributesHTTP() async throws { | ||
| let httpBin = HTTPBin() | ||
| defer { #expect(throws: Never.self) { try httpBin.shutdown() } } | ||
|
|
||
| let tracer = InMemoryTracer() | ||
| var configuration = makeConfiguration(tracer: tracer) | ||
| configuration.tracing.allowedHeaders = ["Authorization"] | ||
| let httpClient = HTTPClient(eventLoopGroupProvider: .singleton, configuration: configuration) | ||
| defer { #expect(throws: Never.self) { try httpClient.syncShutdown() } } | ||
|
|
||
| let request = makeRequest(url: httpBin.baseURL + "echo-method?foo=bar&Signature=secretSignature") | ||
| _ = try await httpClient.execute(request, deadline: .distantFuture) | ||
|
|
||
| #expect( | ||
| tracer.activeSpans.isEmpty, | ||
| "Still active spans which were not finished (\(tracer.activeSpans.count))! \(tracer.activeSpans)" | ||
| ) | ||
| let span = try #require(tracer.finishedSpans.first) | ||
| let keys = HTTPClient.TracingConfiguration.AttributeKeys() | ||
|
|
||
| #expect(span.attributes.get(keys.requestMethod) == "GET") | ||
| #expect(span.attributes.get("\(keys.requestHeader).authorization") == .stringArray(["Bearer secret"])) | ||
| #expect(span.attributes.get("\(keys.requestHeader).password") == nil) | ||
| #expect(span.attributes.get(keys.responseStatusCode) == 200) | ||
| } | ||
|
|
||
| @Test func traceAttributesPathRedaction() async throws { | ||
| let httpBin = HTTPBin() | ||
| defer { #expect(throws: Never.self) { try httpBin.shutdown() } } | ||
|
|
||
| let tracer = InMemoryTracer() | ||
| var configuration = makeConfiguration(tracer: tracer) | ||
| configuration.tracing.sensitivePathComponents = ["nested-path"] | ||
| let httpClient = HTTPClient(eventLoopGroupProvider: .singleton, configuration: configuration) | ||
| defer { #expect(throws: Never.self) { try httpClient.syncShutdown() } } | ||
|
|
||
| let request = makeRequest(url: httpBin.baseURL + "echo-method/nested-path") | ||
| _ = try await httpClient.execute(request, deadline: .distantFuture) | ||
|
|
||
| #expect( | ||
| tracer.activeSpans.isEmpty, | ||
| "Still active spans which were not finished (\(tracer.activeSpans.count))! \(tracer.activeSpans)" | ||
| ) | ||
| let span = try #require(tracer.finishedSpans.first) | ||
| let keys = HTTPClient.TracingConfiguration.AttributeKeys() | ||
|
|
||
| #expect(span.attributes.get(keys.urlPath) == "/echo-method/REDACTED") | ||
| } | ||
|
|
||
| @Test func traceAttributesQueryRedaction() async throws { | ||
| let httpBin = HTTPBin() | ||
| defer { #expect(throws: Never.self) { try httpBin.shutdown() } } | ||
|
|
||
| let tracer = InMemoryTracer() | ||
| var configuration = makeConfiguration(tracer: tracer) | ||
| configuration.tracing.sensitiveQueryComponents.insert("foo") | ||
| let httpClient = HTTPClient(eventLoopGroupProvider: .singleton, configuration: configuration) | ||
| defer { #expect(throws: Never.self) { try httpClient.syncShutdown() } } | ||
|
|
||
| let request = makeRequest(url: httpBin.baseURL + "echo-method?foo=bar&Signature=secretSignature&bar=bar") | ||
| _ = try await httpClient.execute(request, deadline: .distantFuture) | ||
|
|
||
| #expect( | ||
| tracer.activeSpans.isEmpty, | ||
| "Still active spans which were not finished (\(tracer.activeSpans.count))! \(tracer.activeSpans)" | ||
| ) | ||
| let span = try #require(tracer.finishedSpans.first) | ||
| let keys = HTTPClient.TracingConfiguration.AttributeKeys() | ||
|
|
||
| #expect(span.attributes.get(keys.urlQuery) == "foo=REDACTED&Signature=REDACTED&bar=bar") | ||
| } | ||
|
|
||
| @Test func traceAttributesHTTPHeadersDisallowedByDefault() async throws { | ||
| let httpBin = HTTPBin() | ||
| defer { #expect(throws: Never.self) { try httpBin.shutdown() } } | ||
|
|
||
| let tracer = InMemoryTracer() | ||
| let httpClient = HTTPClient( | ||
| eventLoopGroupProvider: .singleton, | ||
| configuration: makeConfiguration(tracer: tracer) | ||
| ) | ||
| defer { #expect(throws: Never.self) { try httpClient.syncShutdown() } } | ||
|
|
||
| let request = makeRequest(url: httpBin.baseURL + "echo-method?foo=bar&Signature=secretSignature") | ||
| _ = try await httpClient.execute(request, deadline: .distantFuture) | ||
|
|
||
| #expect( | ||
| tracer.activeSpans.isEmpty, | ||
| "Still active spans which were not finished (\(tracer.activeSpans.count))! \(tracer.activeSpans)" | ||
| ) | ||
| let span = try #require(tracer.finishedSpans.first) | ||
|
|
||
| #expect(span.operationName == "GET") | ||
| #expect(span.attributes.get("http.request.header.authorization") == nil) | ||
| #expect(span.attributes.get("http.request.header.password") == nil) | ||
| } | ||
|
|
||
| @Test func traceAttributesHTTPHeaders() async throws { | ||
| let httpBin = HTTPBin() | ||
| defer { #expect(throws: Never.self) { try httpBin.shutdown() } } | ||
|
|
||
| let tracer = InMemoryTracer() | ||
| var configuration = makeConfiguration(tracer: tracer) | ||
| configuration.tracing.allowedHeaders = ["Authorization", "Password", "X-Method-Used"] | ||
| let httpClient = HTTPClient(eventLoopGroupProvider: .singleton, configuration: configuration) | ||
| defer { #expect(throws: Never.self) { try httpClient.syncShutdown() } } | ||
|
|
||
| let request = makeRequest(url: httpBin.baseURL + "echo-method?foo=bar&Signature=secretSignature") | ||
| _ = try await httpClient.execute(request, deadline: .distantFuture) | ||
|
|
||
| #expect( | ||
| tracer.activeSpans.isEmpty, | ||
| "Still active spans which were not finished (\(tracer.activeSpans.count))! \(tracer.activeSpans)" | ||
| ) | ||
| let span = try #require(tracer.finishedSpans.first) | ||
|
|
||
| #expect(span.operationName == "GET") | ||
| #expect(span.attributes.get("http.request.header.authorization") == .stringArray(["Bearer secret"])) | ||
| #expect(span.attributes.get("http.request.header.password") == .stringArray(["SuperSecretPassword"])) | ||
| #expect(span.attributes.get("http.response.header.x_method_used") == .stringArray(["GET"])) | ||
| } | ||
|
|
||
| private func makeConfiguration(tracer: InMemoryTracer) -> HTTPClient.Configuration { | ||
| var configuration = HTTPClient.Configuration() | ||
| configuration.httpVersion = .automatic | ||
| configuration.tracing.tracer = tracer | ||
| return configuration | ||
| } | ||
|
|
||
| private func makeRequest(url: String) -> HTTPClientRequest { | ||
| var request = HTTPClientRequest(url: url) | ||
| request.headers.add(name: "Authorization", value: "Bearer secret") | ||
| request.headers.add(name: "Password", value: "SuperSecretPassword") | ||
| return request | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Allowed headers is one way, though I wonder if it should be "redacted headers" instead, similar to the path and query items below.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would work well when it comes to request headers. But I'm thinking if for the cases like the response headers, where we're sometimes communicating with an external services that we have no control over, whether this wouldn't lead to leaking some secrets of custom names, either at the current time, or in case that service evolves and starts sending other headers in response. Maybe in that case ignoring everything unless explicitly allowed would be safer, what do you think?