Skip to content

Commit ab12067

Browse files
committed
Add gRPC message semantic attributes
1 parent c3e589f commit ab12067

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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 static let gRPCMessageType = "message.type"
19+
/// - See: GRPCAttributes
20+
public static let gRPCMessageID = "message.id"
21+
/// - See: GRPCAttributes
22+
public static let gRPCMessageCompressedSize = "message.compressed_size"
23+
/// - See: GRPCAttributes
24+
public static let gRPCMessageUncompressedSize = "message.uncompressed_size"
25+
}
26+
27+
#if swift(>=5.2)
28+
extension SpanAttributes {
29+
/// Semantic conventions for gRPC spans.
30+
public var gRPC: GRPCAttributes {
31+
get {
32+
.init(attributes: self)
33+
}
34+
set {
35+
self = newValue.attributes
36+
}
37+
}
38+
}
39+
40+
/// Semantic conventions for gRPC spans as defined in the OpenTelemetry spec.
41+
///
42+
/// - 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)
43+
@dynamicMemberLookup
44+
public struct GRPCAttributes: SpanAttributeNamespace {
45+
public var attributes: SpanAttributes
46+
47+
public init(attributes: SpanAttributes) {
48+
self.attributes = attributes
49+
}
50+
51+
public struct NestedAttributes: NestedSpanAttributesProtocol {
52+
public init() {}
53+
54+
/// The type of message, e.g. "SENT" or "RECEIVED".
55+
public var messageType: SpanAttributeKey<String> { .init(name: SpanAttributeName.gRPCMessageType) }
56+
57+
/// The message id calculated as two different counters starting from 1, one for sent messages and one for received messages.
58+
public var messageID: SpanAttributeKey<Int> { .init(name: SpanAttributeName.gRPCMessageID) }
59+
60+
/// The compressed message size in bytes.
61+
public var messageCompressedSize: SpanAttributeKey<Int> {
62+
.init(name: SpanAttributeName.gRPCMessageCompressedSize)
63+
}
64+
65+
/// The uncompressed message size in bytes.
66+
public var messageUncompressedSize: SpanAttributeKey<Int> {
67+
.init(name: SpanAttributeName.gRPCMessageUncompressedSize)
68+
}
69+
}
70+
}
71+
#endif

0 commit comments

Comments
 (0)