Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expands LogRecord to allow "anyValue" in body parameter. #499

Merged
merged 3 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}


Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenTelemetryApi/Logs/DefaultLogger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public class DefaultLogger: Logger {
return self
}

func setBody(_ body: String) -> Self {
func setBody(_ body: AttributeValue) -> Self {
return self
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/OpenTelemetryApi/Logs/LogRecordBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenTelemetrySdk/Logs/Data/ReadableLogRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]
}

Expand Down
4 changes: 2 additions & 2 deletions Sources/OpenTelemetrySdk/Logs/LogRecordBuilderSdk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")





}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ 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")])
.emit()

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)
Expand Down
Loading