Skip to content

Commit 114fd64

Browse files
authored
Merge pull request #133 from slashmo/feature/swift-5-attribute-constants
Add SpanAttributeName String constants
2 parents bcbeb3b + 21f3e2f commit 114fd64

File tree

5 files changed

+126
-31
lines changed

5 files changed

+126
-31
lines changed

Sources/OpenTelemetryInstrumentationSupport/SpanAttribute+EndUser.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,18 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#if swift(>=5.2)
1514
import TracingInstrumentation
1615

16+
extension SpanAttributeName {
17+
/// - See: EndUserAttributes
18+
public static let endUserID = "enduser.id"
19+
/// - See: EndUserAttributes
20+
public static let endUserRole = "enduser.role"
21+
/// - See: EndUserAttributes
22+
public static let endUserScope = "enduser.scope"
23+
}
24+
25+
#if swift(>=5.2)
1726
extension SpanAttributes {
1827
/// Semantic end-user attributes.
1928
public var endUser: EndUserAttributes {
@@ -27,6 +36,8 @@ extension SpanAttributes {
2736
}
2837

2938
/// End-user-related semantic conventions as defined in the OpenTelemetry spec.
39+
///
40+
/// - SeeAlso: [OpenTelemetry: General identity attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/b70565d5a8a13d26c91fb692879dc874d22c3ac8/specification/trace/semantic_conventions/span-general.md#general-identity-attributes) (as of August 2020)
3041
@dynamicMemberLookup
3142
public struct EndUserAttributes: SpanAttributeNamespace {
3243
public var attributes: SpanAttributes
@@ -39,14 +50,14 @@ public struct EndUserAttributes: SpanAttributeNamespace {
3950
public init() {}
4051

4152
/// Username or client_id extracted from the access token or Authorization header in the inbound request from outside the system.
42-
public var id: SpanAttributeKey<String> { "enduser.id" }
53+
public var id: SpanAttributeKey<String> { .init(name: SpanAttributeName.endUserID) }
4354

4455
/// Actual/assumed role the client is making the request under extracted from token or application security context.
45-
public var role: SpanAttributeKey<String> { "enduser.role" }
56+
public var role: SpanAttributeKey<String> { .init(name: SpanAttributeName.endUserRole) }
4657

4758
/// Scopes or granted authorities the client currently possesses extracted from token or application security context.
4859
/// The value would come from the scope associated with an OAuth 2.0 Access Token or an attribute value in a SAML 2.0 Assertion.
49-
public var scope: SpanAttributeKey<String> { "enduser.scope" }
60+
public var scope: SpanAttributeKey<String> { .init(name: SpanAttributeName.endUserScope) }
5061
}
5162
}
5263
#endif

Sources/OpenTelemetryInstrumentationSupport/SpanAttribute+HTTPSemantics.swift

Lines changed: 58 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,44 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#if swift(>=5.2)
1514
import TracingInstrumentation
1615

16+
extension SpanAttributeName {
17+
/// - See: HTTPAttributes
18+
public static let httpMethod = "http.method"
19+
/// - See: HTTPAttributes
20+
public static let httpURL = "http.url"
21+
/// - See: HTTPAttributes
22+
public static let httpTarget = "http.target"
23+
/// - See: HTTPAttributes
24+
public static let httpHost = "http.host"
25+
/// - See: HTTPAttributes
26+
public static let httpScheme = "http.scheme"
27+
/// - See: HTTPAttributes
28+
public static let httpStatusCode = "http.status_code"
29+
/// - See: HTTPAttributes
30+
public static let httpStatusText = "http.status_text"
31+
/// - See: HTTPAttributes
32+
public static let httpFlavor = "http.flavor"
33+
/// - See: HTTPAttributes
34+
public static let httpUserAgent = "http.user_agent"
35+
/// - See: HTTPAttributes
36+
public static let httpRequestContentLength = "http.request_content_length"
37+
/// - See: HTTPAttributes
38+
public static let httpRequestContentLengthUncompressed = "http.request_content_length_uncompressed"
39+
/// - See: HTTPAttributes
40+
public static let httpResponseContentLength = "http.response_content_length"
41+
/// - See: HTTPAttributes
42+
public static let httpResponseContentLengthUncompressed = "http.response_content_length_uncompressed"
43+
/// - See: HTTPAttributes
44+
public static let httpServerName = "http.server_name"
45+
/// - See: HTTPAttributes
46+
public static let httpServerRoute = "http.route"
47+
/// - See: HTTPAttributes
48+
public static let httpServerClientIP = "http.client_ip"
49+
}
50+
51+
#if swift(>=5.2)
1752
extension SpanAttributes {
1853
/// Semantic conventions for HTTP spans.
1954
public var http: HTTPAttributes {
@@ -27,6 +62,8 @@ extension SpanAttributes {
2762
}
2863

2964
/// Semantic conventions for HTTP spans as defined in the OpenTelemetry spec.
65+
///
66+
/// - SeeAlso: [OpenTelemetry: Semantic conventions for HTTP spans](https://github.com/open-telemetry/opentelemetry-specification/blob/b70565d5a8a13d26c91fb692879dc874d22c3ac8/specification/trace/semantic_conventions/http.md) (as of August 2020)
3067
@dynamicMemberLookup
3168
public struct HTTPAttributes: SpanAttributeNamespace {
3269
public var attributes: SpanAttributes
@@ -39,64 +76,68 @@ public struct HTTPAttributes: SpanAttributeNamespace {
3976
public init() {}
4077

4178
/// HTTP request method. E.g. "GET".
42-
public var method: SpanAttributeKey<String> { "http.method" }
79+
public var method: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpMethod) }
4380

4481
/// Full HTTP request URL in the form scheme://host[:port]/path?query[#fragment].
4582
/// Usually the fragment is not transmitted over HTTP, but if it is known, it should be included nevertheless.
46-
public var url: SpanAttributeKey<String> { "http.url" }
83+
public var url: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpURL) }
4784

4885
/// The full request target as passed in a HTTP request line or equivalent, e.g. "/path/12314/?q=ddds#123".
49-
public var target: SpanAttributeKey<String> { "http.target" }
86+
public var target: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpTarget) }
5087

5188
/// The value of the HTTP host header. When the header is empty or not present, this attribute should be the same.
52-
public var host: SpanAttributeKey<String> { "http.host" }
89+
public var host: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpHost) }
5390

5491
/// The URI scheme identifying the used protocol: "http" or "https"
55-
public var scheme: SpanAttributeKey<String> { "http.scheme" }
92+
public var scheme: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpScheme) }
5693

5794
/// HTTP response status code. E.g. 200.
58-
public var statusCode: SpanAttributeKey<Int> { "http.status_code" }
95+
public var statusCode: SpanAttributeKey<Int> { .init(name: SpanAttributeName.httpStatusCode) }
5996

6097
/// HTTP reason phrase. E.g. "OK".
61-
public var statusText: SpanAttributeKey<String> { "http.status_text" }
98+
public var statusText: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpStatusText) }
6299

63100
/// Kind of HTTP protocol used: "1.0", "1.1", "2", "SPDY" or "QUIC".
64-
public var flavor: SpanAttributeKey<String> { "http.flavor" }
101+
public var flavor: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpFlavor) }
65102

66103
/// Value of the HTTP User-Agent header sent by the client.
67-
public var userAgent: SpanAttributeKey<String> { "http.user_agent" }
104+
public var userAgent: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpUserAgent) }
68105

69106
/// The size of the request payload body in bytes. This is the number of bytes transferred excluding headers and is often,
70107
/// but not always, present as the Content-Length header. For requests using transport encoding, this should be the
71108
/// compressed size.
72-
public var requestContentLength: SpanAttributeKey<Int> { "http.request_content_length" }
109+
public var requestContentLength: SpanAttributeKey<Int> {
110+
.init(name: SpanAttributeName.httpRequestContentLength)
111+
}
73112

74113
/// The size of the uncompressed request payload body after transport decoding. Not set if transport encoding not used.
75114
public var requestContentLengthUncompressed: SpanAttributeKey<Int> {
76-
"http.request_content_length_uncompressed"
115+
.init(name: SpanAttributeName.httpRequestContentLengthUncompressed)
77116
}
78117

79118
/// The size of the response payload body in bytes. This is the number of bytes transferred excluding headers and
80119
/// is often, but not always, present as the Content-Length header. For requests using transport encoding, this
81120
/// should be the compressed size.
82-
public var responseContentLength: SpanAttributeKey<Int> { "http.response_content_length" }
121+
public var responseContentLength: SpanAttributeKey<Int> {
122+
.init(name: SpanAttributeName.httpResponseContentLength)
123+
}
83124

84125
/// The size of the uncompressed response payload body after transport decoding. Not set if transport encoding not used.
85126
public var responseContentLengthUncompressed: SpanAttributeKey<Int> {
86-
"http.response_content_length_uncompressed"
127+
.init(name: SpanAttributeName.httpResponseContentLengthUncompressed)
87128
}
88129

89130
/// The primary server name of the matched virtual host. This should be obtained via configuration.
90131
/// If no such configuration can be obtained, this attribute MUST NOT be set (`net.hostName` should be used instead).
91-
public var serverName: SpanAttributeKey<String> { "http.server_name" }
132+
public var serverName: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpServerName) }
92133

93134
/// The matched route (path template). E.g. "/users/:userID?".
94-
public var serverRoute: SpanAttributeKey<String> { "http.route" }
135+
public var serverRoute: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpServerRoute) }
95136

96137
/// The IP address of the original client behind all proxies, if known (e.g. from X-Forwarded-For).
97138
/// Note that this is not necessarily the same as `net.peerIP`, which would identify the network-level peer,
98139
/// which may be a proxy.
99-
public var serverClientIP: SpanAttributeKey<String> { "http.client_ip" }
140+
public var serverClientIP: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpServerClientIP) }
100141
}
101142
}
102143
#endif

Sources/OpenTelemetryInstrumentationSupport/SpanAttribute+NetSemantics.swift

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,26 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#if swift(>=5.2)
1514
import TracingInstrumentation
1615

16+
extension SpanAttributeName {
17+
/// - See: NetAttributes
18+
public static let netTransport = "net.transport"
19+
/// - See: NetAttributes
20+
public static let netPeerIP = "net.peer.ip"
21+
/// - See: NetAttributes
22+
public static let netPeerPort = "net.peer.port"
23+
/// - See: NetAttributes
24+
public static let netPeerName = "net.peer.name"
25+
/// - See: NetAttributes
26+
public static let netHostIP = "net.host.ip"
27+
/// - See: NetAttributes
28+
public static let netHostPort = "net.host.port"
29+
/// - See: NetAttributes
30+
public static let netHostName = "net.host.name"
31+
}
32+
33+
#if swift(>=5.2)
1734
extension SpanAttributes {
1835
/// Semantic network attributes.
1936
public var net: NetAttributes {
@@ -27,6 +44,8 @@ extension SpanAttributes {
2744
}
2845

2946
/// Network-related semantic conventions as defined in the OpenTelemetry spec.
47+
///
48+
/// - SeeAlso: [OpenTelemetry: General semantic attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/b70565d5a8a13d26c91fb692879dc874d22c3ac8/specification/trace/semantic_conventions/span-general.md) (as of August 2020)
3049
@dynamicMemberLookup
3150
public struct NetAttributes: SpanAttributeNamespace {
3251
public var attributes: SpanAttributes
@@ -39,25 +58,25 @@ public struct NetAttributes: SpanAttributeNamespace {
3958
public init() {}
4059

4160
/// Transport protocol used.
42-
public var transport: SpanAttributeKey<String> { "net.transport" }
61+
public var transport: SpanAttributeKey<String> { .init(name: SpanAttributeName.netTransport) }
4362

4463
/// Remote address of the peer (dotted decimal for IPv4 or RFC5952 for IPv6).
45-
public var peerIP: SpanAttributeKey<String> { "net.peer.ip" }
64+
public var peerIP: SpanAttributeKey<String> { .init(name: SpanAttributeName.netPeerIP) }
4665

4766
/// Remote port number as an integer. E.g., 80.
48-
public var peerPort: SpanAttributeKey<Int> { "net.peer.port" }
67+
public var peerPort: SpanAttributeKey<Int> { .init(name: SpanAttributeName.netPeerPort) }
4968

5069
/// Remote hostname or similar.
51-
public var peerName: SpanAttributeKey<String> { "net.peer.name" }
70+
public var peerName: SpanAttributeKey<String> { .init(name: SpanAttributeName.netPeerName) }
5271

5372
/// Like `peerIP` but for the host IP. Useful in case of a multi-IP host.
54-
public var hostIP: SpanAttributeKey<String> { "net.host.ip" }
73+
public var hostIP: SpanAttributeKey<String> { .init(name: SpanAttributeName.netHostIP) }
5574

5675
/// Like `peerPort` but for the host port.
57-
public var hostPort: SpanAttributeKey<Int> { "net.host.port" }
76+
public var hostPort: SpanAttributeKey<Int> { .init(name: SpanAttributeName.netHostPort) }
5877

5978
/// Local hostname or similar.
60-
public var hostName: SpanAttributeKey<String> { "net.host.name" }
79+
public var hostName: SpanAttributeKey<String> { .init(name: SpanAttributeName.netHostName) }
6180
}
6281
}
6382
#endif

Sources/OpenTelemetryInstrumentationSupport/SpanAttribute+PeerSemantics.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,14 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14-
#if swift(>=5.2)
1514
import TracingInstrumentation
1615

16+
extension SpanAttributeName {
17+
/// - See: PeerAttributes
18+
public static let peerService = "peer.service"
19+
}
20+
21+
#if swift(>=5.2)
1722
extension SpanAttributes {
1823
/// General semantic attributes.
1924
public var peer: PeerAttributes {
@@ -27,6 +32,8 @@ extension SpanAttributes {
2732
}
2833

2934
/// Peer-related semantic conventions as defined in the OpenTelemetry spec.
35+
///
36+
/// - SeeAlso: [OpenTelemetry: General remote service attributes](https://github.com/open-telemetry/opentelemetry-specification/blob/b70565d5a8a13d26c91fb692879dc874d22c3ac8/specification/trace/semantic_conventions/span-general.md#general-remote-service-attributes) (as of August 2020)
3037
@dynamicMemberLookup
3138
public struct PeerAttributes: SpanAttributeNamespace {
3239
public var attributes: SpanAttributes
@@ -39,7 +46,7 @@ public struct PeerAttributes: SpanAttributeNamespace {
3946
public init() {}
4047

4148
/// The service.name of the remote service. SHOULD be equal to the actual service.name resource attribute of the remote service if any.
42-
public var service: SpanAttributeKey<String> { "peer.service" }
49+
public var service: SpanAttributeKey<String> { .init(name: SpanAttributeName.peerService) }
4350
}
4451
}
4552
#endif
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift Distributed Tracing open source project
4+
//
5+
// Copyright (c) 2020 Moritz Lang and the Swift Tracing project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
import TracingInstrumentation
15+
16+
/// Namespace for attribute key constants used with `SpanAttributes`.
17+
public enum SpanAttributeName {}

0 commit comments

Comments
 (0)