Module: ReactOnRailsPro::OpenTelemetry

Defined in:
lib/react_on_rails_pro/open_telemetry.rb,
sig/react_on_rails_pro/open_telemetry.rbs

Defined Under Namespace

Classes: ClientSpan

Constant Summary collapse

INSTRUMENTATION_NAME =

Returns:

  • (String)
"react_on_rails_pro"
SPAN_NAME =

Returns:

  • (String)
"react_on_rails_pro.renderer_http"
METHOD_ATTRIBUTE =

Returns:

  • (String)
"http.request.method"
PATH_ATTRIBUTE =

Returns:

  • (String)
"url.path"
REQUEST_SIZE_ATTRIBUTE =

Returns:

  • (String)
"http.request.body.size"
RESPONSE_SIZE_ATTRIBUTE =

Returns:

  • (String)
"http.response.body.size"
STATUS_ATTRIBUTE =

Returns:

  • (String)
"http.response.status_code"
TRACE_HEADER_NAMES =

Returns:

  • (Array[String])
%w[traceparent tracestate].freeze
RENDER_PATH_PATTERN =

Returns:

  • (Regexp)
%r{\A/bundles/[^/]+/([^/]+)/[0-9a-f]{32}\z}

Class Method Summary collapse

Class Method Details

.body_size(body) ⇒ Integer

Parameters:

  • body (Object)

Returns:

  • (Integer)


172
173
174
175
176
177
178
179
# File 'lib/react_on_rails_pro/open_telemetry.rb', line 172

def body_size(body)
  return 0 unless body

  size = body.bytesize if body.respond_to?(:bytesize)
  size.is_a?(Integer) ? size : 0
rescue StandardError
  0
end

.capture_contextObject?

Returns:

  • (Object, nil)


137
138
139
140
141
142
143
# File 'lib/react_on_rails_pro/open_telemetry.rb', line 137

def capture_context
  return unless defined?(::OpenTelemetry::Context)

  ::OpenTelemetry::Context.current
rescue StandardError
  nil
end

.start_client_span(method, path, parent_context:) ⇒ ClientSpan?

Parameters:

  • method (Symbol)
  • path (String)
  • parent_context: (Object, nil)

Returns:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/react_on_rails_pro/open_telemetry.rb', line 151

def start_client_span(method, path, parent_context:)
  provider = configured_tracer_provider
  return unless provider

  method_name = method.to_s.upcase
  span_path = request_path(path)
  span = provider.tracer(INSTRUMENTATION_NAME).start_span(
    SPAN_NAME,
    with_parent: parent_context,
    kind: :client,
    attributes: {
      METHOD_ATTRIBUTE => method_name,
      PATH_ATTRIBUTE => span_path
    }
  )
  context = ::OpenTelemetry::Trace.context_with_span(span)
  ClientSpan.new(span, context)
rescue StandardError
  nil
end

.with_context(context) { ... } ⇒ Object

Parameters:

  • context (Object, nil)

Yields:

Yield Returns:

  • (Object)

Returns:

  • (Object)


145
146
147
148
149
# File 'lib/react_on_rails_pro/open_telemetry.rb', line 145

def with_context(context, &)
  return yield unless context

  ::OpenTelemetry::Context.with_current(context, &)
end