Skip to content

Commit

Permalink
fixed removeContextValue and changed log body from String to Any
Browse files Browse the repository at this point in the history
  • Loading branch information
vvydier committed Dec 18, 2023
1 parent 0953181 commit dbc458f
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,8 @@ public class LogRecordAdapter {

protoLogRecord.timeUnixNano = logRecord.timestamp.timeIntervalSince1970.toNanoseconds

if let body = logRecord.body, !body.isEmpty {
if let body = logRecord.body, !body.description.isEmpty {
var protoBody = Opentelemetry_Proto_Common_V1_AnyValue()
protoBody.stringValue = body
protoLogRecord.body = protoBody
}

Expand Down
11 changes: 5 additions & 6 deletions Sources/OpenTelemetryApi/Context/ActivityContextManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,11 @@ class ActivityContextManager: ContextManager {
func removeContextValue(forKey key: OpenTelemetryContextKeys, value: AnyObject) {
let activityIdent = os_activity_get_identifier(OS_ACTIVITY_CURRENT, nil)
rlock.lock()
if let currentValue = contextMap[activityIdent]?[key.rawValue],
currentValue === value
{
contextMap[activityIdent]?[key.rawValue] = nil
if contextMap[activityIdent]?.isEmpty ?? false {
contextMap[activityIdent] = nil

for (activityKey, activity) in contextMap where value === activity[key.rawValue] {
contextMap[activityKey]?[key.rawValue] = nil
if contextMap[activityKey]?.isEmpty ?? false {
contextMap[activityKey] = nil
}
}
if let scope = objectScope.object(forKey: value) {
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 @@ -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("Hello world " + 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(testBody.description.contains(contents))
})

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 @@ -308,6 +308,45 @@ class ActivityContextManagerTests: XCTestCase {
XCTAssert(OpenTelemetry.instance.contextProvider.activeSpan === nil)
}


@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)
func testRemoveContextValuesFromSpan() {

//Create a span
let span1 = defaultTracer.spanBuilder(spanName: "span1").startSpan()
ActivityContextManager.instance.setCurrentContextValue(forKey: .span, value: span1)
XCTAssert(ActivityContextManager.instance.getCurrentContextValue(forKey: .span) === span1)

//Add it to one parent in one thread
let parent1 = defaultTracer.spanBuilder(spanName: "parent1").startSpan()
ActivityContextManager.instance.setCurrentContextValue(forKey: .span, value: parent1)
XCTAssert(ActivityContextManager.instance.getCurrentContextValue(forKey: .span) === parent1)

DispatchQueue.global().async {
let activeSpan = ActivityContextManager.instance.getCurrentContextValue(forKey: .span)
XCTAssert(activeSpan === parent1)
ActivityContextManager.instance.setCurrentContextValue(forKey: .span, value: span1)
parent1.end()
}

//Add it to another parent in another thread
let parent2 = defaultTracer.spanBuilder(spanName: "parent2").startSpan()
ActivityContextManager.instance.setCurrentContextValue(forKey: .span, value: parent2)
XCTAssert(ActivityContextManager.instance.getCurrentContextValue(forKey: .span) === parent2)

DispatchQueue.global().async {
let activeSpan = ActivityContextManager.instance.getCurrentContextValue(forKey: .span)
XCTAssert(activeSpan === parent2)
ActivityContextManager.instance.setCurrentContextValue(forKey: .span, value: span1)
parent2.end()
}

//remove all the contexts from the span and check if the Context is nil
sleep(1)
ActivityContextManager.instance.removeContextValue(forKey: .span, value: span1)
XCTAssert(ActivityContextManager.instance.getCurrentContextValue(forKey: .span) === nil)
}

@available(macOS 10.15, iOS 13.0, tvOS 13.0, *)
func testActiveSpanIsKeptPerTaskAsync() async {
let expectation1 = self.expectation(description: "firstSpan created")
Expand Down
2 changes: 1 addition & 1 deletion Tests/OpenTelemetryApiTests/Logs/DefaultLoggerTests.swift
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
4 changes: 2 additions & 2 deletions Tests/OpenTelemetrySdkTests/Logs/ReadableLogRecordTests.swift
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

0 comments on commit dbc458f

Please sign in to comment.