Module: Legion::Telemetry
- Extended by:
- Logging::Helper
- Defined in:
- lib/legion/telemetry.rb,
lib/legion/telemetry/open_inference.rb,
lib/legion/telemetry/safety_metrics.rb
Defined Under Namespace
Modules: OpenInference, SafetyMetrics
Classes: SlidingWindow
Class Method Summary
collapse
Class Method Details
128
129
130
131
132
133
134
135
136
137
138
139
|
# File 'lib/legion/telemetry.rb', line 128
def configure_console
return false unless defined?(OpenTelemetry::SDK::Trace::Export::ConsoleSpanExporter)
exporter = OpenTelemetry::SDK::Trace::Export::ConsoleSpanExporter.new
processor = OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(exporter)
OpenTelemetry.tracer_provider.add_span_processor(processor)
log.info 'Console telemetry exporter configured'
true
rescue StandardError => e
handle_exception(e, level: :debug, operation: 'telemetry.configure_console')
false
end
|
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/legion/telemetry.rb', line 71
def configure_exporter
backend = tracing_settings[:exporter]&.to_sym || :none
case backend
when :otlp
configure_otlp
when :console
configure_console
end
end
|
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
|
# File 'lib/legion/telemetry.rb', line 100
def configure_otlp
require 'opentelemetry-exporter-otlp'
endpoint = tracing_settings[:endpoint] || 'http://localhost:4318/v1/traces'
= tracing_settings[:headers] || {}
exporter = OpenTelemetry::Exporter::OTLP::Exporter.new(
endpoint: endpoint,
headers:
)
processor = OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
exporter,
max_queue_size: 2048,
max_export_batch_size: tracing_settings[:batch_size] || 512
)
OpenTelemetry.tracer_provider.add_span_processor(processor)
log.info "OTLP exporter configured: #{endpoint}"
true
rescue LoadError
log.warn 'opentelemetry-exporter-otlp gem not available'
false
rescue StandardError => e
handle_exception(e, level: :warn, operation: 'telemetry.configure_otlp', endpoint: endpoint)
false
end
|
.enabled? ⇒ Boolean
22
23
24
25
26
27
|
# File 'lib/legion/telemetry.rb', line 22
def enabled?
defined?(OpenTelemetry::SDK) ? true : false
rescue StandardError => e
handle_exception(e, level: :debug, operation: 'telemetry.enabled')
false
end
|
.otel_available? ⇒ Boolean
14
15
16
17
18
19
20
|
# File 'lib/legion/telemetry.rb', line 14
def otel_available?
defined?(OpenTelemetry::Trace) &&
OpenTelemetry::Trace.current_span != OpenTelemetry::Trace::Span::INVALID
rescue StandardError => e
handle_exception(e, level: :debug, operation: 'telemetry.otel_available')
false
end
|
.otel_init_error?(error) ⇒ Boolean
93
94
95
96
97
98
|
# File 'lib/legion/telemetry.rb', line 93
def otel_init_error?(error)
error.message.include?('OpenTelemetry') || error.message.include?('tracer')
rescue StandardError => e
handle_exception(e, level: :debug, operation: 'telemetry.otel_init_error?')
false
end
|
.record_exception(span, exception) ⇒ Object
46
47
48
49
50
51
52
53
54
|
# File 'lib/legion/telemetry.rb', line 46
def record_exception(span, exception)
return unless span.respond_to?(:record_exception)
span.record_exception(exception)
span.status = OpenTelemetry::Trace::Status.error(exception.message)
rescue StandardError => e
handle_exception(e, level: :debug, operation: 'telemetry.record_exception')
nil
end
|
.sanitize_attributes(hash, max_keys: 20) ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
|
# File 'lib/legion/telemetry.rb', line 56
def sanitize_attributes(hash, max_keys: 20)
return {} unless hash.is_a?(Hash)
hash.first(max_keys).to_h do |k, v|
val = case v
when String, Integer, Float, TrueClass, FalseClass then v
else v.to_s
end
[k.to_s, val]
end
rescue StandardError => e
handle_exception(e, level: :debug, operation: 'telemetry.sanitize_attributes')
{}
end
|
.tracing_settings ⇒ Object
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/legion/telemetry.rb', line 82
def tracing_settings
telemetry = Legion::Settings[:telemetry]
return {} unless telemetry.is_a?(Hash)
tracing = telemetry[:tracing]
tracing.is_a?(Hash) ? tracing : {}
rescue StandardError => e
handle_exception(e, level: :debug, operation: 'telemetry.tracing_settings')
{}
end
|
.with_span(name, kind: :internal, attributes: {}) ⇒ Object
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/legion/telemetry.rb', line 29
def with_span(name, kind: :internal, attributes: {}, &)
unless enabled?
return yield(nil) if block_given?
return
end
log.debug { "[Telemetry] starting span=#{name} kind=#{kind}" }
tracer = OpenTelemetry.tracer_provider.tracer('legion', Legion::VERSION)
tracer.in_span(name, kind: kind, attributes: sanitize_attributes(attributes), &)
rescue StandardError => e
raise if block_given? && !otel_init_error?(e)
handle_exception(e, level: :debug, operation: 'telemetry.with_span', span_name: name, kind: kind)
yield(nil) if block_given?
end
|