Class: Highlight::H

Inherits:
Object
  • Object
show all
Defined in:
lib/highlight.rb

Constant Summary collapse

SDK_NAME =
'highlight-ruby'.freeze
OTLP_HTTP =
'https://otel.highlight.io:4318'.freeze
HIGHLIGHT_REQUEST_HEADER =
'X-Highlight-Request'.freeze
HIGHLIGHT_PROJECT_ATTRIBUTE =
'highlight.project_id'.freeze
HIGHLIGHT_SESSION_ATTRIBUTE =
'highlight.session_id'.freeze
HIGHLIGHT_TRACE_ATTRIBUTE =
'highlight.trace_id'.freeze
LOG_EVENT =
'log'.freeze
LOG_SEVERITY_ATTRIBUTE =
'log.severity'.freeze
LOG_MESSAGE_ATTRIBUTE =
'log.message'.freeze
CODE_FILEPATH =
OpenTelemetry::SemanticConventions::Trace::CODE_FILEPATH
CODE_LINENO =
OpenTelemetry::SemanticConventions::Trace::CODE_LINENO
CODE_FUNCTION =
OpenTelemetry::SemanticConventions::Trace::CODE_FUNCTION
DEPLOYMENT_ENVIRONMENT_ATTRIBUTE =
OpenTelemetry::SemanticConventions::Resource::DEPLOYMENT_ENVIRONMENT
HIGHLIGHT_SDK_VERSION_ATTRIBUTE =
'telemetry.distro.version'.freeze
HIGHLIGHT_SDK_NAME_ATTRIBUTE =
'telemetry.distro.name'.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_id, environment: '', otlp_endpoint: OTLP_HTTP, &block) ⇒ H

Returns a new instance of H.



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/highlight.rb', line 115

def initialize(project_id, environment: '', otlp_endpoint: OTLP_HTTP, &block)
  self.class.instance_variable_set(:@instance, self)

  @project_id = project_id
  @otlp_endpoint = otlp_endpoint
  @environment = environment

  configure_opentelemetry(&block)

  @tracer_provider = OpenTelemetry.tracer_provider
  @tracer = @tracer_provider.tracer('highlight-tracer')
end

Class Attribute Details

.instanceObject (readonly)

Returns the value of attribute instance.



88
89
90
# File 'lib/highlight.rb', line 88

def instance
  @instance
end

Class Method Details

.initialized?Boolean

Returns:

  • (Boolean)


91
92
93
# File 'lib/highlight.rb', line 91

def self.initialized?
  !@instance.nil?
end

.log_level_string(level) ⇒ Object



104
105
106
107
108
109
110
111
112
113
# File 'lib/highlight.rb', line 104

def self.log_level_string(level)
  case level
  when Logger::FATAL then 'FATAL'
  when Logger::ERROR then 'ERROR'
  when Logger::WARN then 'WARN'
  when Logger::INFO then 'INFO'
  when Logger::DEBUG then 'DEBUG'
  else 'UNKNOWN'
  end
end

.parse_headers(headers) ⇒ Object



95
96
97
98
99
100
101
102
# File 'lib/highlight.rb', line 95

def self.parse_headers(headers)
  return HighlightHeaders.new(nil, nil) if headers.nil? || !headers.key?(HIGHLIGHT_REQUEST_HEADER)

  session_id, request_id = headers[HIGHLIGHT_REQUEST_HEADER].split('/')
  traceparent = headers['traceparent']
  trace_id = traceparent&.split('-')&.[](1) || request_id
  HighlightHeaders.new(session_id, trace_id)
end

Instance Method Details

#flushObject



132
133
134
# File 'lib/highlight.rb', line 132

def flush
  @tracer_provider&.force_flush
end

#initialized?Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/highlight.rb', line 128

def initialized?
  !@tracer_provider.nil?
end

#record_exception(e, attrs = {}) ⇒ Object



154
155
156
157
158
# File 'lib/highlight.rb', line 154

def record_exception(e, attrs = {})
  return unless initialized?

  OpenTelemetry::Trace.current_span&.record_exception(e, attributes: attrs.transform_keys(&:to_s))
end

#record_log(session_id, request_id, level, message, attrs = {}) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/highlight.rb', line 160

def record_log(session_id, request_id, level, message, attrs = {})
  return unless initialized?

  log_attributes = create_log_attributes(level, message, attrs)

  @tracer.in_span(
    'highlight.log',
    attributes: {
      HIGHLIGHT_SESSION_ATTRIBUTE => session_id || '',
      HIGHLIGHT_TRACE_ATTRIBUTE => request_id || ''
    }.compact
  ) do |span|
    span.status = OpenTelemetry::Trace::Status.error(message) if [Logger::ERROR, Logger::FATAL].include?(level)
    span.add_event(LOG_EVENT, attributes: log_attributes)
  end
end

#shutdownObject



136
137
138
# File 'lib/highlight.rb', line 136

def shutdown
  @tracer_provider&.shutdown
end

#start_span(name, attrs = {}) ⇒ Object



148
149
150
151
152
# File 'lib/highlight.rb', line 148

def start_span(name, attrs = {})
  return unless initialized?

  @tracer.in_span(name, attributes: attrs.transform_keys(&:to_s)) { |span| yield(span) if block_given? }
end

#trace(session_id, request_id, attrs = {}, name: 'highlight.span', &block) ⇒ Object



140
141
142
143
144
145
146
# File 'lib/highlight.rb', line 140

def trace(session_id, request_id, attrs = {}, name: 'highlight.span', &block)
  return unless initialized?

  ctx = OpenTelemetry::Baggage.set_value(HIGHLIGHT_SESSION_ATTRIBUTE, session_id || '')
  ctx = OpenTelemetry::Baggage.set_value(HIGHLIGHT_TRACE_ATTRIBUTE, request_id || '', context: ctx)
  OpenTelemetry::Context.with_current(ctx) { start_span(name, attrs, &block) }
end