Skip to content

Commit

Permalink
Add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
maxep committed Aug 14, 2024
1 parent 5f2ced6 commit 121b2fd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
10 changes: 9 additions & 1 deletion BenchmarkTests/Benchmarks/Sources/Benchmarks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ import OpenTelemetrySdk
let instrumentationName = "benchmarks"
let instrumentationVersion = "1.0.0"

/// Benchmark entrypoint to configure opentelemetry with metrics meters
/// and tracer.
public enum Benchmarks {
/// Configuration of the Benchmarks library.
public struct Configuration {
/// Context of Benchmarks measures.
/// The context properties will be added metrics as tags.
public struct Context {
var applicationIdentifier: String
var applicationName: String
Expand Down Expand Up @@ -68,7 +73,10 @@ public enum Benchmarks {
self.context = context
}
}


/// Configure OpenTelemetry metrics meter and start measuring Memory.
///
/// - Parameter configuration: The Benchmark configuration.
public static func metrics(with configuration: Configuration) {
let loggerProvider = LoggerProviderBuilder()
.build()
Expand Down
18 changes: 13 additions & 5 deletions BenchmarkTests/Benchmarks/Sources/MetricExporter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ enum MetricExporterError: Error {

/// Replacement of otel `DatadogExporter` for metrics.
///
/// This version doesn not store data to disk, it uploads to the intake directly.
/// This version does not store data to disk, it uploads to the intake directly.
/// Additionally, it does not crash.
final class MetricExporter: Exporter, OpenTelemetrySdk.MetricExporter {
struct Configuration {
Expand All @@ -29,6 +29,7 @@ final class MetricExporter: Exporter, OpenTelemetrySdk.MetricExporter {
case gauge = 3
}

/// https://docs.datadoghq.com/api/latest/metrics/#submit-metrics
internal struct Serie: Codable {
struct Point: Codable {
let timestamp: Int64
Expand Down Expand Up @@ -66,15 +67,19 @@ final class MetricExporter: Exporter, OpenTelemetrySdk.MetricExporter {

func export(metrics: [Metric], shouldCancel: (() -> Bool)?) -> MetricExporterResultCode {
do {
let series = try metrics.map { try export(metric: $0) }
let series = try metrics.map(transform)
try submit(series: series)
return.success
} catch {
return .failureNotRetryable
}
}

func export(metric: Metric) throws -> Serie {

/// Transforms otel `Metric` to Datadog `serie`.
///
/// - Parameter metric: The otel metric
/// - Returns: The timeserie.
func transform(_ metric: Metric) throws -> Serie {
var tags: Set<String> = []

let points: [Serie.Point] = try metric.data.map { data in
Expand Down Expand Up @@ -113,7 +118,10 @@ final class MetricExporter: Exporter, OpenTelemetrySdk.MetricExporter {
tags: Array(tags)
)
}


/// Submit timeseries to the Metrics intake.
///
/// - Parameter series: The timeseries.
func submit(series: [Serie]) throws {
var data = try series.reduce(Data()) { data, serie in
try data + encoder.encode(serie) + separator
Expand Down

0 comments on commit 121b2fd

Please sign in to comment.