Skip to content

Commit 4d4a132

Browse files
slashmoktoso
andauthored
Create probabilistic sampler (#9)
Co-authored-by: Konrad `ktoso` Malawski <ktoso@apple.com>
1 parent 94e9cfe commit 4d4a132

File tree

8 files changed

+92
-9
lines changed

8 files changed

+92
-9
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ let package = Package(
1010
dependencies: [
1111
.package(name: "swift-context", url: "https://github.com/slashmo/gsoc-swift-baggage-context", from: "0.5.0"),
1212
.package(url: "https://github.com/slashmo/gsoc-swift-tracing.git", .branch("main")),
13-
.package(url: "https://github.com/slashmo/swift-w3c-trace-context.git", from: "0.5.0"),
13+
.package(url: "https://github.com/slashmo/swift-w3c-trace-context.git", from: "0.6.0"),
1414
.package(url: "https://github.com/slashmo/swift-nio.git", .branch("feature/baggage-context")),
1515
],
1616
targets: [

Sources/Jaeger/Sampling/ConstantSampler.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,16 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
import W3CTraceContext
15+
1416
public struct ConstantSampler: Sampler {
1517
private let samples: Bool
1618

1719
public init(samples: Bool) {
1820
self.samples = samples
1921
}
2022

21-
public func sample(operationName: String, traceID: String) -> SamplingStatus {
23+
public func sample(operationName: String, traceID: TraceID) -> SamplingStatus {
2224
SamplingStatus(isSampled: self.samples, attributes: [
2325
"sampler.type": "const",
2426
"sampler.param": self.samples ? "true" : "false",
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Jaeger Client Swift open source project
4+
//
5+
// Copyright (c) 2020 Moritz Lang and the Jaeger Client Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
import W3CTraceContext
15+
16+
public struct ProbabilisticSampler: Sampler {
17+
private let samplingRate: Double
18+
private let lowUpperBound: UInt64
19+
20+
public init(samplingRate: Double = 0.001) {
21+
precondition(
22+
samplingRate > 0 && samplingRate < 1,
23+
"The sampling rate must be greater than 0.0 and less than 1.0. Please use ConstantSampler instead."
24+
)
25+
self.samplingRate = samplingRate
26+
self.lowUpperBound = UInt64(samplingRate * Double(UInt64.max))
27+
}
28+
29+
public func sample(operationName: String, traceID: TraceID) -> SamplingStatus {
30+
SamplingStatus(isSampled: traceID.low < self.lowUpperBound, attributes: [
31+
"sampler.type": "probabilistic",
32+
"sampler.param": .double(self.samplingRate),
33+
])
34+
}
35+
}

Sources/Jaeger/Sampling/Sampler.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
import Tracing
15+
import W3CTraceContext
1516

1617
public struct SamplingStatus {
17-
var isSampled: Bool
18-
var attributes: SpanAttributes
18+
public let isSampled: Bool
19+
public let attributes: SpanAttributes
1920
}
2021

2122
public protocol Sampler {
22-
func sample(operationName: String, traceID: String) -> SamplingStatus
23+
func sample(operationName: String, traceID: TraceID) -> SamplingStatus
2324
}

Sources/ZipkinReporting/Span+ZipkinRepresentation.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ extension JaegerSpan {
4444

4545
return ZipkinRepresentation(
4646
id: traceContext.parent.parentID,
47-
traceID: traceContext.parent.traceID,
47+
traceID: String(describing: traceContext.parent.traceID),
4848
parentID: self.links.first?.baggage.traceContext?.parent.parentID,
4949
name: self.operationName,
5050
timestamp: self.startTimestamp.microsSinceEpoch,
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Jaeger Client Swift open source project
4+
//
5+
// Copyright (c) 2020 Moritz Lang and the Jaeger Client Swift project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
//
10+
// SPDX-License-Identifier: Apache-2.0
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
import Jaeger
15+
import W3CTraceContext
16+
import XCTest
17+
18+
final class ProbabilisticSamplerTests: XCTestCase {
19+
func test_sample_comparesLowIDPartToUpperBound() {
20+
let samplingRate = Double.random(in: Double.leastNonzeroMagnitude ..< 1.0)
21+
let upperBound = UInt64(samplingRate * Double(UInt64.max))
22+
let sampler = ProbabilisticSampler(samplingRate: samplingRate)
23+
24+
do {
25+
let traceID = TraceID(high: 0, low: upperBound)
26+
let samplingStatus = sampler.sample(operationName: "test", traceID: traceID)
27+
XCTAssertFalse(samplingStatus.isSampled)
28+
}
29+
30+
do {
31+
let traceID = TraceID(high: 0, low: upperBound - 1)
32+
let samplingStatus = sampler.sample(operationName: "test", traceID: traceID)
33+
XCTAssert(samplingStatus.isSampled)
34+
}
35+
}
36+
37+
func test_sample_returnsSamplingStatus_withAttributes() {
38+
let sampler = ProbabilisticSampler(samplingRate: 0.1)
39+
40+
let samplingStatus = sampler.sample(operationName: "test", traceID: .random())
41+
42+
XCTAssertEqual(samplingStatus.attributes["sampler.type"], "probabilistic")
43+
XCTAssertEqual(samplingStatus.attributes["sampler.param"], 0.1)
44+
}
45+
}

Tests/ZipkinReportingTests/ZipkinSpanRepresentationTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ final class ZipkinSpanRepresentationTests: XCTestCase {
4444

4545
XCTAssertNotNil(zipkinRepresentation)
4646
XCTAssertEqual(zipkinRepresentation?.id, child.baggage.traceContext?.parent.parentID)
47-
XCTAssertEqual(zipkinRepresentation?.traceID, child.baggage.traceContext?.parent.traceID)
47+
XCTAssertEqual(zipkinRepresentation?.traceID, child.baggage.traceContext?.parent.traceID.description)
4848
XCTAssertNotNil(zipkinRepresentation?.parentID)
4949
XCTAssertEqual(zipkinRepresentation?.parentID, parent.baggage.traceContext?.parent.parentID)
5050
XCTAssertEqual(zipkinRepresentation?.name, "child")

0 commit comments

Comments
 (0)