forked from open-telemetry/opentelemetry-swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Existing code landing (open-telemetry#3)
* Add existing code to the official OpenTelemetry Swift repo. * Update README with current status
- Loading branch information
1 parent
3defe28
commit 828b188
Showing
138 changed files
with
11,752 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2020, OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
|
||
class Logger { | ||
static let startTime = Date() | ||
|
||
static func printHeader() { | ||
print("TimeSinceStart | ThreadId | API") | ||
} | ||
|
||
static func log(_ s: String) { | ||
let output = String(format: "%.9f | %@ | %@", timeSinceStart(), Thread.current.description, s) | ||
print(output) | ||
} | ||
|
||
private static func timeSinceStart() -> Double { | ||
let start = startTime | ||
return Date().timeIntervalSince(start) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Copyright 2020, OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import OpenTelemetryApi | ||
|
||
struct LoggingBinaryFormat: BinaryFormattable { | ||
func fromByteArray(bytes: [UInt8]) -> SpanContext? { | ||
Logger.log("LoggingBinaryFormat.FromByteArray(...)") | ||
return SpanContext.invalid | ||
} | ||
|
||
func toByteArray(spanContext: SpanContext) -> [UInt8] { | ||
Logger.log("LoggingBinaryFormat.ToByteArray({spanContext})") | ||
return [UInt8]() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2020, OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import OpenTelemetryApi | ||
|
||
class LoggingSpan: Span { | ||
var name: String | ||
var kind: SpanKind | ||
var context: SpanContext = SpanContext.invalid | ||
var isRecordingEvents: Bool = true | ||
var status: Status? | ||
|
||
public init(name: String, kind: SpanKind) { | ||
self.name = name | ||
self.kind = kind | ||
} | ||
|
||
public var description: String { | ||
return name | ||
} | ||
|
||
public func updateName(name: String) { | ||
Logger.log("Span.updateName(\(name))") | ||
self.name = name | ||
} | ||
|
||
public func setAttribute(key: String, value: String) { | ||
Logger.log("Span.setAttribute(key: \(key), value: \(value))") | ||
} | ||
|
||
public func setAttribute(key: String, value: Int) { | ||
Logger.log("Span.setAttribute(key: \(key), value: \(value))") | ||
} | ||
|
||
public func setAttribute(key: String, value: Double) { | ||
Logger.log("Span.setAttribute(key: \(key), value: \(value))") | ||
} | ||
|
||
public func setAttribute(key: String, value: Bool) { | ||
Logger.log("Span.setAttribute(key: \(key), value: \(value))") | ||
} | ||
|
||
public func setAttribute(key: String, value: AttributeValue) { | ||
Logger.log("Span.setAttribute(key: \(key), value: \(value))") | ||
} | ||
|
||
public func setAttribute(keyValuePair: (String, AttributeValue)) { | ||
Logger.log("Span.SetAttributes(keyValue: \(keyValuePair))") | ||
setAttribute(key: keyValuePair.0, value: keyValuePair.1) | ||
} | ||
|
||
public func addEvent(name: String) { | ||
Logger.log("Span.addEvent(\(name))") | ||
} | ||
|
||
public func addEvent(name: String, timestamp: Int) { | ||
Logger.log("Span.addEvent(\(name) timestamp:\(timestamp))") | ||
} | ||
|
||
public func addEvent(name: String, attributes: [String: AttributeValue]) { | ||
Logger.log("Span.addEvent(\(name), attributes:\(attributes) )") | ||
} | ||
|
||
public func addEvent(name: String, attributes: [String: AttributeValue], timestamp: Int) { | ||
Logger.log("Span.addEvent(\(name), attributes:\(attributes), timestamp:\(timestamp))") | ||
} | ||
|
||
public func addEvent<E>(event: E) where E: Event { | ||
Logger.log("Span.addEvent(\(event))") | ||
} | ||
|
||
public func addEvent<E>(event: E, timestamp: Int) where E: Event { | ||
Logger.log("Span.addEvent(\(event), timestamp:\(timestamp))") | ||
} | ||
|
||
public func addLink(link: Link) { | ||
Logger.log("Span.addLink(\(link))") | ||
} | ||
|
||
public func end() { | ||
Logger.log("Span.End, Name: \(name)") | ||
} | ||
|
||
public func end(endOptions: EndSpanOptions) { | ||
Logger.log("Span.End, Name: \(name), timestamp:\(endOptions.timestamp)) }") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2020, OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import OpenTelemetryApi | ||
|
||
class LoggingTextFormat: TextFormattable { | ||
var fields = Set<String>() | ||
|
||
func inject<S>(spanContext: SpanContext, carrier: inout [String: String], setter: S) where S: Setter { | ||
Logger.log("LoggingTextFormat.Inject(\(spanContext), ...)") | ||
} | ||
|
||
func extract<G>(carrier: [String: String], getter: G) -> SpanContext where G: Getter { | ||
Logger.log("LoggingTextFormat.Extract(...)") | ||
return SpanContext.invalid | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2020, OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import OpenTelemetryApi | ||
|
||
class LoggingTracer: Tracer { | ||
let tracerName = "LoggingTracer" | ||
public var currentSpan: Span? { | ||
return ContextUtils.getCurrentSpan() | ||
} | ||
|
||
var binaryFormat: BinaryFormattable = BinaryTraceContextFormat() | ||
var textFormat: TextFormattable = HttpTraceContextFormat() | ||
|
||
func spanBuilder(spanName: String) -> SpanBuilder { | ||
return LoggingSpanBuilder(tracer: self, spanName: spanName) | ||
} | ||
|
||
func withSpan(_ span: Span) -> Scope { | ||
Logger.log("\(tracerName).WithSpan") | ||
return ContextUtils.withSpan(span) | ||
} | ||
|
||
class LoggingSpanBuilder: SpanBuilder { | ||
private var tracer: Tracer | ||
private var isRootSpan: Bool = false | ||
private var spanContext: SpanContext? | ||
private var name: String | ||
|
||
init(tracer: Tracer, spanName: String) { | ||
self.tracer = tracer | ||
name = spanName | ||
} | ||
|
||
func startSpan() -> Span { | ||
if spanContext == nil && !isRootSpan { | ||
spanContext = tracer.currentSpan?.context | ||
} | ||
return spanContext != nil && spanContext != SpanContext.invalid ? LoggingSpan(name: name, kind: .client) : DefaultSpan.random() | ||
} | ||
|
||
func setParent(_ parent: Span) -> Self { | ||
spanContext = parent.context | ||
return self | ||
} | ||
|
||
func setParent(_ parent: SpanContext) -> Self { | ||
spanContext = parent | ||
return self | ||
} | ||
|
||
func setNoParent() -> Self { | ||
isRootSpan = true | ||
return self | ||
} | ||
|
||
func addLink(spanContext: SpanContext) -> Self { | ||
return self | ||
} | ||
|
||
func addLink(spanContext: SpanContext, attributes: [String: AttributeValue]) -> Self { | ||
return self | ||
} | ||
|
||
func addLink(_ link: Link) -> Self { | ||
return self | ||
} | ||
|
||
func setSpanKind(spanKind: SpanKind) -> Self { | ||
return self | ||
} | ||
|
||
func setStartTimestamp(startTimestamp: Int) -> Self { | ||
return self | ||
} | ||
|
||
func setAttribute(key: String, value: AttributeValue) -> Self { | ||
return self | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2020, OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import OpenTelemetryApi | ||
|
||
class LoggingTracerRegistry: TracerRegistry { | ||
override func get(instrumentationName: String, instrumentationVersion: String?) -> Tracer { | ||
Logger.log("TracerFactory.get(\(instrumentationName), \(instrumentationVersion ?? ""))") | ||
var labels = [String: String]() | ||
labels["instrumentationName"] = instrumentationName | ||
labels["instrumentationVersion"] = instrumentationVersion | ||
return LoggingTracer() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright 2020, OpenTelemetry Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import OpenTelemetryApi | ||
|
||
Logger.printHeader() | ||
|
||
OpenTelemetry.registerTracerRegistry(tracerRegistry: LoggingTracerRegistry()) | ||
|
||
var tracer = OpenTelemetry.instance.tracerRegistry.get(instrumentationName: "ConsoleApp", instrumentationVersion: "semver:1.0.0") | ||
|
||
let scope = tracer.withSpan(tracer.spanBuilder(spanName: "Main (span1)").startSpan()) | ||
let semaphore = DispatchSemaphore(value: 0) | ||
DispatchQueue.global().async { | ||
var scope2 = tracer.withSpan(tracer.spanBuilder(spanName: "Main (span2)").startSpan()) | ||
tracer.currentSpan?.setAttribute(key: "myAttribute", value: "myValue") | ||
sleep(1) | ||
semaphore.signal() | ||
scope2.close() | ||
} | ||
|
||
semaphore.wait() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
### Simple Exporter | ||
|
||
This example shows the Jaeger an Stdout exporters in action using a MultiSpanExporter | ||
|
||
The sample expects a local Jaeger installation as explained in [Jaeger docs](https://www.jaegertracing.io/docs/1.16/getting-started/#all-in-one): | ||
|
||
``` | ||
docker run -d --name jaeger \ | ||
-e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \ | ||
-p 5775:5775/udp \ | ||
-p 6831:6831/udp \ | ||
-p 6832:6832/udp \ | ||
-p 5778:5778 \ | ||
-p 16686:16686 \ | ||
-p 14268:14268 \ | ||
-p 9411:9411 \ | ||
jaegertracing/all-in-one:1.16 | ||
``` | ||
|
Oops, something went wrong.