Skip to content

Commit 44a8f17

Browse files
Datadog exporter: Properly encode errors (#72)
Some of the error tags were not encoded correctly and were getting lost Also clean a compiler warning
1 parent 7ed84cb commit 44a8f17

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

Sources/Exporters/DatadogExporter/Spans/SpanEncoder.swift

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ internal struct DDSpan: Encodable {
6262
let resource: String
6363
let startTime: UInt64
6464
let duration: UInt64
65-
let isError: Bool
65+
let error: Bool
6666
let errorMessage: String?
6767
let errorType: String?
6868
let errorStack: String?
@@ -79,6 +79,10 @@ internal struct DDSpan: Encodable {
7979
/// Custom tags, received from user
8080
let tags: [String: String]
8181

82+
static let errorTagKeys: Set<String> = [
83+
"error.message", "error.type", "error.stack",
84+
]
85+
8286
func encode(to encoder: Encoder) throws {
8387
try SpanEncoder().encode(self, to: encoder)
8488
}
@@ -100,17 +104,17 @@ internal struct DDSpan: Encodable {
100104
self.duration = spanData.endEpochNanos - spanData.startEpochNanos
101105

102106
if spanData.attributes["error"] != nil {
103-
self.isError = true
104-
self.errorMessage = spanData.attributes["error.msg"]?.description
107+
self.error = true
108+
self.errorMessage = spanData.attributes["error.message"]?.description
105109
self.errorType = spanData.attributes["error.type"]?.description
106110
self.errorStack = spanData.attributes["error.stack"]?.description
107111
} else if !(spanData.status?.isOk ?? false) {
108-
self.isError = true
112+
self.error = true
109113
self.errorMessage = spanData.status?.description ?? "error"
110114
self.errorType = spanData.status?.description ?? "error"
111115
self.errorStack = nil
112116
} else {
113-
self.isError = false
117+
self.error = false
114118
self.errorMessage = nil
115119
self.errorType = nil
116120
self.errorStack = nil
@@ -121,7 +125,11 @@ internal struct DDSpan: Encodable {
121125

122126
self.tracerVersion = "1.0" // spanData.tracerVersion
123127
self.applicationVersion = "0.0.1" // spanData.applicationVersion
124-
self.tags = spanData.attributes.mapValues { $0.description }
128+
self.tags = spanData.attributes.filter {
129+
!DDSpan.errorTagKeys.contains($0.key)
130+
}.mapValues {
131+
$0.description
132+
}
125133
}
126134
}
127135

@@ -140,7 +148,10 @@ internal struct SpanEncoder {
140148
case type
141149
case start
142150
case duration
143-
case isError = "error"
151+
case error
152+
case errorMessage = "meta.error.message"
153+
case errorType = "meta.error.type"
154+
case errorStack = "meta.error.stack"
144155

145156
// MARK: - Metrics
146157

@@ -179,8 +190,14 @@ internal struct SpanEncoder {
179190
try container.encode(span.startTime, forKey: .start)
180191
try container.encode(span.duration, forKey: .duration)
181192

182-
let isError = span.isError ? 1 : 0
183-
try container.encode(isError, forKey: .isError)
193+
if span.error {
194+
try container.encode(1, forKey: .error)
195+
try container.encode(span.errorMessage, forKey: .errorMessage)
196+
try container.encode(span.errorType, forKey: .errorType)
197+
try container.encode(span.errorStack, forKey: .errorStack)
198+
} else {
199+
try container.encode(0, forKey: .error)
200+
}
184201

185202
try encodeDefaultMetrics(span, to: &container)
186203
try encodeDefaultMeta(span, to: &container)

Sources/OpenTelemetrySdk/Trace/Export/SimpleSpanProcessor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public struct SimpleSpanProcessor: SpanProcessor {
4141
}
4242

4343
public func forceFlush() {
44-
spanExporter.flush()
44+
_ = spanExporter.flush()
4545
}
4646

4747
/// Returns a new SimpleSpansProcessor that converts spans to proto and forwards them to

0 commit comments

Comments
 (0)