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

Add support for Linux by tracking context with ServiceContext #438

Closed
wants to merge 5 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
12 changes: 6 additions & 6 deletions Examples/Datadog Sample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ func simpleSpan() {
}

func childSpan() {
let span = tracer.spanBuilder(spanName: "parentSpan").setSpanKind(spanKind: .client).setActive(true).startSpan()
span.setAttribute(key: sampleKey, value: sampleValue)
let childSpan = tracer.spanBuilder(spanName: "childSpan").setSpanKind(spanKind: .client).startSpan()
childSpan.setAttribute(key: sampleKey, value: sampleValue)
childSpan.end()
span.end()
tracer.spanBuilder(spanName: "parentSpan").setSpanKind(spanKind: .client).withStartedActive { span in
span.setAttribute(key: sampleKey, value: sampleValue)
let childSpan = tracer.spanBuilder(spanName: "childSpan").setSpanKind(spanKind: .client).startSpan()
childSpan.setAttribute(key: sampleKey, value: sampleValue)
childSpan.end()
}
}

func testMetrics() {
Expand Down
31 changes: 20 additions & 11 deletions Examples/Logging Tracer/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,26 @@ var tracer = OpenTelemetry.instance.tracerProvider.get(instrumentationName: "Con


let span1 = tracer.spanBuilder(spanName: "Main (span1)").startSpan()
OpenTelemetry.instance.contextProvider.setActiveSpan(span1)
let semaphore = DispatchSemaphore(value: 0)
DispatchQueue.global().async {
let span2 = tracer.spanBuilder(spanName: "Main (span2)").startSpan()
OpenTelemetry.instance.contextProvider.setActiveSpan(span2)
OpenTelemetry.instance.contextProvider.activeSpan?.setAttribute(key: "myAttribute", value: "myValue")
sleep(1)
span2.end()
semaphore.signal()
OpenTelemetry.instance.contextProvider.withActiveSpan(span1) {
let semaphore = DispatchSemaphore(value: 0)

let state = OpenTelemetry.instance.contextProvider.getCurrentState()
DispatchQueue.global().async {
// Note: Restoring state here isn't necessary for the activity based context manager, activities are tracked across DispatchQueues
state.withRestoredState {
let span2 = tracer.spanBuilder(spanName: "Main (span2)").startSpan()
OpenTelemetry.instance.contextProvider.withActiveSpan(span2) {
OpenTelemetry.instance.contextProvider.activeSpan?.setAttribute(key: "myAttribute", value: "myValue")
sleep(1)
}

span2.end()
semaphore.signal()
}
}
span1.end()

semaphore.wait()
}
span1.end()

semaphore.wait()

27 changes: 14 additions & 13 deletions Examples/OTLP Exporter/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,22 @@ if #available(macOS 10.14, *), #available(iOS 12.0, *) {
func createSpans() {
let parentSpan1 = tracer.spanBuilder(spanName: "Main").setSpanKind(spanKind: .client).startSpan()
parentSpan1.setAttribute(key: sampleKey, value: sampleValue)
OpenTelemetry.instance.contextProvider.setActiveSpan(parentSpan1)
for _ in 1...3 {
doWork()
}
Thread.sleep(forTimeInterval: 0.5)

let parentSpan2 = tracer.spanBuilder(spanName: "Another").setSpanKind(spanKind: .client).setActive(true).startSpan()
parentSpan2.setAttribute(key: sampleKey, value: sampleValue)
// do more Work
for _ in 1...3 {
doWork()
OpenTelemetry.instance.contextProvider.withActiveSpan(parentSpan1) {
for _ in 1...3 {
doWork()
}
Thread.sleep(forTimeInterval: 0.5)

tracer.spanBuilder(spanName: "Another").setSpanKind(spanKind: .client).withStartedActive() { parentSpan2 in
parentSpan2.setAttribute(key: sampleKey, value: sampleValue)
// do more Work
for _ in 1...3 {
doWork()
}
Thread.sleep(forTimeInterval: 0.5)
}
}
Thread.sleep(forTimeInterval: 0.5)

parentSpan2.end()
parentSpan1.end()
}

Expand Down
29 changes: 15 additions & 14 deletions Examples/OTLP HTTP Exporter/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,22 @@ if #available(macOS 10.14, *), #available(iOS 12.0, *) {
func createSpans() {
let parentSpan1 = tracer.spanBuilder(spanName: "Main").setSpanKind(spanKind: .client).startSpan()
parentSpan1.setAttribute(key: sampleKey, value: sampleValue)
OpenTelemetry.instance.contextProvider.setActiveSpan(parentSpan1)
for _ in 1...3 {
doWork()
}
Thread.sleep(forTimeInterval: 0.5)

let parentSpan2 = tracer.spanBuilder(spanName: "Another").setSpanKind(spanKind: .client).setActive(true).startSpan()
parentSpan2.setAttribute(key: sampleKey, value: sampleValue)
// do more Work
for _ in 1...3 {
doWork()
OpenTelemetry.instance.contextProvider.withActiveSpan(parentSpan1) {
for _ in 1...3 {
doWork()
}
Thread.sleep(forTimeInterval: 0.5)

tracer.spanBuilder(spanName: "Another").setSpanKind(spanKind: .client).withStartedActive() { parentSpan2 in
parentSpan2.setAttribute(key: sampleKey, value: sampleValue)
// do more Work
for _ in 1...3 {
doWork()
}
Thread.sleep(forTimeInterval: 0.5)
}
}
Thread.sleep(forTimeInterval: 0.5)

parentSpan2.end()

parentSpan1.end()
}

Expand Down
16 changes: 8 additions & 8 deletions Examples/Simple Exporter/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ func simpleSpan() {
}

func childSpan() {
let span = tracer.spanBuilder(spanName: "parentSpan").setSpanKind(spanKind: .client).setActive(true).startSpan()
span.setAttribute(key: sampleKey, value: sampleValue)
Thread.sleep(forTimeInterval: 0.2)
let childSpan = tracer.spanBuilder(spanName: "childSpan").setSpanKind(spanKind: .client).startSpan()
childSpan.setAttribute(key: sampleKey, value: sampleValue)
Thread.sleep(forTimeInterval: 0.5)
childSpan.end()
span.end()
tracer.spanBuilder(spanName: "parentSpan").setSpanKind(spanKind: .client).withStartedActive { span in
span.setAttribute(key: sampleKey, value: sampleValue)
Thread.sleep(forTimeInterval: 0.2)
let childSpan = tracer.spanBuilder(spanName: "childSpan").setSpanKind(spanKind: .client).startSpan()
childSpan.setAttribute(key: sampleKey, value: sampleValue)
Thread.sleep(forTimeInterval: 0.5)
childSpan.end()
}
}

simpleSpan()
Expand Down
Loading