Skip to content

Commit ced7ffc

Browse files
committed
Add SpanAttributeName String constants
1 parent bcbeb3b commit ced7ffc

File tree

5 files changed

+91
-31
lines changed

5 files changed

+91
-31
lines changed

Sources/OpenTelemetryInstrumentationSupport/SpanAttribute+EndUser.swift

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

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

16+
extension SpanAttributeName {
17+
public static let endUserID = "enduser.id"
18+
public static let endUserRole = "enduser.role"
19+
public static let endUserScope = "enduser.scope"
20+
}
21+
22+
#if swift(>=5.2)
1723
extension SpanAttributes {
1824
/// Semantic end-user attributes.
1925
public var endUser: EndUserAttributes {
@@ -39,14 +45,14 @@ public struct EndUserAttributes: SpanAttributeNamespace {
3945
public init() {}
4046

4147
/// 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" }
48+
public var id: SpanAttributeKey<String> { .init(name: SpanAttributeName.endUserID) }
4349

4450
/// 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" }
51+
public var role: SpanAttributeKey<String> { .init(name: SpanAttributeName.endUserRole) }
4652

4753
/// Scopes or granted authorities the client currently possesses extracted from token or application security context.
4854
/// 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" }
55+
public var scope: SpanAttributeKey<String> { .init(name: SpanAttributeName.endUserScope) }
5056
}
5157
}
5258
#endif

Sources/OpenTelemetryInstrumentationSupport/SpanAttribute+HTTPSemantics.swift

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

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

16+
extension SpanAttributeName {
17+
public static let httpMethod = "http.method"
18+
public static let httpURL = "http.url"
19+
public static let httpTarget = "http.target"
20+
public static let httpHost = "http.host"
21+
public static let httpScheme = "http.scheme"
22+
public static let httpStatusCode = "http.status_code"
23+
public static let httpStatusText = "http.status_text"
24+
public static let httpFlavor = "http.flavor"
25+
public static let httpUserAgent = "http.user_agent"
26+
public static let httpRequestContentLength = "http.request_content_length"
27+
public static let httpRequestContentLengthUncompressed = "http.request_content_length_uncompressed"
28+
public static let httpResponseContentLength = "http.response_content_length"
29+
public static let httpResponseContentLengthUncompressed = "http.response_content_length_uncompressed"
30+
public static let httpServerName = "http.server_name"
31+
public static let httpServerRoute = "http.route"
32+
public static let httpServerClientIP = "http.client_ip"
33+
}
34+
35+
#if swift(>=5.2)
1736
extension SpanAttributes {
1837
/// Semantic conventions for HTTP spans.
1938
public var http: HTTPAttributes {
@@ -39,64 +58,68 @@ public struct HTTPAttributes: SpanAttributeNamespace {
3958
public init() {}
4059

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

4463
/// Full HTTP request URL in the form scheme://host[:port]/path?query[#fragment].
4564
/// 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" }
65+
public var url: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpURL) }
4766

4867
/// 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" }
68+
public var target: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpTarget) }
5069

5170
/// 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" }
71+
public var host: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpHost) }
5372

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

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

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

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

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

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

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

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

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

89112
/// The primary server name of the matched virtual host. This should be obtained via configuration.
90113
/// 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" }
114+
public var serverName: SpanAttributeKey<String> { .init(name: SpanAttributeName.httpServerName) }
92115

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

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

Sources/OpenTelemetryInstrumentationSupport/SpanAttribute+NetSemantics.swift

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

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

16+
extension SpanAttributeName {
17+
public static let netTransport = "net.transport"
18+
public static let netPeerIP = "net.peer.ip"
19+
public static let netPeerPort = "net.peer.port"
20+
public static let netPeerName = "net.peer.name"
21+
public static let netHostIP = "net.host.ip"
22+
public static let netHostPort = "net.host.port"
23+
public static let netHostName = "net.host.name"
24+
}
25+
26+
#if swift(>=5.2)
1727
extension SpanAttributes {
1828
/// Semantic network attributes.
1929
public var net: NetAttributes {
@@ -39,25 +49,25 @@ public struct NetAttributes: SpanAttributeNamespace {
3949
public init() {}
4050

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

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

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

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

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

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

5969
/// Local hostname or similar.
60-
public var hostName: SpanAttributeKey<String> { "net.host.name" }
70+
public var hostName: SpanAttributeKey<String> { .init(name: SpanAttributeName.netHostName) }
6171
}
6272
}
6373
#endif

Sources/OpenTelemetryInstrumentationSupport/SpanAttribute+PeerSemantics.swift

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

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

16+
extension SpanAttributeName {
17+
public static let peerService = "peer.service"
18+
}
19+
20+
#if swift(>=5.2)
1721
extension SpanAttributes {
1822
/// General semantic attributes.
1923
public var peer: PeerAttributes {
@@ -39,7 +43,7 @@ public struct PeerAttributes: SpanAttributeNamespace {
3943
public init() {}
4044

4145
/// 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" }
46+
public var service: SpanAttributeKey<String> { .init(name: SpanAttributeName.peerService) }
4347
}
4448
}
4549
#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+
/// A set of `String` constants describing semantic names for `SpanAttributes`.
17+
public enum SpanAttributeName {}

0 commit comments

Comments
 (0)