Class: OpenTelemetry::Instrumentation::Rage::Handlers::Cable

Inherits:
Rage::Telemetry::Handler
  • Object
show all
Defined in:
lib/opentelemetry/instrumentation/rage/handlers/cable.rb

Overview

The class customizes the initial Rack span used for WebSocket handshakes and wraps subsequent Cable connection and action processing in spans linked to the handshake span.

Class Method Summary collapse

Class Method Details

.create_broadcast_span(stream:) ⇒ Object

Parameters:

  • stream (String)

    the name of the stream



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/opentelemetry/instrumentation/rage/handlers/cable.rb', line 123

def self.create_broadcast_span(stream:)
  attributes = {
    "rage.cable.broadcast.stream" => stream
  }

  Rage::Instrumentation.instance.tracer.in_span("Rage::Cable broadcast", kind: :producer, attributes:) do |span|
    result = yield

    if result.error?
      span.record_exception(result.exception)
      span.status = OpenTelemetry::Trace::Status.error
    end
  end
end

.create_channel_span(env:, action:, channel:) ⇒ Object

Parameters:

  • env (Hash)

    the Rack env

  • action (Symbol)

    the name of the action being processed

  • channel (Rage::Cable::Channel)

    the channel instance



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/opentelemetry/instrumentation/rage/handlers/cable.rb', line 81

def self.create_channel_span(env:, action:, channel:)
  handshake_context = env[HANDSHAKE_CONTEXT]
  handshake_link = env[HANDSHAKE_LINK]

  OpenTelemetry::Context.with_current(handshake_context) do
    attributes = {
      "rpc.system" => "rage.cable",
      "rpc.service" => channel.class.name,
      "rpc.method" => action.to_s
    }

    span_name = if action == :subscribed
      "#{channel.class} subscribe"
    elsif action == :unsubscribed
      "#{channel.class} unsubscribe"
    else
      "#{channel.class} #{action}"
    end

    kind = (action == :unsubscribed) ? :internal : :server

    span = Rage::Instrumentation.instance.tracer.start_root_span(
      span_name,
      links: handshake_link,
      kind:,
      attributes:
    )

    OpenTelemetry::Trace.with_span(span) do
      result = yield

      if result.error?
        span.record_exception(result.exception)
        span.status = OpenTelemetry::Trace::Status.error
      end
    ensure
      span.finish
    end
  end
end

.create_connection_span(env:, action:, connection:) ⇒ Object

Parameters:

  • env (Hash)

    the Rack env

  • action (:connect, :disconnect)

    the name of the action being processed

  • connection (Rage::Cable::Connection)

    the connection instance



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/opentelemetry/instrumentation/rage/handlers/cable.rb', line 46

def self.create_connection_span(env:, action:, connection:)
  handshake_context = env[HANDSHAKE_CONTEXT]
  handshake_link = env[HANDSHAKE_LINK]

  OpenTelemetry::Context.with_current(handshake_context) do
    attributes = {
      "rage.cable.connection.class" => connection.class.name,
      "rage.cable.connection.action" => action.to_s
    }

    kind = (action == :connect) ? :server : :internal

    span = Rage::Instrumentation.instance.tracer.start_root_span(
      "#{connection.class} #{action}",
      links: handshake_link,
      kind:,
      attributes:
    )

    OpenTelemetry::Trace.with_span(span) do
      result = yield

      if result.error?
        span.record_exception(result.exception)
        span.status = OpenTelemetry::Trace::Status.error
      end
    ensure
      span.finish
    end
  end
end

.save_context(env:) ⇒ Object

Parameters:

  • env (Hash)

    the Rack env



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/opentelemetry/instrumentation/rage/handlers/cable.rb', line 30

def self.save_context(env:)
  span = OpenTelemetry::Instrumentation::Rack.current_span
  return yield unless span.recording?

  request = ::Rack::Request.new(env)
  span.name = "#{request.request_method} #{request.path}"

  env[HANDSHAKE_CONTEXT] = OpenTelemetry::Context.current
  env[HANDSHAKE_LINK] = [OpenTelemetry::Trace::Link.new(span.context)]

  yield
end