Skip to content

Commit

Permalink
Existing code landing (open-telemetry#3)
Browse files Browse the repository at this point in the history
* Add existing code to the official OpenTelemetry Swift repo.
* Update README with current status
  • Loading branch information
nachoBonafonte authored Feb 20, 2020
1 parent 3defe28 commit 828b188
Show file tree
Hide file tree
Showing 138 changed files with 11,752 additions and 6 deletions.
10 changes: 5 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ playground.xcworkspace
# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
# *.xcodeproj
Packages/
Package.pins
Package.resolved
*.xcodeproj
#
# Xcode automatically generates this directory with a .xcworkspacedata file and xcuserdata
# hence it is not needed unless you have added a package configuration file to your project
# .swiftpm
.swiftpm

.build/

Expand Down
34 changes: 34 additions & 0 deletions Examples/Logging Tracer/Logger.swift
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)
}
}
29 changes: 29 additions & 0 deletions Examples/Logging Tracer/LoggingBinaryFormat.swift
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]()
}
}
100 changes: 100 additions & 0 deletions Examples/Logging Tracer/LoggingSpan.swift
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)) }")
}
}
30 changes: 30 additions & 0 deletions Examples/Logging Tracer/LoggingTextFormat.swift
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
}
}
94 changes: 94 additions & 0 deletions Examples/Logging Tracer/LoggingTracer.swift
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
}
}
}
27 changes: 27 additions & 0 deletions Examples/Logging Tracer/LoggingTracerRegistry.swift
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()
}
}
35 changes: 35 additions & 0 deletions Examples/Logging Tracer/main.swift
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()
19 changes: 19 additions & 0 deletions Examples/Simple Exporter/README.md
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
```

Loading

0 comments on commit 828b188

Please sign in to comment.