Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ let package = Package(
dependencies: [
.package(name: "swift-context", url: "https://github.com/slashmo/gsoc-swift-baggage-context", from: "0.5.0"),
.package(url: "https://github.com/slashmo/gsoc-swift-tracing.git", .branch("main")),
.package(url: "https://github.com/slashmo/swift-w3c-trace-context.git", from: "0.5.0"),
.package(url: "https://github.com/slashmo/swift-w3c-trace-context.git", from: "0.6.0"),
.package(url: "https://github.com/slashmo/swift-nio.git", .branch("feature/baggage-context")),
],
targets: [
Expand Down
4 changes: 3 additions & 1 deletion Sources/Jaeger/Sampling/ConstantSampler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@
//
//===----------------------------------------------------------------------===//

import W3CTraceContext

public struct ConstantSampler: Sampler {
private let samples: Bool

public init(samples: Bool) {
self.samples = samples
}

public func sample(operationName: String, traceID: String) -> SamplingStatus {
public func sample(operationName: String, traceID: TraceID) -> SamplingStatus {
SamplingStatus(isSampled: self.samples, attributes: [
"sampler.type": "const",
"sampler.param": self.samples ? "true" : "false",
Expand Down
35 changes: 35 additions & 0 deletions Sources/Jaeger/Sampling/ProbabilisticSampler.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Jaeger Client Swift open source project
//
// Copyright (c) 2020 Moritz Lang and the Jaeger Client Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import W3CTraceContext

public struct ProbabilisticSampler: Sampler {
private let samplingRate: Double
private let lowUpperBound: UInt64

public init(samplingRate: Double = 0.001) {
precondition(
samplingRate > 0 && samplingRate < 1,
"The sampling rate must be greater than 0.0 and less than 1.0. Please use ConstantSampler instead."
)
self.samplingRate = samplingRate
self.lowUpperBound = UInt64(samplingRate * Double(UInt64.max))
}

public func sample(operationName: String, traceID: TraceID) -> SamplingStatus {
SamplingStatus(isSampled: traceID.low < self.lowUpperBound, attributes: [
"sampler.type": "probabilistic",
"sampler.param": .double(self.samplingRate),
])
}
}
7 changes: 4 additions & 3 deletions Sources/Jaeger/Sampling/Sampler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
//===----------------------------------------------------------------------===//

import Tracing
import W3CTraceContext

public struct SamplingStatus {
var isSampled: Bool
var attributes: SpanAttributes
public let isSampled: Bool
public let attributes: SpanAttributes
}

public protocol Sampler {
func sample(operationName: String, traceID: String) -> SamplingStatus
func sample(operationName: String, traceID: TraceID) -> SamplingStatus
}
2 changes: 1 addition & 1 deletion Sources/ZipkinReporting/Span+ZipkinRepresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ extension JaegerSpan {

return ZipkinRepresentation(
id: traceContext.parent.parentID,
traceID: traceContext.parent.traceID,
traceID: String(describing: traceContext.parent.traceID),
parentID: self.links.first?.baggage.traceContext?.parent.parentID,
name: self.operationName,
timestamp: self.startTimestamp.microsSinceEpoch,
Expand Down
45 changes: 45 additions & 0 deletions Tests/JaegerTests/Sampling/ProbabilisticSamplerTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Jaeger Client Swift open source project
//
// Copyright (c) 2020 Moritz Lang and the Jaeger Client Swift project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Jaeger
import W3CTraceContext
import XCTest

final class ProbabilisticSamplerTests: XCTestCase {
func test_sample_comparesLowIDPartToUpperBound() {
let samplingRate = Double.random(in: Double.leastNonzeroMagnitude ..< 1.0)
let upperBound = UInt64(samplingRate * Double(UInt64.max))
let sampler = ProbabilisticSampler(samplingRate: samplingRate)

do {
let traceID = TraceID(high: 0, low: upperBound)
let samplingStatus = sampler.sample(operationName: "test", traceID: traceID)
XCTAssertFalse(samplingStatus.isSampled)
}

do {
let traceID = TraceID(high: 0, low: upperBound - 1)
let samplingStatus = sampler.sample(operationName: "test", traceID: traceID)
XCTAssert(samplingStatus.isSampled)
}
}

func test_sample_returnsSamplingStatus_withAttributes() {
let sampler = ProbabilisticSampler(samplingRate: 0.1)

let samplingStatus = sampler.sample(operationName: "test", traceID: .random())

XCTAssertEqual(samplingStatus.attributes["sampler.type"], "probabilistic")
XCTAssertEqual(samplingStatus.attributes["sampler.param"], 0.1)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ final class ZipkinSpanRepresentationTests: XCTestCase {

XCTAssertNotNil(zipkinRepresentation)
XCTAssertEqual(zipkinRepresentation?.id, child.baggage.traceContext?.parent.parentID)
XCTAssertEqual(zipkinRepresentation?.traceID, child.baggage.traceContext?.parent.traceID)
XCTAssertEqual(zipkinRepresentation?.traceID, child.baggage.traceContext?.parent.traceID.description)
XCTAssertNotNil(zipkinRepresentation?.parentID)
XCTAssertEqual(zipkinRepresentation?.parentID, parent.baggage.traceContext?.parent.parentID)
XCTAssertEqual(zipkinRepresentation?.name, "child")
Expand Down