This repository was archived by the owner on Jul 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Add OTEL URL attributes #41
Closed
NeedleInAJayStack
wants to merge
2
commits into
apple:main
from
NeedleInAJayStack:feat/url-semantics
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
Sources/TracingOpenTelemetrySemanticConventions/SpanAttributes+URLSemantics.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,88 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift Distributed Tracing open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the Swift Distributed Tracing project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Swift Distributed Tracing project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Tracing | ||
|
||
extension SpanAttributes { | ||
/// URL attributes. | ||
/// | ||
/// OpenTelemetry Spec: [URL attributes](https://opentelemetry.io/docs/specs/semconv/attributes-registry/url/) | ||
public var url: URLAttributes { | ||
get { | ||
.init(attributes: self) | ||
} | ||
set { | ||
self = newValue.attributes | ||
} | ||
} | ||
} | ||
|
||
/// URL attributes. | ||
/// | ||
/// OpenTelemetry Spec: [URL attributes](https://opentelemetry.io/docs/specs/semconv/attributes-registry/url/) | ||
@dynamicMemberLookup | ||
public struct URLAttributes: SpanAttributeNamespace { | ||
public var attributes: SpanAttributes | ||
|
||
public init(attributes: SpanAttributes) { | ||
self.attributes = attributes | ||
} | ||
|
||
public struct NestedSpanAttributes: NestedSpanAttributesProtocol { | ||
public init() {} | ||
|
||
/// Domain extracted from the url.full, such as “opentelemetry.io”. | ||
public var domain: Key<String> { "url.domain" } | ||
|
||
/// The file extension extracted from the url.full, excluding the leading dot. | ||
public var `extension`: Key<String> { "url.extension" } | ||
|
||
/// The URI fragment component | ||
public var fragment: Key<String> { "url.fragment" } | ||
|
||
/// Absolute URL describing a network resource according to [RFC3986](https://www.rfc-editor.org/rfc/rfc3986) | ||
public var full: Key<String> { "url.full" } | ||
|
||
/// Unmodified original URL as seen in the event source | ||
public var original: Key<String> { "url.original" } | ||
|
||
/// The URI path component | ||
public var path: Key<String> { "url.path" } | ||
|
||
/// Port extracted from the url.full | ||
public var port: Key<Int> { "url.port" } | ||
|
||
/// The URI query component | ||
public var query: Key<String> { "url.query" } | ||
|
||
/// The highest registered url domain, stripped of the subdomain | ||
public var registeredDomain: Key<String> { "url.registered_domain" } | ||
|
||
/// The URI scheme component identifying the used protocol | ||
public var scheme: Key<String> { "url.scheme" } | ||
|
||
/// The subdomain portion of a fully qualified domain name includes all of the names except the | ||
/// host name under the registered\_domain. In a partially qualified domain, or if the qualification | ||
/// level of the full name cannot be determined, subdomain contains all of the names below the | ||
/// registered domain | ||
public var subdomain: Key<String> { "url.subdomain" } | ||
|
||
/// The low-cardinality template of an absolute path reference | ||
public var template: Key<String> { "url.template" } | ||
|
||
/// The effective top level domain (eTLD), also known as the domain suffix, is the last part of the | ||
/// domain name. For example, the top level domain for example.com is com | ||
public var topLevelDomain: Key<String> { "url.top_level_domain" } | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
Tests/TracingOpenTelemetrySemanticConventionsTests/URLSemanticsTests.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,53 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift Distributed Tracing open source project | ||
// | ||
// Copyright (c) 2023 Apple Inc. and the Swift Distributed Tracing project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of Swift Distributed Tracing project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Tracing | ||
import TracingOpenTelemetrySemanticConventions | ||
import XCTest | ||
|
||
final class URLSemanticsTests: XCTestCase { | ||
func test_peer() { | ||
var attributes = SpanAttributes() | ||
|
||
attributes.url.domain = "www.foo.bar" | ||
attributes.url.`extension` = "png" | ||
attributes.url.fragment = "SemConv" | ||
attributes.url.full = "https://www.foo.bar/search?q=OpenTelemetry#SemConv" | ||
attributes.url.original = "https://www.foo.bar/search?q=OpenTelemetry#SemConv" | ||
attributes.url.path = "/search" | ||
attributes.url.port = 443 | ||
attributes.url.query = "q=OpenTelemetry" | ||
attributes.url.registeredDomain = "example.com" | ||
attributes.url.scheme = "https" | ||
attributes.url.subdomain = "east" | ||
attributes.url.template = "/users/{id}" | ||
attributes.url.topLevelDomain = "co.uk" | ||
|
||
XCTAssertSpanAttributesEqual(attributes, [ | ||
"url.domain": "www.foo.bar", | ||
"url.extension": "png", | ||
"url.fragment": "SemConv", | ||
"url.full": "https://www.foo.bar/search?q=OpenTelemetry#SemConv", | ||
"url.original": "https://www.foo.bar/search?q=OpenTelemetry#SemConv", | ||
"url.path": "/search", | ||
"url.port": 443, | ||
"url.query": "q=OpenTelemetry", | ||
"url.registered_domain": "example.com", | ||
"url.scheme": "https", | ||
"url.subdomain": "east", | ||
"url.template": "/users/{id}", | ||
"url.top_level_domain": "co.uk", | ||
]) | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.