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

fixed removeContextValue and changed log body from String to Any #494

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 @@ -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, !body.description.isEmpty {
protoLogRecord.body = Opentelemetry_Proto_Common_V1_AnyValue()
}


Expand Down
12 changes: 5 additions & 7 deletions Sources/OpenTelemetryApi/Context/ActivityContextManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,12 @@ 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,12 +43,12 @@
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)

XCTAssertEqual(protoLog.body.stringValue, "Hello, world")

Check failure on line 51 in Tests/ExportersTests/OpenTelemetryProtocol/LogRecordAdapterTests.swift

View workflow job for this annotation

GitHub Actions / watchOS

testToProto, XCTAssertEqual failed: ("") is not equal to ("Hello, world")

Check failure on line 51 in Tests/ExportersTests/OpenTelemetryProtocol/LogRecordAdapterTests.swift

View workflow job for this annotation

GitHub Actions / tvOS

testToProto, XCTAssertEqual failed: ("") is not equal to ("Hello, world")

Check failure on line 51 in Tests/ExportersTests/OpenTelemetryProtocol/LogRecordAdapterTests.swift

View workflow job for this annotation

GitHub Actions / iOS

testToProto, XCTAssertEqual failed: ("") is not equal to ("Hello, world")
XCTAssertEqual(protoLog.hasBody, true)
XCTAssertEqual(protoLog.severityText, "FATAL")
XCTAssertEqual(protoLog.observedTimeUnixNano, Date.distantPast.timeIntervalSince1970.toNanoseconds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
}

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 @@
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))

Check failure on line 60 in Tests/ExportersTests/OpenTelemetryProtocol/OtlpHttpLogRecordExporterTests.swift

View workflow job for this annotation

GitHub Actions / watchOS

testExport, XCTAssertTrue failed

Check failure on line 60 in Tests/ExportersTests/OpenTelemetryProtocol/OtlpHttpLogRecordExporterTests.swift

View workflow job for this annotation

GitHub Actions / tvOS

testExport, XCTAssertTrue failed

Check failure on line 60 in Tests/ExportersTests/OpenTelemetryProtocol/OtlpHttpLogRecordExporterTests.swift

View workflow job for this annotation

GitHub Actions / iOS

testExport, XCTAssertTrue failed
})

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
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