Skip to content

Commit

Permalink
Merge branch 'main' into feature/log-processing-wip
Browse files Browse the repository at this point in the history
  • Loading branch information
xtremekforever committed Oct 3, 2024
2 parents 468e932 + 81a3eb9 commit 5c1a976
Show file tree
Hide file tree
Showing 20 changed files with 277 additions and 16 deletions.
3 changes: 2 additions & 1 deletion .github/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ coverage:
threshold: 5%

ignore:
- Sources/OTelTesting
- "Sources/OTelTesting"
- "**/*/OTelTracing+Benchmarking.swift"
73 changes: 73 additions & 0 deletions .github/workflows/benchmark.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
name: Benchmark PR vs. main

on:
pull_request:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}-benchmark
cancel-in-progress: true

jobs:
benchmark:
name: Check benchmark regressions
runs-on: ubuntu-latest
timeout-minutes: 15
continue-on-error: true

steps:
- name: Checkout
uses: actions/checkout@v4.1.7
with:
fetch-depth: 0

- name: Install jemalloc
run: sudo apt-get install -y libjemalloc-dev

- name: Benchmark PR
run: |
cd Benchmarks
swift package --allow-writing-to-directory .benchmarkBaselines/ benchmark baseline update pr
- name: Benchmark main
run: |
git switch main
cd Benchmarks
swift package --allow-writing-to-directory .benchmarkBaselines/ benchmark baseline update main
- name: Check benchmark delta
id: check_delta
run: |
echo $(date) >> $GITHUB_STEP_SUMMARY
echo "BENCHMARK_STATUS=1" >> "$GITHUB_OUTPUT"
cd Benchmarks
swift package benchmark baseline check main pr --format markdown >> $GITHUB_STEP_SUMMARY
echo "BENCHMARK_STATUS=0" >> "$GITHUB_OUTPUT"
continue-on-error: true

- name: Produce success comment
if: ${{ steps.check_delta.outputs.BENCHMARK_STATUS == '0' }}
run: |
echo 'PRTEST<<EOF' >> $GITHUB_ENV
echo "[Pull request benchmark comparison with 'main' run at $(date -Iseconds)](https://github.com/slashmo/${{ github.event.repository.name }}/actions/runs/${{ github.run_id }})" >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
- name: Produce failure comment
if: ${{ steps.check_delta.outputs.BENCHMARK_STATUS != '0' }}
run: |
echo 'PRTEST<<EOF' >> $GITHUB_ENV
echo "[Pull request benchmark comparison with 'main' run at $(date -Iseconds)](https://github.com/slashmo/${{ github.event.repository.name }}/actions/runs/${{ github.run_id }})" >> $GITHUB_ENV
echo "_Pull request had performance regressions_" >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
- name: Comment PR
uses: thollander/actions-comment-pull-request@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
message: ${{ env.PRTEST }}
comment_tag: benchmark

- name: Exit with correct status
run: |
exit ${{ steps.check_delta.outputs.BENCHMARK_STATUS }}
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v4.1.7
uses: actions/checkout@v4.2.0

- name: Bootstrap Mint
uses: irgaly/setup-mint@v1
Expand Down Expand Up @@ -35,7 +35,7 @@ jobs:
SWIFTLY_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Checkout
uses: actions/checkout@v4.1.7
uses: actions/checkout@v4.2.0

- name: Cache Swift PM
uses: actions/cache@v4.0.2
Expand Down Expand Up @@ -83,7 +83,7 @@ jobs:
env:
SWIFTLY_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout
uses: actions/checkout@v4.1.7
uses: actions/checkout@v4.2.0
- name: Resolve Swift dependencies
run: swift package resolve
- name: Build
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
SWIFTLY_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Checkout repository
uses: actions/checkout@v4.1.7
uses: actions/checkout@v4.2.0

- name: Initialize CodeQL
uses: github/codeql-action/init@v3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ xcuserdata/
.protoc-grpc-swift-plugins/
.protoc-grpc-swift-plugins.download/
swift-otel-workspace.xcworkspace/
.benchmarkBaselines
32 changes: 32 additions & 0 deletions Benchmarks/Benchmarks/OTelTracing/Benchmarks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift OTel open source project
//
// Copyright (c) 2023 Moritz Lang and the Swift OTel project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Benchmark
import Foundation

let benchmarks = {
let ciMetrics: [BenchmarkMetric] = [
.instructions,
.mallocCountTotal,
]
let localMetrics = BenchmarkMetric.default

Benchmark.defaultConfiguration = .init(
metrics: ProcessInfo.processInfo.environment["CI"] != nil ? ciMetrics : localMetrics,
warmupIterations: 10
)

// MARK: - Benchmarks

tracerBenchmarks()
}
76 changes: 76 additions & 0 deletions Benchmarks/Benchmarks/OTelTracing/TracerBenchmarks.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift OTel open source project
//
// Copyright (c) 2023 Moritz Lang and the Swift OTel project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Benchmark
@_spi(OTelBenchmarking) import OTel
import ServiceContextModule
import W3CTraceContext

func tracerBenchmarks() {
Benchmark("Starting sampled root spans") { benchmark in
let tracer = OTelTracer(
idGenerator: OTelRandomIDGenerator(),
sampler: OTelConstantSampler(isOn: true),
propagator: OTelW3CPropagator(),
processor: OTelNoOpSpanProcessor(),
environment: [:],
resource: OTelResource()
)

benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
blackHole(tracer.startSpan("test"))
}
}

Benchmark("Starting sampled child spans") { benchmark in
let tracer = OTelTracer(
idGenerator: OTelRandomIDGenerator(),
sampler: OTelConstantSampler(isOn: true),
propagator: OTelW3CPropagator(),
processor: OTelNoOpSpanProcessor(),
environment: [:],
resource: OTelResource()
)

let parentSpanContext = OTelSpanContext.local(
traceID: .random(),
spanID: .random(),
parentSpanID: nil,
traceFlags: .sampled,
traceState: TraceState()
)
let parentContext = ServiceContext.withSpanContext(parentSpanContext)

benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
blackHole(tracer.startSpan("test", context: parentContext))
}
}

Benchmark("Starting dropped root spans") { benchmark in
let tracer = OTelTracer(
idGenerator: OTelRandomIDGenerator(),
sampler: OTelConstantSampler(isOn: false),
propagator: OTelW3CPropagator(),
processor: OTelNoOpSpanProcessor(),
environment: [:],
resource: OTelResource()
)

benchmark.startMeasurement()
for _ in benchmark.scaledIterations {
blackHole(tracer.startSpan("test"))
}
}
}
26 changes: 26 additions & 0 deletions Benchmarks/Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// swift-tools-version:5.9
import PackageDescription

let package = Package(
name: "swif-otel-benchmarks",
platforms: [
.macOS(.v13),
],
dependencies: [
.package(url: "https://github.com/ordo-one/package-benchmark.git", from: "1.0.0"),
.package(path: ".."),
],
targets: [
.executableTarget(
name: "OTelTracing",
dependencies: [
.product(name: "Benchmark", package: "package-benchmark"),
.product(name: "OTel", package: "swift-otel"),
],
path: "Benchmarks/OTelTracing",
plugins: [
.plugin(name: "BenchmarkPlugin", package: "package-benchmark"),
]
),
]
)
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ define contents_xcworkspacedata
<Group location="container:" name="Examples">
$(foreach example,$(EXAMPLES),<FileRef location="group:$(example)"></FileRef>\n)
</Group>
<Group location="container:Benchmarks" name="Benchmarks">
<FileRef location="group:." name="benchmarks"></FileRef>
</Group>
</Workspace>
endef
export contents_xcworkspacedata
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ let package = Package(
.package(url: "https://github.com/apple/swift-nio.git", from: "2.0.0"),
.package(url: "https://github.com/apple/swift-atomics.git", from: "1.0.2"),
.package(url: "https://github.com/apple/swift-metrics.git", from: "2.4.1"),
.package(url: "https://github.com/slashmo/swift-w3c-trace-context.git", from: "1.0.0-beta.1"),
.package(url: "https://github.com/slashmo/swift-w3c-trace-context.git", exact: "1.0.0-beta.3"),

// MARK: - OTLP

Expand Down
24 changes: 24 additions & 0 deletions Sources/OTel/Tracing/OTelFinishedSpan.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,28 @@ public struct OTelFinishedSpan: Sendable {

/// The links from this span to other spans.
public let links: [SpanLink]

public init(
spanContext: OTelSpanContext,
operationName: String,
kind: SpanKind,
status: SpanStatus?,
startTimeNanosecondsSinceEpoch: UInt64,
endTimeNanosecondsSinceEpoch: UInt64,
attributes: SpanAttributes,
resource: OTelResource,
events: [SpanEvent],
links: [SpanLink]
) {
self.spanContext = spanContext
self.operationName = operationName
self.kind = kind
self.status = status
self.startTimeNanosecondsSinceEpoch = startTimeNanosecondsSinceEpoch
self.endTimeNanosecondsSinceEpoch = endTimeNanosecondsSinceEpoch
self.attributes = attributes
self.resource = resource
self.events = events
self.links = links
}
}
23 changes: 23 additions & 0 deletions Sources/OTel/Tracing/OTelTracing+Benchmarking.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift OTel open source project
//
// Copyright (c) 2023 Moritz Lang and the Swift OTel project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import ServiceContextModule

extension ServiceContext {
@_spi(OTelBenchmarking)
public static func withSpanContext(_ spanContext: OTelSpanContext) -> Self {
var context = ServiceContext.topLevel
context.spanContext = spanContext
return context
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public actor OTelBatchSpanProcessor<Exporter: OTelSpanExporter, Clock: _Concurre
private func export(_ batch: some Collection<OTelFinishedSpan> & Sendable) async {
let batchID = batchID
self.batchID += 1
print(batchID)

var exportLogger = logger
exportLogger[metadataKey: "batch_id"] = "\(batchID)"
Expand Down
4 changes: 2 additions & 2 deletions Sources/OTelTesting/OTelSpanID+Stub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import W3CTraceContext

extension SpanID {
/// A stub span ID for testing with bytes from one to eight.
public static let oneToEight = SpanID(bytes: (1, 2, 3, 4, 5, 6, 7, 8))
public static let oneToEight = SpanID(bytes: .init((1, 2, 3, 4, 5, 6, 7, 8)))

/// A stub span ID for testing with all bytes being zero.
public static let allZeroes = SpanID(bytes: (0, 0, 0, 0, 0, 0, 0, 0))
public static let allZeroes = SpanID(bytes: .init((0, 0, 0, 0, 0, 0, 0, 0)))
}
4 changes: 2 additions & 2 deletions Sources/OTelTesting/OTelTraceID+Stub.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import W3CTraceContext

extension TraceID {
/// A trace ID stub with bytes from one to sixteen.
public static let oneToSixteen = TraceID(bytes: (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16))
public static let oneToSixteen = TraceID(bytes: .init((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)))

/// A trace ID stub with all bytes being zero.
public static let allZeroes = TraceID(bytes: (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0))
public static let allZeroes = TraceID(bytes: .init((0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)))
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ final class OTelFinishedSpanProtoTests: XCTestCase {
}

func test_initProtoSpan_withFinishedSpan_withParentSpanID_setsParentSpanID() {
let parentSpanID = SpanID(bytes: (1, 2, 3, 4, 5, 6, 7, 8))
let parentSpanID = SpanID.oneToEight
let span = OTelFinishedSpan.stub(parentSpanID: parentSpanID)

let protoSpan = Opentelemetry_Proto_Trace_V1_Span(span)
Expand Down
4 changes: 2 additions & 2 deletions Tests/OTelTests/Context/OTelRandomIDGeneratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ final class OTelRandomIDGeneratorTests: XCTestCase {

XCTAssertEqual(
generator.nextTraceID(),
TraceID(bytes: (255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255))
TraceID(bytes: .init((255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255)))
)
}

Expand Down Expand Up @@ -52,7 +52,7 @@ final class OTelRandomIDGeneratorTests: XCTestCase {
func test_spanID_witConstantNumberGenerator_returnsConstantSpanID() {
let generator = OTelRandomIDGenerator(randomNumberGenerator: ConstantNumberGenerator(value: .max))

XCTAssertEqual(generator.nextSpanID(), SpanID(bytes: (255, 255, 255, 255, 255, 255, 255, 255)))
XCTAssertEqual(generator.nextSpanID(), SpanID(bytes: .init((255, 255, 255, 255, 255, 255, 255, 255))))
}

func test_spanID_withConstantNumberGenerator_withRandomNumber_returnsRandomSpanID() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ private struct SystemAPropagator: OTelPropagator {
}

private struct SystemBPropagator: OTelPropagator {
static let validTraceID = TraceID(bytes: (16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1))
static let validSpanID = SpanID(bytes: (8, 7, 6, 5, 4, 3, 2, 1))
static let validTraceID = TraceID(bytes: .init((16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1)))
static let validSpanID = SpanID(bytes: .init((8, 7, 6, 5, 4, 3, 2, 1)))

public func extractSpanContext<Carrier, Extract>(
from carrier: Carrier,
Expand Down
Loading

0 comments on commit 5c1a976

Please sign in to comment.