Skip to content

Commit be1c632

Browse files
authored
Merge pull request #131 from slashmo/feature/otel-grpc-attributes
Add (g)RPC semantic attributes
2 parents d57e613 + b4f8eba commit be1c632

File tree

2 files changed

+139
-0
lines changed

2 files changed

+139
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
extension SpanAttributeName {
17+
/// - See: GRPCAttributes
18+
public enum GRPC {
19+
/// - See: GRPCAttributes
20+
public static let messageType = "message.type"
21+
/// - See: GRPCAttributes
22+
public static let messageID = "message.id"
23+
/// - See: GRPCAttributes
24+
public static let messageCompressedSize = "message.compressed_size"
25+
/// - See: GRPCAttributes
26+
public static let messageUncompressedSize = "message.uncompressed_size"
27+
}
28+
}
29+
30+
#if swift(>=5.2)
31+
extension SpanAttributes {
32+
/// Semantic conventions for gRPC spans.
33+
public var gRPC: GRPCAttributes {
34+
get {
35+
.init(attributes: self)
36+
}
37+
set {
38+
self = newValue.attributes
39+
}
40+
}
41+
}
42+
43+
/// Semantic conventions for gRPC spans as defined in the OpenTelemetry spec.
44+
///
45+
/// - SeeAlso: [OpenTelemetry: Semantic conventions for gRPC spans](https://github.com/open-telemetry/opentelemetry-specification/blob/master/specification/trace/semantic_conventions/rpc.md#grpc) (as of August 2020)
46+
@dynamicMemberLookup
47+
public struct GRPCAttributes: SpanAttributeNamespace {
48+
public var attributes: SpanAttributes
49+
50+
public init(attributes: SpanAttributes) {
51+
self.attributes = attributes
52+
}
53+
54+
public struct NestedAttributes: NestedSpanAttributesProtocol {
55+
public init() {}
56+
57+
/// The type of message, e.g. "SENT" or "RECEIVED".
58+
public var messageType: SpanAttributeKey<String> { .init(name: SpanAttributeName.GRPC.messageType) }
59+
60+
/// The message id calculated as two different counters starting from 1, one for sent messages and one for received messages.
61+
public var messageID: SpanAttributeKey<Int> { .init(name: SpanAttributeName.GRPC.messageID) }
62+
63+
/// The compressed message size in bytes.
64+
public var messageCompressedSize: SpanAttributeKey<Int> {
65+
.init(name: SpanAttributeName.GRPC.messageCompressedSize)
66+
}
67+
68+
/// The uncompressed message size in bytes.
69+
public var messageUncompressedSize: SpanAttributeKey<Int> {
70+
.init(name: SpanAttributeName.GRPC.messageUncompressedSize)
71+
}
72+
}
73+
}
74+
#endif
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
extension SpanAttributeName {
17+
/// - See: RPCAttributes
18+
public enum RPC {
19+
/// - See: RPCAttributes
20+
public static let system = "rpc.system"
21+
/// - See: RPCAttributes
22+
public static let service = "rpc.service"
23+
/// - See: RPCAttributes
24+
public static let method = "rpc.method"
25+
}
26+
}
27+
28+
#if swift(>=5.2)
29+
extension SpanAttributes {
30+
/// Semantic conventions for RPC spans.
31+
public var rpc: RPCAttributes {
32+
get {
33+
.init(attributes: self)
34+
}
35+
set {
36+
self = newValue.attributes
37+
}
38+
}
39+
}
40+
41+
/// Semantic conventions for RPC spans as defined in the OpenTelemetry spec.
42+
///
43+
/// - SeeAlso: [OpenTelemetry: Semantic conventions for RPC spans](https://github.com/open-telemetry/opentelemetry-specification/blob/b70565d5a8a13d26c91fb692879dc874d22c3ac8/specification/trace/semantic_conventions/rpc.md) (as of August 2020)
44+
@dynamicMemberLookup
45+
public struct RPCAttributes: SpanAttributeNamespace {
46+
public var attributes: SpanAttributes
47+
48+
public init(attributes: SpanAttributes) {
49+
self.attributes = attributes
50+
}
51+
52+
public struct NestedAttributes: NestedSpanAttributesProtocol {
53+
public init() {}
54+
55+
/// A string identifying the remoting system, e.g., "grpc", "java_rmi" or "wcf".
56+
public var system: SpanAttributeKey<String> { .init(name: SpanAttributeName.RPC.system) }
57+
58+
/// The full name of the service being called, including its package name, if applicable.
59+
public var service: SpanAttributeKey<String> { .init(name: SpanAttributeName.RPC.service) }
60+
61+
/// The name of the method being called, must be equal to the $method part in the span name.
62+
public var method: SpanAttributeKey<String> { .init(name: SpanAttributeName.RPC.method) }
63+
}
64+
}
65+
#endif

0 commit comments

Comments
 (0)