|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'opentelemetry/exporter/otlp' |
| 4 | +require 'opentelemetry/sdk' |
| 5 | +require 'temporalio/contrib/open_telemetry' |
| 6 | +require 'temporalio/runtime' |
| 7 | + |
| 8 | +module OpenTelemetry |
| 9 | + module Util |
| 10 | + def self.configure_metrics_and_tracing |
| 11 | + # Before doing anything, configure the default runtime with OpenTelemetry metrics. Unlike OpenTelemetry tracing in |
| 12 | + # Temporal, OpenTelemetry metrics does not use the Ruby OpenTelemetry library, but rather an internal one. |
| 13 | + Temporalio::Runtime.default = Temporalio::Runtime.new( |
| 14 | + telemetry: Temporalio::Runtime::TelemetryOptions.new( |
| 15 | + metrics: Temporalio::Runtime::MetricsOptions.new( |
| 16 | + opentelemetry: Temporalio::Runtime::OpenTelemetryMetricsOptions.new( |
| 17 | + url: 'http://127.0.0.1:4317', |
| 18 | + durations_as_seconds: true |
| 19 | + ) |
| 20 | + ) |
| 21 | + ) |
| 22 | + ) |
| 23 | + # Globally configure the Ruby OpenTelemetry library for tracing purposes. As of this writing, OpenTelemetry Ruby |
| 24 | + # does not support OTLP over gRPC, so we use the HTTP endpoint instead. |
| 25 | + OpenTelemetry::SDK.configure do |c| |
| 26 | + c.service_name = 'my-service' |
| 27 | + c.use_all |
| 28 | + # Can use a SimpleSpanProcessor instead of a BatchSpanProcessor, but batch is better for production and moves |
| 29 | + # the span exporting outside of the workflow instead of synchronously inside the workflow context. |
| 30 | + processor = OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new( |
| 31 | + OpenTelemetry::Exporter::OTLP::Exporter.new( |
| 32 | + endpoint: 'http://localhost:4318/v1/traces' |
| 33 | + ) |
| 34 | + ) |
| 35 | + c.add_span_processor(processor) |
| 36 | + # We need to shutdown the batch span processor on process exit to flush spans |
| 37 | + at_exit { processor.shutdown } |
| 38 | + end |
| 39 | + end |
| 40 | + end |
| 41 | +end |
0 commit comments