Skip to content

Commit

Permalink
reformatted all files with swiftformat
Browse files Browse the repository at this point in the history
  • Loading branch information
bryce-b committed Feb 19, 2025
1 parent 9efd3b7 commit 788de32
Show file tree
Hide file tree
Showing 644 changed files with 34,323 additions and 34,419 deletions.
5 changes: 5 additions & 0 deletions .swiftformat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
--exclude Sources/Exporters/OpenTelemetryProtocolCommon/proto,.build,Build
--indent 2
--allman false
--disable wrapMultilineStatementBraces
--commas inline
2 changes: 2 additions & 0 deletions .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ included:
excluded:
- "Sources/Exporters/OpenTelemetryProtocolCommon/proto"
- ".build"
- "Tests"
disabled_rules:
- todo
- nesting
- line_length
- type_body_length
Expand Down
76 changes: 38 additions & 38 deletions Examples/ConcurrencyContext/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,63 +4,63 @@
*/

#if canImport(_Concurrency)
import OpenTelemetrySdk
import OpenTelemetryConcurrency
import StdoutExporter
import OpenTelemetryConcurrency
import OpenTelemetrySdk
import StdoutExporter

let sampleKey = "sampleKey"
let sampleValue = "sampleValue"
let sampleKey = "sampleKey"
let sampleValue = "sampleValue"

// On Apple platforms, the default is the activity based context manager. We want to opt-in to the structured concurrency based context manager instead.
OpenTelemetry.registerDefaultConcurrencyContextManager()
// On Apple platforms, the default is the activity based context manager. We want to opt-in to the structured concurrency based context manager instead.
OpenTelemetry.registerDefaultConcurrencyContextManager()

let stdout = StdoutSpanExporter()
OpenTelemetry.registerTracerProvider(
let stdout = StdoutSpanExporter()
OpenTelemetry.registerTracerProvider(
tracerProvider: TracerProviderBuilder().add(
spanProcessor: SimpleSpanProcessor(spanExporter: stdout)
spanProcessor: SimpleSpanProcessor(spanExporter: stdout)
).build()
)
)

let tracer = OpenTelemetry.instance.tracerProvider.get(instrumentationName: "ConcurrencyContext", instrumentationVersion: "semver:0.1.0")
let tracer = OpenTelemetry.instance.tracerProvider.get(instrumentationName: "ConcurrencyContext", instrumentationVersion: "semver:0.1.0")

extension Task where Failure == Never, Success == Never {
extension Task where Failure == Never, Success == Never {
static func sleep(seconds: Double) async throws {
try await self.sleep(nanoseconds: UInt64(seconds * 1_000_000_000))
try await sleep(nanoseconds: UInt64(seconds * 1_000_000_000))
}
}
}

func simpleSpan() async throws {
func simpleSpan() async throws {
let span = await tracer.spanBuilder(spanName: "SimpleSpan").setSpanKind(spanKind: .client).startSpan()
span.setAttribute(key: sampleKey, value: sampleValue)
try await Task.sleep(seconds: 0.5)
span.end()
}
}

func childSpan() async throws {
func childSpan() async throws {
// SpanBuilder's `setActive` method is not available here, since it isn't compatible with structured concurrency based context management
try await tracer.spanBuilder(spanName: "parentSpan").setSpanKind(spanKind: .client).withActiveSpan { span in
span.setAttribute(key: sampleKey, value: sampleValue)
await Task.detached {
// A detached task doesn't inherit the task local context, so this span won't have a parent.
let notAChildSpan = await tracer.spanBuilder(spanName: "notAChild").setSpanKind(spanKind: .client).startSpan()
notAChildSpan.setAttribute(key: sampleKey, value: sampleValue)
notAChildSpan.end()
}.value
span.setAttribute(key: sampleKey, value: sampleValue)
await Task.detached {
// A detached task doesn't inherit the task local context, so this span won't have a parent.
let notAChildSpan = await tracer.spanBuilder(spanName: "notAChild").setSpanKind(spanKind: .client).startSpan()
notAChildSpan.setAttribute(key: sampleKey, value: sampleValue)
notAChildSpan.end()
}.value

try await Task {
// Normal tasks should still inherit the context.
try await Task.sleep(seconds: 0.2)
let childSpan = await tracer.spanBuilder(spanName: "childSpan").setSpanKind(spanKind: .client).startSpan()
childSpan.setAttribute(key: sampleKey, value: sampleValue)
try await Task.sleep(seconds: 0.5)
childSpan.end()
}.value
try await Task {
// Normal tasks should still inherit the context.
try await Task.sleep(seconds: 0.2)
let childSpan = await tracer.spanBuilder(spanName: "childSpan").setSpanKind(spanKind: .client).startSpan()
childSpan.setAttribute(key: sampleKey, value: sampleValue)
try await Task.sleep(seconds: 0.5)
childSpan.end()
}.value
}
}
}

try await simpleSpan()
try await Task.sleep(seconds: 1)
try await childSpan()
try await Task.sleep(seconds: 1)
try await simpleSpan()
try await Task.sleep(seconds: 1)
try await childSpan()
try await Task.sleep(seconds: 1)

#endif
92 changes: 46 additions & 46 deletions Examples/Datadog Sample/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,28 @@

#if os(macOS)

import DatadogExporter
import Foundation
import OpenTelemetryApi
import OpenTelemetrySdk
import DatadogExporter
import Foundation
import OpenTelemetryApi
import OpenTelemetrySdk

let apikeyOrClientToken = ""
let apikeyOrClientToken = ""

let resourceKey = "resource.name"
let resourceValue = "The resource"
let resourceKey = "resource.name"
let resourceValue = "The resource"

let sampleKey = "sampleKey"
let sampleValue = "sampleValue"
let sampleKey = "sampleKey"
let sampleValue = "sampleValue"

let instrumentationScopeName = "DatadogExporter"
let instrumentationScopeVersion = "semver:0.1.0"
let instrumentationScopeName = "DatadogExporter"
let instrumentationScopeVersion = "semver:0.1.0"

var tracer: Tracer
tracer = OpenTelemetry.instance.tracerProvider.get(instrumentationName: instrumentationScopeName, instrumentationVersion: instrumentationScopeVersion)
var tracer: Tracer
tracer = OpenTelemetry.instance.tracerProvider.get(instrumentationName: instrumentationScopeName, instrumentationVersion: instrumentationScopeVersion)

let hostName = Host.current().localizedName
let hostName = Host.current().localizedName

let exporterConfiguration = ExporterConfiguration(
let exporterConfiguration = ExporterConfiguration(
serviceName: "Opentelemetry exporter Example",
resource: "Opentelemetry exporter",
applicationName: "SwiftDatadogSample",
Expand All @@ -37,48 +37,48 @@ let exporterConfiguration = ExporterConfiguration(
uploadCondition: { true },
performancePreset: .instantDataDelivery,
hostName: hostName
)
)

let datadogExporter = try! DatadogExporter(config: exporterConfiguration)
let datadogExporter = try! DatadogExporter(config: exporterConfiguration)

testTraces()
testMetrics()
testTraces()
testMetrics()

sleep(10)
sleep(10)

func testTraces() {
func testTraces() {
let spanProcessor = SimpleSpanProcessor(spanExporter: datadogExporter)

OpenTelemetry.registerTracerProvider(tracerProvider:
TracerProviderBuilder()
.add(spanProcessor: spanProcessor)
.build()
TracerProviderBuilder()
.add(spanProcessor: spanProcessor)
.build()
)
tracer = OpenTelemetry.instance.tracerProvider.get(instrumentationName: instrumentationScopeName, instrumentationVersion: instrumentationScopeVersion) as! TracerSdk

simpleSpan()
childSpan()
spanProcessor.shutdown()
}
}

func simpleSpan() {
func simpleSpan() {
let span = tracer.spanBuilder(spanName: "SimpleSpan").setSpanKind(spanKind: .client).startSpan()
span.setAttribute(key: resourceKey, value: resourceValue)
span.addEvent(name: "My event", attributes: ["message": AttributeValue.string("test message"),
"newKey": AttributeValue.string("New Value")])
span.end()
}
}

func childSpan() {
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()
}
}

func testMetrics() {
func testMetrics() {
let processor = MetricProcessorSdk()

let meterProvider = MeterProviderSdk(metricProcessor: processor, metricExporter: datadogExporter, metricPushInterval: 0.1)
Expand All @@ -93,27 +93,27 @@ func testMetrics() {
let labels2 = ["dim1": "value2"]

_ = meter.createIntObserver(name: "MyObservation") { observer in
var taskInfo = mach_task_basic_info()
var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size) / 4
let _: kern_return_t = withUnsafeMutablePointer(to: &taskInfo) {
$0.withMemoryRebound(to: integer_t.self, capacity: 1) {
task_info(mach_task_self_, task_flavor_t(MACH_TASK_BASIC_INFO), $0, &count)
}
var taskInfo = mach_task_basic_info()
var count = mach_msg_type_number_t(MemoryLayout<mach_task_basic_info>.size) / 4
let _: kern_return_t = withUnsafeMutablePointer(to: &taskInfo) {
$0.withMemoryRebound(to: integer_t.self, capacity: 1) {
task_info(mach_task_self_, task_flavor_t(MACH_TASK_BASIC_INFO), $0, &count)
}
observer.observe(value: Int(taskInfo.resident_size), labels: labels2)
}
observer.observe(value: Int(taskInfo.resident_size), labels: labels2)
}

var counter = 0
while counter < 3000 {
testCounter.add(value: 100, labelset: meter.getLabelSet(labels: labels1))

testMeasure.record(value: 100, labelset: meter.getLabelSet(labels: labels1))
testMeasure.record(value: 500, labelset: meter.getLabelSet(labels: labels1))
testMeasure.record(value: 5, labelset: meter.getLabelSet(labels: labels1))
testMeasure.record(value: 750, labelset: meter.getLabelSet(labels: labels1))
counter += 1
sleep(1)
testCounter.add(value: 100, labelset: meter.getLabelSet(labels: labels1))

testMeasure.record(value: 100, labelset: meter.getLabelSet(labels: labels1))
testMeasure.record(value: 500, labelset: meter.getLabelSet(labels: labels1))
testMeasure.record(value: 5, labelset: meter.getLabelSet(labels: labels1))
testMeasure.record(value: 750, labelset: meter.getLabelSet(labels: labels1))
counter += 1
sleep(1)
}
}
}

#endif
24 changes: 12 additions & 12 deletions Examples/Logging Tracer/Logger.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@
import Foundation

class Logger {
static let startTime = Date()
static let startTime = Date()

static func printHeader() {
print("TimeSinceStart | ThreadId | API")
}
static func printHeader() {
print("TimeSinceStart | ThreadId | API")
}

static func log(_ s: String) {
let output = String(format: "%.9f | %@ | %@", timeSinceStart(), Thread.current.description, s)
print(output)
}
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)
}
private static func timeSinceStart() -> Double {
let start = startTime
return Date().timeIntervalSince(start)
}
}
16 changes: 8 additions & 8 deletions Examples/Logging Tracer/LoggingBinaryFormat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import Foundation
import OpenTelemetryApi

struct LoggingBinaryFormat: BinaryFormattable {
func fromByteArray(bytes: [UInt8]) -> SpanContext? {
Logger.log("LoggingBinaryFormat.FromByteArray(...)")
return nil
}
func fromByteArray(bytes _: [UInt8]) -> SpanContext? {
Logger.log("LoggingBinaryFormat.FromByteArray(...)")
return nil
}

func toByteArray(spanContext: SpanContext) -> [UInt8] {
Logger.log("LoggingBinaryFormat.ToByteArray({spanContext})")
return [UInt8]()
}
func toByteArray(spanContext _: SpanContext) -> [UInt8] {
Logger.log("LoggingBinaryFormat.ToByteArray({spanContext})")
return [UInt8]()
}
}
Loading

0 comments on commit 788de32

Please sign in to comment.