-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
226 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
import ArgumentParser | ||
import Node | ||
import ServiceLifecycle | ||
import TracingUtils | ||
|
||
@main | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import OTel | ||
import OTLPGRPC | ||
import ServiceLifecycle | ||
import TracingUtils | ||
|
||
public enum Tracing { | ||
public static func bootstrap(_ serviceName: String) async throws -> [Service] { | ||
// Bootstrap the logging backend with the OTel metadata provider which includes span IDs in logging messages. | ||
LoggingSystem.bootstrap { label in | ||
var handler = StreamLogHandler.standardError(label: label, metadataProvider: .otel) | ||
handler.logLevel = .trace | ||
return handler | ||
} | ||
|
||
// Configure OTel resource detection to automatically apply helpful attributes to events. | ||
let environment = OTelEnvironment.detected() | ||
let resourceDetection = OTelResourceDetection(detectors: [ | ||
OTelProcessResourceDetector(), | ||
OTelEnvironmentResourceDetector(environment: environment), | ||
.manual( | ||
OTelResource( | ||
attributes: SpanAttributes(["service.name": serviceName.toSpanAttribute()]) | ||
) | ||
), | ||
]) | ||
let resource = await resourceDetection.resource(environment: environment, logLevel: .trace) | ||
|
||
// Bootstrap the metrics backend to export metrics periodically in OTLP/gRPC. | ||
let registry = OTelMetricRegistry() | ||
let metricsExporter = try OTLPGRPCMetricExporter( | ||
configuration: .init(environment: environment) | ||
) | ||
let metrics = OTelPeriodicExportingMetricsReader( | ||
resource: resource, | ||
producer: registry, | ||
exporter: metricsExporter, | ||
configuration: .init( | ||
environment: environment, | ||
exportInterval: .seconds(5) // NOTE: This is overridden for the example; the default is 60 seconds. | ||
) | ||
) | ||
MetricsSystem.bootstrap(OTLPMetricsFactory(registry: registry)) | ||
|
||
// Bootstrap the tracing backend to export traces periodically in OTLP/gRPC. | ||
let exporter = try OTLPGRPCSpanExporter(configuration: .init(environment: environment)) | ||
let processor = OTelBatchSpanProcessor( | ||
exporter: exporter, configuration: .init(environment: environment) | ||
) | ||
let tracer = OTelTracer( | ||
idGenerator: OTelRandomIDGenerator(), | ||
sampler: OTelConstantSampler(isOn: true), | ||
propagator: OTelW3CPropagator(), | ||
processor: processor, | ||
environment: environment, | ||
resource: resource | ||
) | ||
InstrumentationSystem.bootstrap(tracer) | ||
|
||
return [tracer, metrics] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.