diff --git a/Sources/Exporters/OpenTelemetryProtocolCommon/common/CommonAdapter.swift b/Sources/Exporters/OpenTelemetryProtocolCommon/common/CommonAdapter.swift index 6249e696..cf18c67f 100644 --- a/Sources/Exporters/OpenTelemetryProtocolCommon/common/CommonAdapter.swift +++ b/Sources/Exporters/OpenTelemetryProtocolCommon/common/CommonAdapter.swift @@ -54,6 +54,49 @@ public struct CommonAdapter { return keyValue } + public static func toProtoAnyValue(attributeValue: AttributeValue) -> Opentelemetry_Proto_Common_V1_AnyValue { + var anyValue = Opentelemetry_Proto_Common_V1_AnyValue() + switch attributeValue { + case let .string(value): + anyValue.stringValue = value + case let .bool(value): + anyValue.boolValue = value + case let .int(value): + anyValue.intValue = Int64(value) + case let .double(value): + anyValue.doubleValue = value + case let .stringArray(value): + anyValue.arrayValue.values = value.map { + var anyValue = Opentelemetry_Proto_Common_V1_AnyValue() + anyValue.stringValue = $0 + return anyValue + } + case let .boolArray(value): + anyValue.arrayValue.values = value.map { + var anyValue = Opentelemetry_Proto_Common_V1_AnyValue() + anyValue.boolValue = $0 + return anyValue + } + case let .intArray(value): + anyValue.arrayValue.values = value.map { + var anyValue = Opentelemetry_Proto_Common_V1_AnyValue() + anyValue.intValue = Int64($0) + return anyValue + } + case let .doubleArray(value): + anyValue.arrayValue.values = value.map { + var anyValue = Opentelemetry_Proto_Common_V1_AnyValue() + anyValue.doubleValue = $0 + return anyValue + } + case let .set(value): + anyValue.kvlistValue.values = value.labels.map({ + return toProtoAttribute(key: $0, attributeValue: $1) + }) + } + return anyValue + } + public static func toProtoInstrumentationScope(instrumentationScopeInfo: InstrumentationScopeInfo) -> Opentelemetry_Proto_Common_V1_InstrumentationScope { diff --git a/Sources/Exporters/OpenTelemetryProtocolCommon/logs/LogRecordAdapter.swift b/Sources/Exporters/OpenTelemetryProtocolCommon/logs/LogRecordAdapter.swift index cdba8324..3b0abe48 100644 --- a/Sources/Exporters/OpenTelemetryProtocolCommon/logs/LogRecordAdapter.swift +++ b/Sources/Exporters/OpenTelemetryProtocolCommon/logs/LogRecordAdapter.swift @@ -45,10 +45,8 @@ public class LogRecordAdapter { protoLogRecord.timeUnixNano = logRecord.timestamp.timeIntervalSince1970.toNanoseconds - if let body = logRecord.body, !body.isEmpty { - var protoBody = Opentelemetry_Proto_Common_V1_AnyValue() - protoBody.stringValue = body - protoLogRecord.body = protoBody + if let body = logRecord.body { + protoLogRecord.body = CommonAdapter.toProtoAnyValue(attributeValue: body) } diff --git a/Sources/OpenTelemetryApi/Logs/DefaultLogger.swift b/Sources/OpenTelemetryApi/Logs/DefaultLogger.swift index 72d76dce..ab7a184a 100644 --- a/Sources/OpenTelemetryApi/Logs/DefaultLogger.swift +++ b/Sources/OpenTelemetryApi/Logs/DefaultLogger.swift @@ -53,7 +53,7 @@ public class DefaultLogger: Logger { return self } - func setBody(_ body: String) -> Self { + func setBody(_ body: AttributeValue) -> Self { return self } diff --git a/Sources/OpenTelemetryApi/Logs/LogRecordBuilder.swift b/Sources/OpenTelemetryApi/Logs/LogRecordBuilder.swift index 3c72752a..907471bd 100644 --- a/Sources/OpenTelemetryApi/Logs/LogRecordBuilder.swift +++ b/Sources/OpenTelemetryApi/Logs/LogRecordBuilder.swift @@ -34,7 +34,7 @@ public protocol LogRecordBuilder { /// /// - Parameter body: string value of the log /// - Returns: self - func setBody(_ body: String) -> Self + func setBody(_ body: AttributeValue) -> Self /// set attributes assoicated with the log. /// diff --git a/Sources/OpenTelemetrySdk/Logs/Data/ReadableLogRecord.swift b/Sources/OpenTelemetrySdk/Logs/Data/ReadableLogRecord.swift index a32cffd0..98b5a317 100644 --- a/Sources/OpenTelemetrySdk/Logs/Data/ReadableLogRecord.swift +++ b/Sources/OpenTelemetrySdk/Logs/Data/ReadableLogRecord.swift @@ -7,7 +7,7 @@ import Foundation import OpenTelemetryApi public struct ReadableLogRecord : Codable { - public init(resource: Resource, instrumentationScopeInfo: InstrumentationScopeInfo, timestamp: Date, observedTimestamp: Date? = nil, spanContext: SpanContext? = nil, severity: Severity? = nil, body: String? = nil, attributes: [String : AttributeValue]) { + public init(resource: Resource, instrumentationScopeInfo: InstrumentationScopeInfo, timestamp: Date, observedTimestamp: Date? = nil, spanContext: SpanContext? = nil, severity: Severity? = nil, body: AttributeValue? = nil, attributes: [String : AttributeValue]) { self.resource = resource self.instrumentationScopeInfo = instrumentationScopeInfo self.timestamp = timestamp @@ -24,7 +24,7 @@ public struct ReadableLogRecord : Codable { public private(set) var observedTimestamp : Date? public private(set) var spanContext : SpanContext? public private(set) var severity : Severity? - public private(set) var body: String? + public private(set) var body: AttributeValue? public private(set) var attributes : [String: AttributeValue] } diff --git a/Sources/OpenTelemetrySdk/Logs/LogRecordBuilderSdk.swift b/Sources/OpenTelemetrySdk/Logs/LogRecordBuilderSdk.swift index 7a52e834..ade416f5 100644 --- a/Sources/OpenTelemetrySdk/Logs/LogRecordBuilderSdk.swift +++ b/Sources/OpenTelemetrySdk/Logs/LogRecordBuilderSdk.swift @@ -14,7 +14,7 @@ public class LogRecordBuilderSdk: EventBuilder { private var includeSpanContext: Bool private var timestamp: Date? private var observedTimestamp: Date? - private var body: String? + private var body: AttributeValue? private var severity: Severity? private var attributes: AttributesDictionary private var spanContext: SpanContext? @@ -53,7 +53,7 @@ public class LogRecordBuilderSdk: EventBuilder { return self } - public func setBody(_ body: String) -> Self { + public func setBody(_ body: AttributeValue) -> Self { self.body = body return self } diff --git a/Tests/ExportersTests/OpenTelemetryProtocol/CommonAdapterTests.swift b/Tests/ExportersTests/OpenTelemetryProtocol/CommonAdapterTests.swift index e4287aa5..44fadaff 100644 --- a/Tests/ExportersTests/OpenTelemetryProtocol/CommonAdapterTests.swift +++ b/Tests/ExportersTests/OpenTelemetryProtocol/CommonAdapterTests.swift @@ -53,4 +53,47 @@ class CommonAdapterTests: XCTestCase { XCTAssertEqual(instrumentationScope.name, "name") XCTAssertEqual(instrumentationScope.version, "") } + + func testToProtoAnyValue() { + + let anyStringValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.string("hello,world")) + XCTAssertEqual(anyStringValue.stringValue, "hello,world") + + let anyBoolValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.bool(false)) + XCTAssertFalse(anyBoolValue.boolValue) + + let anyIntValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.int(12)) + XCTAssertEqual(anyIntValue.intValue, 12) + + let anyDoubleValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.double(3.14)) + XCTAssertEqual(anyDoubleValue.doubleValue, 3.14) + + let anyStringArrayValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.stringArray(["hello"])) + + XCTAssertEqual(anyStringArrayValue.arrayValue.values.count, 1) + XCTAssertTrue(anyStringArrayValue.arrayValue.values[0].stringValue == "hello") + + let anyBoolArrayValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.boolArray([true])) + + XCTAssertEqual(anyBoolArrayValue.arrayValue.values.count, 1) + XCTAssertTrue(anyBoolArrayValue.arrayValue.values[0].boolValue) + + let anyIntArrayValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.intArray([1])) + XCTAssertEqual(anyIntArrayValue.arrayValue.values.count, 1) + XCTAssertTrue(anyIntArrayValue.arrayValue.values[0].intValue == 1) + + let anyDoubleArrayValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.doubleArray([3.14])) + XCTAssertEqual(anyDoubleArrayValue.arrayValue.values.count, 1) + XCTAssertTrue(anyDoubleArrayValue.arrayValue.values[0].doubleValue == 3.14) + + let anySetValue = CommonAdapter.toProtoAnyValue(attributeValue: AttributeValue.set(AttributeSet(labels: ["Hello": AttributeValue.string("world")]))) + XCTAssertTrue(anySetValue.kvlistValue.values.count == 1) + XCTAssertTrue(anySetValue.kvlistValue.values[0].key == "Hello") + XCTAssertTrue(anySetValue.kvlistValue.values[0].value.stringValue == "world") + + + + + + } } diff --git a/Tests/ExportersTests/OpenTelemetryProtocol/LogRecordAdapterTests.swift b/Tests/ExportersTests/OpenTelemetryProtocol/LogRecordAdapterTests.swift index a909d634..39467c9d 100644 --- a/Tests/ExportersTests/OpenTelemetryProtocol/LogRecordAdapterTests.swift +++ b/Tests/ExportersTests/OpenTelemetryProtocol/LogRecordAdapterTests.swift @@ -43,7 +43,7 @@ class LogRecordAdapterTests : XCTestCase { observedTimestamp: Date.distantPast, spanContext: spanContext, severity: .fatal, - body: "Hello, world", + body: AttributeValue.string("Hello, world"), attributes: ["event.name":AttributeValue.string("name"), "event.domain": AttributeValue.string("domain")]) let protoLog = LogRecordAdapter.toProtoLogRecord(logRecord: logRecord) diff --git a/Tests/ExportersTests/OpenTelemetryProtocol/OtlpHttpLogRecordExporterTests.swift b/Tests/ExportersTests/OpenTelemetryProtocol/OtlpHttpLogRecordExporterTests.swift index e34be308..a46cb3b1 100644 --- a/Tests/ExportersTests/OpenTelemetryProtocol/OtlpHttpLogRecordExporterTests.swift +++ b/Tests/ExportersTests/OpenTelemetryProtocol/OtlpHttpLogRecordExporterTests.swift @@ -34,7 +34,7 @@ class OtlpHttpLogRecordExporterTests: XCTestCase { } func testExport() { - let testBody = "Hello world " + String(Int.random(in: 1...100)) + let testBody = AttributeValue.string("Helloworld" + String(Int.random(in: 1...100))) let logRecord = ReadableLogRecord(resource: Resource(), instrumentationScopeInfo: InstrumentationScopeInfo(name: "scope"), timestamp: Date(), @@ -57,7 +57,7 @@ class OtlpHttpLogRecordExporterTests: XCTestCase { XCTAssertNoThrow(try testServer.receiveBodyAndVerify() { body in var contentsBuffer = ByteBuffer(buffer: body) let contents = contentsBuffer.readString(length: contentsBuffer.readableBytes)! - XCTAssertTrue(contents.contains(testBody)) + XCTAssertTrue(contents.description.contains(testBody.description)) }) XCTAssertNoThrow(try testServer.receiveEnd()) diff --git a/Tests/ExportersTests/OpenTelemetryProtocol/OtlpLogRecordExporterTests.swift b/Tests/ExportersTests/OpenTelemetryProtocol/OtlpLogRecordExporterTests.swift index 42f751a8..aa079fd5 100644 --- a/Tests/ExportersTests/OpenTelemetryProtocol/OtlpLogRecordExporterTests.swift +++ b/Tests/ExportersTests/OpenTelemetryProtocol/OtlpLogRecordExporterTests.swift @@ -74,7 +74,7 @@ class OtlpLogRecordExporterTests: XCTestCase { observedTimestamp: Date.distantPast, spanContext: spanContext, severity: .fatal, - body: "Hello, world", + body: AttributeValue.string("Hello, world"), attributes: ["event.name":AttributeValue.string("name"), "event.domain": AttributeValue.string("domain")]) let exporter = OtlpLogExporter(channel: channel) @@ -122,7 +122,7 @@ class OtlpLogRecordExporterTests: XCTestCase { observedTimestamp: Date.distantPast, spanContext: spanContext, severity: .fatal, - body: "Hello, world", + body: AttributeValue.string("Hello, world"), attributes: ["event.name":AttributeValue.string("name"), "event.domain": AttributeValue.string("domain")]) let exporter = OtlpLogExporter(channel: channel) exporter.shutdown() @@ -139,7 +139,7 @@ class OtlpLogRecordExporterTests: XCTestCase { observedTimestamp: Date.distantPast, spanContext: spanContext, severity: .fatal, - body: "Hello, world", + body: AttributeValue.string("Hello, world"), attributes: ["event.name":AttributeValue.string("name"), "event.domain": AttributeValue.string("domain")]) let result = exporter.export(logRecords: [logRecord]) diff --git a/Tests/OpenTelemetryApiTests/Logs/DefaultLoggerTests.swift b/Tests/OpenTelemetryApiTests/Logs/DefaultLoggerTests.swift index 1df6873c..063c59be 100644 --- a/Tests/OpenTelemetryApiTests/Logs/DefaultLoggerTests.swift +++ b/Tests/OpenTelemetryApiTests/Logs/DefaultLoggerTests.swift @@ -28,7 +28,7 @@ class DefaultLoggerTests : XCTestCase { .setTimestamp(Date()) .setObservedTimestamp(Date()) .setSeverity(.debug) - .setBody("hello, world") + .setBody(AttributeValue.string("hello, world")) .emit()) XCTAssertNoThrow(defaultLogger.eventBuilder(name: "Event").emit()) diff --git a/Tests/OpenTelemetrySdkTests/Logs/ReadableLogRecordTests.swift b/Tests/OpenTelemetrySdkTests/Logs/ReadableLogRecordTests.swift index f00a7ecc..01cef1ad 100644 --- a/Tests/OpenTelemetrySdkTests/Logs/ReadableLogRecordTests.swift +++ b/Tests/OpenTelemetrySdkTests/Logs/ReadableLogRecordTests.swift @@ -17,7 +17,7 @@ class ReadableLogRecordTests : XCTestCase { let provider = LoggerProviderBuilder().with(logLimits: LogLimits(maxAttributeCount: 1, maxAttributeLength: 1)).with(processors: [processor]).build() let logger = provider.get(instrumentationScopeName: "temp") logger.logRecordBuilder() - .setBody("hello, world") + .setBody(AttributeValue.string("hello, world")) .setSeverity(.debug) .setObservedTimestamp(observedTimestamp) .setAttributes(["firstAttribute": AttributeValue.string("only the 'o' will be captured"), "secondAttribute": AttributeValue.string("this attribute will be dropped")]) @@ -25,7 +25,7 @@ class ReadableLogRecordTests : XCTestCase { let logRecord = processor.onEmitCalledLogRecord XCTAssertEqual(logRecord?.observedTimestamp, observedTimestamp) - XCTAssertEqual(logRecord?.body, "hello, world") + XCTAssertEqual(logRecord?.body, AttributeValue.string("hello, world")) XCTAssertEqual(logRecord?.attributes.count, 1) let key = logRecord?.attributes.keys.first XCTAssertEqual(logRecord?.attributes[key!]?.description.count, 1)