Skip to content

Commit 2f06851

Browse files
authored
Merge pull request #499 from open-telemetry/spec-change
Expands LogRecord to allow "anyValue" in body parameter.
2 parents e1c6468 + 36f4dcf commit 2f06851

File tree

12 files changed

+103
-19
lines changed

12 files changed

+103
-19
lines changed

Sources/Exporters/OpenTelemetryProtocolCommon/common/CommonAdapter.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,49 @@ public struct CommonAdapter {
5454
return keyValue
5555
}
5656

57+
public static func toProtoAnyValue(attributeValue: AttributeValue) -> Opentelemetry_Proto_Common_V1_AnyValue {
58+
var anyValue = Opentelemetry_Proto_Common_V1_AnyValue()
59+
switch attributeValue {
60+
case let .string(value):
61+
anyValue.stringValue = value
62+
case let .bool(value):
63+
anyValue.boolValue = value
64+
case let .int(value):
65+
anyValue.intValue = Int64(value)
66+
case let .double(value):
67+
anyValue.doubleValue = value
68+
case let .stringArray(value):
69+
anyValue.arrayValue.values = value.map {
70+
var anyValue = Opentelemetry_Proto_Common_V1_AnyValue()
71+
anyValue.stringValue = $0
72+
return anyValue
73+
}
74+
case let .boolArray(value):
75+
anyValue.arrayValue.values = value.map {
76+
var anyValue = Opentelemetry_Proto_Common_V1_AnyValue()
77+
anyValue.boolValue = $0
78+
return anyValue
79+
}
80+
case let .intArray(value):
81+
anyValue.arrayValue.values = value.map {
82+
var anyValue = Opentelemetry_Proto_Common_V1_AnyValue()
83+
anyValue.intValue = Int64($0)
84+
return anyValue
85+
}
86+
case let .doubleArray(value):
87+
anyValue.arrayValue.values = value.map {
88+
var anyValue = Opentelemetry_Proto_Common_V1_AnyValue()
89+
anyValue.doubleValue = $0
90+
return anyValue
91+
}
92+
case let .set(value):
93+
anyValue.kvlistValue.values = value.labels.map({
94+
return toProtoAttribute(key: $0, attributeValue: $1)
95+
})
96+
}
97+
return anyValue
98+
}
99+
57100
public static func toProtoInstrumentationScope(instrumentationScopeInfo: InstrumentationScopeInfo)
58101
-> Opentelemetry_Proto_Common_V1_InstrumentationScope
59102
{

Sources/Exporters/OpenTelemetryProtocolCommon/logs/LogRecordAdapter.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,8 @@ public class LogRecordAdapter {
4545

4646
protoLogRecord.timeUnixNano = logRecord.timestamp.timeIntervalSince1970.toNanoseconds
4747

48-
if let body = logRecord.body, !body.isEmpty {
49-
var protoBody = Opentelemetry_Proto_Common_V1_AnyValue()
50-
protoBody.stringValue = body
51-
protoLogRecord.body = protoBody
48+
if let body = logRecord.body {
49+
protoLogRecord.body = CommonAdapter.toProtoAnyValue(attributeValue: body)
5250
}
5351

5452

Sources/OpenTelemetryApi/Logs/DefaultLogger.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class DefaultLogger: Logger {
5353
return self
5454
}
5555

56-
func setBody(_ body: String) -> Self {
56+
func setBody(_ body: AttributeValue) -> Self {
5757
return self
5858
}
5959

Sources/OpenTelemetryApi/Logs/LogRecordBuilder.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public protocol LogRecordBuilder {
3434
///
3535
/// - Parameter body: string value of the log
3636
/// - Returns: self
37-
func setBody(_ body: String) -> Self
37+
func setBody(_ body: AttributeValue) -> Self
3838

3939
/// set attributes assoicated with the log.
4040
///

Sources/OpenTelemetrySdk/Logs/Data/ReadableLogRecord.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import Foundation
77
import OpenTelemetryApi
88

99
public struct ReadableLogRecord : Codable {
10-
public init(resource: Resource, instrumentationScopeInfo: InstrumentationScopeInfo, timestamp: Date, observedTimestamp: Date? = nil, spanContext: SpanContext? = nil, severity: Severity? = nil, body: String? = nil, attributes: [String : AttributeValue]) {
10+
public init(resource: Resource, instrumentationScopeInfo: InstrumentationScopeInfo, timestamp: Date, observedTimestamp: Date? = nil, spanContext: SpanContext? = nil, severity: Severity? = nil, body: AttributeValue? = nil, attributes: [String : AttributeValue]) {
1111
self.resource = resource
1212
self.instrumentationScopeInfo = instrumentationScopeInfo
1313
self.timestamp = timestamp
@@ -24,7 +24,7 @@ public struct ReadableLogRecord : Codable {
2424
public private(set) var observedTimestamp : Date?
2525
public private(set) var spanContext : SpanContext?
2626
public private(set) var severity : Severity?
27-
public private(set) var body: String?
27+
public private(set) var body: AttributeValue?
2828
public private(set) var attributes : [String: AttributeValue]
2929
}
3030

Sources/OpenTelemetrySdk/Logs/LogRecordBuilderSdk.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class LogRecordBuilderSdk: EventBuilder {
1414
private var includeSpanContext: Bool
1515
private var timestamp: Date?
1616
private var observedTimestamp: Date?
17-
private var body: String?
17+
private var body: AttributeValue?
1818
private var severity: Severity?
1919
private var attributes: AttributesDictionary
2020
private var spanContext: SpanContext?
@@ -53,7 +53,7 @@ public class LogRecordBuilderSdk: EventBuilder {
5353
return self
5454
}
5555

56-
public func setBody(_ body: String) -> Self {
56+
public func setBody(_ body: AttributeValue) -> Self {
5757
self.body = body
5858
return self
5959
}

Tests/ExportersTests/OpenTelemetryProtocol/CommonAdapterTests.swift

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,47 @@ class CommonAdapterTests: XCTestCase {
5353
XCTAssertEqual(instrumentationScope.name, "name")
5454
XCTAssertEqual(instrumentationScope.version, "")
5555
}
56+
57+
func testToProtoAnyValue() {
58+
59+
let anyStringValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.string("hello,world"))
60+
XCTAssertEqual(anyStringValue.stringValue, "hello,world")
61+
62+
let anyBoolValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.bool(false))
63+
XCTAssertFalse(anyBoolValue.boolValue)
64+
65+
let anyIntValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.int(12))
66+
XCTAssertEqual(anyIntValue.intValue, 12)
67+
68+
let anyDoubleValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.double(3.14))
69+
XCTAssertEqual(anyDoubleValue.doubleValue, 3.14)
70+
71+
let anyStringArrayValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.stringArray(["hello"]))
72+
73+
XCTAssertEqual(anyStringArrayValue.arrayValue.values.count, 1)
74+
XCTAssertTrue(anyStringArrayValue.arrayValue.values[0].stringValue == "hello")
75+
76+
let anyBoolArrayValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.boolArray([true]))
77+
78+
XCTAssertEqual(anyBoolArrayValue.arrayValue.values.count, 1)
79+
XCTAssertTrue(anyBoolArrayValue.arrayValue.values[0].boolValue)
80+
81+
let anyIntArrayValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.intArray([1]))
82+
XCTAssertEqual(anyIntArrayValue.arrayValue.values.count, 1)
83+
XCTAssertTrue(anyIntArrayValue.arrayValue.values[0].intValue == 1)
84+
85+
let anyDoubleArrayValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.doubleArray([3.14]))
86+
XCTAssertEqual(anyDoubleArrayValue.arrayValue.values.count, 1)
87+
XCTAssertTrue(anyDoubleArrayValue.arrayValue.values[0].doubleValue == 3.14)
88+
89+
let anySetValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.set(AttributeSet(labels: ["Hello": AttributeValue.string("world")])))
90+
XCTAssertTrue(anySetValue.kvlistValue.values.count == 1)
91+
XCTAssertTrue(anySetValue.kvlistValue.values[0].key == "Hello")
92+
XCTAssertTrue(anySetValue.kvlistValue.values[0].value.stringValue == "world")
93+
94+
95+
96+
97+
98+
}
5699
}

Tests/ExportersTests/OpenTelemetryProtocol/LogRecordAdapterTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class LogRecordAdapterTests : XCTestCase {
4343
observedTimestamp: Date.distantPast,
4444
spanContext: spanContext,
4545
severity: .fatal,
46-
body: "Hello, world",
46+
body: AttributeValue.string("Hello, world"),
4747
attributes: ["event.name":AttributeValue.string("name"), "event.domain": AttributeValue.string("domain")])
4848

4949
let protoLog = LogRecordAdapter.toProtoLogRecord(logRecord: logRecord)

Tests/ExportersTests/OpenTelemetryProtocol/OtlpHttpLogRecordExporterTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class OtlpHttpLogRecordExporterTests: XCTestCase {
3434
}
3535

3636
func testExport() {
37-
let testBody = "Hello world " + String(Int.random(in: 1...100))
37+
let testBody = AttributeValue.string("Helloworld" + String(Int.random(in: 1...100)))
3838
let logRecord = ReadableLogRecord(resource: Resource(),
3939
instrumentationScopeInfo: InstrumentationScopeInfo(name: "scope"),
4040
timestamp: Date(),
@@ -57,7 +57,7 @@ class OtlpHttpLogRecordExporterTests: XCTestCase {
5757
XCTAssertNoThrow(try testServer.receiveBodyAndVerify() { body in
5858
var contentsBuffer = ByteBuffer(buffer: body)
5959
let contents = contentsBuffer.readString(length: contentsBuffer.readableBytes)!
60-
XCTAssertTrue(contents.contains(testBody))
60+
XCTAssertTrue(contents.description.contains(testBody.description))
6161
})
6262

6363
XCTAssertNoThrow(try testServer.receiveEnd())

Tests/ExportersTests/OpenTelemetryProtocol/OtlpLogRecordExporterTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class OtlpLogRecordExporterTests: XCTestCase {
7474
observedTimestamp: Date.distantPast,
7575
spanContext: spanContext,
7676
severity: .fatal,
77-
body: "Hello, world",
77+
body: AttributeValue.string("Hello, world"),
7878
attributes: ["event.name":AttributeValue.string("name"), "event.domain": AttributeValue.string("domain")])
7979

8080
let exporter = OtlpLogExporter(channel: channel)
@@ -122,7 +122,7 @@ class OtlpLogRecordExporterTests: XCTestCase {
122122
observedTimestamp: Date.distantPast,
123123
spanContext: spanContext,
124124
severity: .fatal,
125-
body: "Hello, world",
125+
body: AttributeValue.string("Hello, world"),
126126
attributes: ["event.name":AttributeValue.string("name"), "event.domain": AttributeValue.string("domain")])
127127
let exporter = OtlpLogExporter(channel: channel)
128128
exporter.shutdown()
@@ -139,7 +139,7 @@ class OtlpLogRecordExporterTests: XCTestCase {
139139
observedTimestamp: Date.distantPast,
140140
spanContext: spanContext,
141141
severity: .fatal,
142-
body: "Hello, world",
142+
body: AttributeValue.string("Hello, world"),
143143
attributes: ["event.name":AttributeValue.string("name"),
144144
"event.domain": AttributeValue.string("domain")])
145145
let result = exporter.export(logRecords: [logRecord])

0 commit comments

Comments
 (0)