Module: ElasticAPM

Defined in:
lib/elastic_apm.rb,
lib/elastic_apm/grpc.rb,
lib/elastic_apm/span.rb,
lib/elastic_apm/util.rb,
lib/elastic_apm/agent.rb,
lib/elastic_apm/error.rb,
lib/elastic_apm/grape.rb,
lib/elastic_apm/rails.rb,
lib/elastic_apm/spies.rb,
lib/elastic_apm/config.rb,
lib/elastic_apm/fields.rb,
lib/elastic_apm/resque.rb,
lib/elastic_apm/context.rb,
lib/elastic_apm/graphql.rb,
lib/elastic_apm/logging.rb,
lib/elastic_apm/metrics.rb,
lib/elastic_apm/railtie.rb,
lib/elastic_apm/sinatra.rb,
lib/elastic_apm/version.rb,
lib/elastic_apm/metadata.rb,
lib/elastic_apm/spies/s3.rb,
lib/elastic_apm/error/log.rb,
lib/elastic_apm/metricset.rb,
lib/elastic_apm/spies/sns.rb,
lib/elastic_apm/spies/sqs.rb,
lib/elastic_apm/middleware.rb,
lib/elastic_apm/spies/http.rb,
lib/elastic_apm/spies/json.rb,
lib/elastic_apm/spies/rake.rb,
lib/elastic_apm/spies/tilt.rb,
lib/elastic_apm/sql/tokens.rb,
lib/elastic_apm/stacktrace.rb,
lib/elastic_apm/subscriber.rb,
lib/elastic_apm/metrics/set.rb,
lib/elastic_apm/normalizers.rb,
lib/elastic_apm/opentracing.rb,
lib/elastic_apm/spies/mongo.rb,
lib/elastic_apm/spies/redis.rb,
lib/elastic_apm/transaction.rb,
lib/elastic_apm/config/bytes.rb,
lib/elastic_apm/context/user.rb,
lib/elastic_apm/deprecations.rb,
lib/elastic_apm/instrumenter.rb,
lib/elastic_apm/span/context.rb,
lib/elastic_apm/span_helpers.rb,
lib/elastic_apm/spies/resque.rb,
lib/elastic_apm/spies/sequel.rb,
lib/elastic_apm/error_builder.rb,
lib/elastic_apm/spies/faraday.rb,
lib/elastic_apm/spies/sidekiq.rb,
lib/elastic_apm/spies/sinatra.rb,
lib/elastic_apm/sql/signature.rb,
lib/elastic_apm/sql/tokenizer.rb,
lib/elastic_apm/trace_context.rb,
lib/elastic_apm/util/deep_dup.rb,
lib/elastic_apm/util/throttle.rb,
lib/elastic_apm/central_config.rb,
lib/elastic_apm/config/options.rb,
lib/elastic_apm/internal_error.rb,
lib/elastic_apm/metrics/metric.rb,
lib/elastic_apm/metrics/vm_set.rb,
lib/elastic_apm/spies/net_http.rb,
lib/elastic_apm/spies/sneakers.rb,
lib/elastic_apm/transport/base.rb,
lib/elastic_apm/util/inflector.rb,
lib/elastic_apm/util/lru_cache.rb,
lib/elastic_apm/child_durations.rb,
lib/elastic_apm/config/duration.rb,
lib/elastic_apm/context/request.rb,
lib/elastic_apm/context_builder.rb,
lib/elastic_apm/error/exception.rb,
lib/elastic_apm/metrics/jvm_set.rb,
lib/elastic_apm/span/context/db.rb,
lib/elastic_apm/spies/dynamo_db.rb,
lib/elastic_apm/spies/shoryuken.rb,
lib/elastic_apm/context/response.rb,
lib/elastic_apm/naively_hashable.rb,
lib/elastic_apm/stacktrace/frame.rb,
lib/elastic_apm/transport/worker.rb,
lib/elastic_apm/span/context/http.rb,
lib/elastic_apm/spies/delayed_job.rb,
lib/elastic_apm/transport/filters.rb,
lib/elastic_apm/transport/headers.rb,
lib/elastic_apm/config/regexp_list.rb,
lib/elastic_apm/config/round_float.rb,
lib/elastic_apm/config/server_info.rb,
lib/elastic_apm/span/context/links.rb,
lib/elastic_apm/spies/sucker_punch.rb,
lib/elastic_apm/stacktrace_builder.rb,
lib/elastic_apm/context/request/url.rb,
lib/elastic_apm/metadata/cloud_info.rb,
lib/elastic_apm/metrics/cpu_mem_set.rb,
lib/elastic_apm/spies/elasticsearch.rb,
lib/elastic_apm/config/log_level_map.rb,
lib/elastic_apm/metadata/system_info.rb,
lib/elastic_apm/span/context/message.rb,
lib/elastic_apm/span/context/service.rb,
lib/elastic_apm/transport/connection.rb,
lib/elastic_apm/transport/user_agent.rb,
lib/elastic_apm/metadata/process_info.rb,
lib/elastic_apm/metadata/service_info.rb,
lib/elastic_apm/metrics/breakdown_set.rb,
lib/elastic_apm/spies/action_dispatch.rb,
lib/elastic_apm/transport/serializers.rb,
lib/elastic_apm/context/request/socket.rb,
lib/elastic_apm/metrics/span_scoped_set.rb,
lib/elastic_apm/metrics/transaction_set.rb,
lib/elastic_apm/span/context/destination.rb,
lib/elastic_apm/trace_context/tracestate.rb,
lib/elastic_apm/util/precision_validator.rb,
lib/elastic_apm/spies/azure_storage_table.rb,
lib/elastic_apm/trace_context/traceparent.rb,
lib/elastic_apm/transport/connection/http.rb,
lib/elastic_apm/central_config/cache_control.rb,
lib/elastic_apm/config/wildcard_pattern_list.rb,
lib/elastic_apm/normalizers/rails/action_view.rb,
lib/elastic_apm/normalizers/grape/endpoint_run.rb,
lib/elastic_apm/normalizers/rails/action_mailer.rb,
lib/elastic_apm/normalizers/rails/active_record.rb,
lib/elastic_apm/transport/connection/proxy_pipe.rb,
lib/elastic_apm/transport/filters/hash_sanitizer.rb,
lib/elastic_apm/transport/filters/secrets_filter.rb,
lib/elastic_apm/metadata/system_info/container_info.rb,
lib/elastic_apm/normalizers/rails/action_controller.rb,
lib/elastic_apm/transport/serializers/span_serializer.rb,
lib/elastic_apm/transport/serializers/error_serializer.rb,
lib/elastic_apm/transport/serializers/context_serializer.rb,
lib/elastic_apm/transport/serializers/metadata_serializer.rb,
lib/elastic_apm/transport/serializers/metricset_serializer.rb,
lib/elastic_apm/transport/serializers/transaction_serializer.rb

Overview

frozen_string_literal: true

Defined Under Namespace

Modules: ChildDurations, Deprecations, Fields, Grape, GraphQL, Logging, Metrics, NaivelyHashable, Normalizers, OpenTracing, Rails, Resque, Sinatra, SpanHelpers, Spies, Sql, Transport, Util Classes: Agent, CentralConfig, Config, Context, ContextBuilder, Error, ErrorBuilder, ExistingTransactionError, GRPC, Instrumenter, InternalError, Metadata, Metricset, Middleware, Railtie, Span, Stacktrace, StacktraceBuilder, Subscriber, TraceContext, Transaction

Constant Summary collapse

VERSION =
'4.8.0'

Class Method Summary collapse

Class Method Details

.add_filter(key, callback = nil) {|Hash| ... } ⇒ Bool

Provide a filter to transform payloads before sending them off

Yields:

  • (Hash)

    A filter. Used if provided. Otherwise using ‘callback`



404
405
406
407
408
409
410
# File 'lib/elastic_apm.rb', line 404

def add_filter(key, callback = nil, &block)
  if callback.nil? && !block
    raise ArgumentError, '#add_filter needs either `callback\' or a block'
  end

  agent&.add_filter(key, block || callback)
end

.agentAgent



85
86
87
# File 'lib/elastic_apm.rb', line 85

def agent
  Agent.instance
end

.build_context(rack_env: nil, for_type: :transaction) ⇒ Context

Build a [Context] from a Rack ‘env`. The context may include information about the request, response, current user and more



319
320
321
322
323
324
# File 'lib/elastic_apm.rb', line 319

def build_context(
  rack_env: nil,
  for_type: :transaction
)
  agent&.build_context(rack_env: rack_env, for_type: for_type)
end

.current_spanSpan

Returns the currently active span (if any)



101
102
103
# File 'lib/elastic_apm.rb', line 101

def current_span
  agent&.current_span
end

.current_transactionTransaction

Returns the currently active transaction (if any)



94
95
96
# File 'lib/elastic_apm.rb', line 94

def current_transaction
  agent&.current_transaction
end

.end_span(span = nil) ⇒ Span

Ends the current span



251
252
253
# File 'lib/elastic_apm.rb', line 251

def end_span(span = nil)
  agent&.end_span(span)
end

.end_transaction(result = nil) ⇒ Transaction

Ends the current transaction with ‘result`



154
155
156
# File 'lib/elastic_apm.rb', line 154

def end_transaction(result = nil)
  agent&.end_transaction(result)
end

.log_ids {|String|nil, String|nil, String|nil| ... } ⇒ String

Get a formatted string containing transaction, span, and trace ids. If a block is provided, the ids are yielded.

and trace ids. rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity

Yields:

  • (String|nil, String|nil, String|nil)

    The transaction, span,



112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/elastic_apm.rb', line 112

def log_ids
  trace_id = (current_transaction || current_span)&.trace_id
  if block_given?
    return yield(current_transaction&.id, current_span&.id, trace_id)
  end

  ids = []
  ids << "transaction.id=#{current_transaction.id}" if current_transaction
  ids << "span.id=#{current_span.id}" if current_span
  ids << "trace.id=#{trace_id}" if trace_id
  ids.join(' ')
end

.report(exception, context: nil, handled: true) ⇒ String

Report and exception to APM



334
335
336
# File 'lib/elastic_apm.rb', line 334

def report(exception, context: nil, handled: true)
  agent&.report(exception, context: context, handled: handled)
end

.report_message(message, context: nil, **attrs) ⇒ String

Report a custom string error message to APM



343
344
345
346
347
348
349
350
# File 'lib/elastic_apm.rb', line 343

def report_message(message, context: nil, **attrs)
  agent&.report_message(
    message,
    context: context,
    backtrace: caller,
    **attrs
  )
end

.restart(config = nil) ⇒ Object

Restarts the ElasticAPM Agent using the same config or a new one, if it is provided. Starts the agent if it is not running. Stops and starts the agent if it is running.



73
74
75
76
77
# File 'lib/elastic_apm.rb', line 73

def restart(config = nil)
  config ||= agent&.config
  stop if running?
  start(config)
end

.running?Boolean



80
81
82
# File 'lib/elastic_apm.rb', line 80

def running?
  Agent.running?
end

.set_custom_context(custom) ⇒ Hash

Provide further context for the current transaction



376
377
378
# File 'lib/elastic_apm.rb', line 376

def set_custom_context(custom)
  agent&.set_custom_context(custom)
end

.set_destination(address: nil, port: nil, service: nil, cloud: nil) ⇒ Object

Set destination fields on the current span



394
395
396
# File 'lib/elastic_apm.rb', line 394

def set_destination(address: nil, port: nil, service: nil, cloud: nil)
  agent&.set_destination(address: address, port: port, service: service, cloud: cloud)
end

.set_label(key, value) ⇒ Object

Set a label value for the current transaction



359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/elastic_apm.rb', line 359

def set_label(key, value)
  case value
  when TrueClass,
       FalseClass,
       Numeric,
       NilClass,
       String
    agent&.set_label(key, value)
  else
    agent&.set_label(key, value.to_s)
  end
end

.set_user(user) ⇒ Object

Provide a user to the current transaction



384
385
386
# File 'lib/elastic_apm.rb', line 384

def set_user(user)
  agent&.set_user(user)
end

.start(config = {}) ⇒ Agent

Starts the ElasticAPM Agent



60
61
62
# File 'lib/elastic_apm.rb', line 60

def start(config = {})
  Agent.start config
end

.start_span(name, type = nil, subtype: nil, action: nil, context: nil, include_stacktrace: true, trace_context: nil, parent: nil, sync: nil) ⇒ Span

rubocop:disable Metrics/ParameterLists Start a new span



216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/elastic_apm.rb', line 216

def start_span(
  name,
  type = nil,
  subtype: nil,
  action: nil,
  context: nil,
  include_stacktrace: true,
  trace_context: nil,
  parent: nil,
  sync: nil
)
  agent&.start_span(
    name,
    type,
    subtype: subtype,
    action: action,
    context: context,
    trace_context: trace_context,
    parent: parent,
    sync: sync
  ).tap do |span|
    break unless span && include_stacktrace
    break unless agent.config.span_frames_min_duration?

    span.original_backtrace ||= caller
  end
end

.start_transaction(name = nil, type = nil, context: nil, trace_context: nil) ⇒ Transaction

Start a new transaction

‘ExamplesController#index` `db.mysql2.query`



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/elastic_apm.rb', line 136

def start_transaction(
  name = nil,
  type = nil,
  context: nil,
  trace_context: nil
)
  agent&.start_transaction(
    name,
    type,
    context: context,
    trace_context: trace_context
  )
end

.stopObject

Stops the ElasticAPM Agent



65
66
67
# File 'lib/elastic_apm.rb', line 65

def stop
  Agent.stop
end

.with_span(name, type = nil, subtype: nil, action: nil, context: nil, include_stacktrace: true, trace_context: nil, parent: nil, sync: nil) {|Span| ... } ⇒ Object

rubocop:disable Metrics/ParameterLists Wrap a block in a Span, ending it after the block

Yields:



271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/elastic_apm.rb', line 271

def with_span(
  name,
  type = nil,
  subtype: nil,
  action: nil,
  context: nil,
  include_stacktrace: true,
  trace_context: nil,
  parent: nil,
  sync: nil
)
  unless block_given?
    raise ArgumentError,
      'expected a block. Do you want `start_span\' instead?'
  end

  return yield nil unless agent

  begin
    span =
      start_span(
        name,
        type,
        subtype: subtype,
        action: action,
        context: context,
        include_stacktrace: include_stacktrace,
        trace_context: trace_context,
        parent: parent,
        sync: sync
      )
    result = yield span
    span&.outcome ||= Span::Outcome::SUCCESS
    result
  rescue
    span&.outcome ||= Span::Outcome::FAILURE
    raise
  ensure
    end_span
  end
end

.with_transaction(name = nil, type = nil, context: nil, trace_context: nil) {|Transaction| ... } ⇒ Object

Wrap a block in a Transaction, ending it after the block

‘ExamplesController#index` `db.mysql2.query`

Yields:



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/elastic_apm.rb', line 169

def with_transaction(
  name = nil,
  type = nil,
  context: nil,
  trace_context: nil
)
  unless block_given?
    raise ArgumentError,
      'expected a block. Do you want `start_transaction\' instead?'
  end

  return yield(nil) unless agent

  begin
    transaction =
      start_transaction(
        name,
        type,
        context: context,
        trace_context: trace_context
      )
    result = yield transaction
    transaction&.outcome ||= Transaction::Outcome::SUCCESS
    result
  rescue
    transaction&.outcome ||= Transaction::Outcome::FAILURE
    raise
  ensure
    end_transaction
  end
end