Class: Brut::Instrumentation::OpenTelemetry
- Inherits:
-
Object
- Object
- Brut::Instrumentation::OpenTelemetry
- Defined in:
- lib/brut/instrumentation/open_telemetry.rb
Overview
Class to interact with the OpenTelemetry standard in a simpler way than
the provided Ruby gem does. In general, you should use this class
via Brut.container.instrumentation
, and you should not use the
OpenTelemetry ruby library directly. You probably wouldn't want to, anyway.
Defined Under Namespace
Classes: NormalizedAttributes, Span
Instance Method Summary collapse
-
#add_attributes(attributes) ⇒ Object
Adds attributes to the span, converting the hash or keyword arguments to strings.
-
#add_event(name, **attributes) ⇒ Object
Adds an event to the current span.
-
#add_prefixed_attributes(prefix, attributes) ⇒ Object
Adds attributes to the span, prefixing each key with the given prefix, then converting the hash or keyword arguments to strings.
-
#record_and_reraise_exception!(ex, attributes = nil) ⇒ Object
Record an exception and re-raise it.
-
#record_exception(ex, attributes = nil) ⇒ Object
Record an exception.
-
#span(name, **attributes) {|Brut::Instrumentation::OpenTelemetry::Span| ... } ⇒ Object
Create a span around the given block of code.
Instance Method Details
#add_attributes(attributes) ⇒ Object
Adds attributes to the span, converting the hash or keyword arguments to strings. This will use the app's Otel prefix for all attributes, so you do not have to prefix them. If you need to set standard attributes, you should use #add_prefixed_attributes instead.
76 77 78 79 |
# File 'lib/brut/instrumentation/open_telemetry.rb', line 76 def add_attributes(attributes) current_span = OpenTelemetry::Trace.current_span current_span.add_attributes(NormalizedAttributes.new(:detect,attributes).to_h) end |
#add_event(name, **attributes) ⇒ Object
Adds an event to the current span
37 38 39 40 41 42 43 44 |
# File 'lib/brut/instrumentation/open_telemetry.rb', line 37 def add_event(name,**attributes) explicit_attributes = attributes.delete(:attributes) || {} = attributes.delete(:timestamp) current_span = OpenTelemetry::Trace.current_span current_span.add_event(name, attributes: NormalizedAttributes.new(nil,attributes.merge(explicit_attributes)).to_h, timestamp:) end |
#add_prefixed_attributes(prefix, attributes) ⇒ Object
Adds attributes to the span, prefixing each key with the given prefix, then converting the hash or keyword arguments to strings. For example, if the prefix is 'my_app' and you add the attributes 'type' and 'reason', the actual attribute names will be 'my_app.type' and 'my_app.reason'.
84 85 86 87 88 89 |
# File 'lib/brut/instrumentation/open_telemetry.rb', line 84 def add_prefixed_attributes(prefix,attributes) current_span = OpenTelemetry::Trace.current_span current_span.add_attributes( NormalizedAttributes.new(prefix,attributes).to_h ) end |
#record_and_reraise_exception!(ex, attributes = nil) ⇒ Object
Record an exception and re-raise it. This is useful if you want the exception recorded as part of the parent span, but still plan to let it raise. Don't do this for every exception you intend to raise.
66 67 68 69 |
# File 'lib/brut/instrumentation/open_telemetry.rb', line 66 def record_and_reraise_exception!(ex,attributes=nil) reecord_exception(ex,attributes) raise ex end |
#record_exception(ex, attributes = nil) ⇒ Object
Record an exception. In general, use this only if:
- You need to have the parent span record this particular exception
- You are not going to re-raise the exception.
Otherwise, look at #record_and_reraise_exception!.
55 56 57 58 |
# File 'lib/brut/instrumentation/open_telemetry.rb', line 55 def record_exception(ex,attributes=nil) current_span = OpenTelemetry::Trace.current_span current_span.record_exception(ex,attributes: NormalizedAttributes.new(nil,attributes).to_h) end |
#span(name, **attributes) {|Brut::Instrumentation::OpenTelemetry::Span| ... } ⇒ Object
Create a span around the given block of code.
example, you could call this the method name, but should not include parameters in the name.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/brut/instrumentation/open_telemetry.rb', line 19 def span(name,**attributes,&block) result = nil normalized_attributes = NormalizedAttributes.new(:detect,attributes).to_h Brut.container.tracer.in_span(name, attributes: normalized_attributes) do |span| wrapped_span = Span.new(span) begin result = block.(wrapped_span) rescue => ex span.record_exception(ex) raise end end result end |