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/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/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/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/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/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, 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.0.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

Parameters:

  • key (Symbol)

    Unique filter key

  • callback (Object, Proc) (defaults to: nil)

    A filter that responds to #call(payload)

Yields:

  • (Hash)

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

Returns:

  • (Bool)

    true



399
400
401
402
403
404
405
# File 'lib/elastic_apm.rb', line 399

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

Returns Currently running [Agent] if any.

Returns:

  • (Agent)

    Currently running [Agent] if any



83
84
85
# File 'lib/elastic_apm.rb', line 83

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

Parameters:

  • rack_env (Rack::Env) (defaults to: nil)

    A Rack env

Returns:



314
315
316
317
318
319
# File 'lib/elastic_apm.rb', line 314

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)

Returns:

  • (Span)

    or ‘nil`



99
100
101
# File 'lib/elastic_apm.rb', line 99

def current_span
  agent&.current_span
end

.current_transactionTransaction

Returns the currently active transaction (if any)

Returns:



92
93
94
# File 'lib/elastic_apm.rb', line 92

def current_transaction
  agent&.current_transaction
end

.end_spanSpan

Ends the current span

Returns:



246
247
248
# File 'lib/elastic_apm.rb', line 246

def end_span
  agent&.end_span
end

.end_transaction(result = nil) ⇒ Transaction

Ends the current transaction with ‘result`

Parameters:

  • result (String) (defaults to: nil)

    The result of the transaction

Returns:



152
153
154
# File 'lib/elastic_apm.rb', line 152

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,

Returns:

  • (String)

    Unless block given



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

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

Parameters:

  • exception (Exception)

    The exception

  • context (Context) (defaults to: nil)

    An optional [Context]

  • handled (Boolean) (defaults to: true)

    Whether the exception was rescued

Returns:

  • (String)

    ID of the generated [Error]



329
330
331
# File 'lib/elastic_apm.rb', line 329

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

Parameters:

  • message (String)

    The message

  • context (Context) (defaults to: nil)

    An optional [Context]

Returns:

  • (String)

    ID of the generated [Error]



338
339
340
341
342
343
344
345
# File 'lib/elastic_apm.rb', line 338

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.



71
72
73
74
75
# File 'lib/elastic_apm.rb', line 71

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

.running?Boolean

Returns Whether there’s an [Agent] running.

Returns:

  • (Boolean)

    Whether there’s an [Agent] running



78
79
80
# File 'lib/elastic_apm.rb', line 78

def running?
  Agent.running?
end

.set_custom_context(custom) ⇒ Hash

Provide further context for the current transaction

Parameters:

  • custom (Hash)

    A hash with custom information. Can be nested.

Returns:

  • (Hash)

    The current custom context



371
372
373
# File 'lib/elastic_apm.rb', line 371

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

Parameters:

  • address (String) (defaults to: nil)

    Destination address

  • address (String) (defaults to: nil)

    Destination address

  • address (Hash) (defaults to: nil)

    Destination service

  • address (Hash) (defaults to: nil)

    Destination cloud



389
390
391
# File 'lib/elastic_apm.rb', line 389

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

Parameters:

  • key (String, Symbol)

    A key

  • value (Object)

    A value

Returns:

  • (Object)

    The given value



354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/elastic_apm.rb', line 354

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

Parameters:

  • user (Object)

    An object representing a user

Returns:

  • (Object)

    Given user



379
380
381
# File 'lib/elastic_apm.rb', line 379

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

.start(config = {}) ⇒ Agent

Starts the ElasticAPM Agent

Parameters:

  • config (Config) (defaults to: {})

    An instance of Config

Returns:

  • (Agent)

    The resulting [Agent]



58
59
60
# File 'lib/elastic_apm.rb', line 58

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

Parameters:

  • name (String)

    A description of the span, eq ‘SELECT FROM “users”`

  • type (String) (defaults to: nil)

    The span type, eq ‘db`

  • subtype (String) (defaults to: nil)

    The span subtype, eq ‘postgresql`

  • action (String) (defaults to: nil)

    The span action type, eq ‘connect` or `query`

  • context (Span::Context) (defaults to: nil)

    Context information about the span

  • include_stacktrace (Boolean) (defaults to: true)

    Whether or not to capture a stacktrace

  • trace_context (TraceContext) (defaults to: nil)

    An optional [TraceContext] object for Distributed Tracing.

  • parent (Transaction, Span) (defaults to: nil)

    The parent transaction or span. Relevant when the span is created in another thread.

  • sync (Boolean) (defaults to: nil)

    Whether the span is created synchronously or not.

Returns:



214
215
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
# File 'lib/elastic_apm.rb', line 214

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`

Parameters:

  • name (String) (defaults to: nil)

    A description of the transaction, eg

  • type (String) (defaults to: nil)

    The kind of the transaction, eg ‘app.request.get` or

  • context (Context) (defaults to: nil)

    An optional [Context]

  • trace_context (TraceContext) (defaults to: nil)

    An optional [TraceContext] object for Distributed Tracing.

Returns:



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

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



63
64
65
# File 'lib/elastic_apm.rb', line 63

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

Parameters:

  • name (String)

    A description of the span, eq ‘SELECT FROM “users”`

  • type (String) (defaults to: nil)

    The kind of span, eq ‘db`

  • subtype (String) (defaults to: nil)

    The subtype of span eg. ‘postgresql`.

  • action (String) (defaults to: nil)

    The action type of span eg. ‘connect` or `query`.

  • context (Span::Context) (defaults to: nil)

    Context information about the span

  • include_stacktrace (Boolean) (defaults to: true)

    Whether or not to capture a stacktrace

  • trace_context (TraceContext) (defaults to: nil)

    An optional [TraceContext] object for Distributed Tracing.

  • parent (Transaction, Span) (defaults to: nil)

    The parent transaction or span. Relevant when the span is created in another thread.

  • sync (Boolean) (defaults to: nil)

    Whether the span is created synchronously or not.

Yields:

Returns:

  • Result of block



266
267
268
269
270
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
# File 'lib/elastic_apm.rb', line 266

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`

Parameters:

  • name (String) (defaults to: nil)

    A description of the transaction, eg

  • type (String) (defaults to: nil)

    The kind of the transaction, eg ‘app.request.get` or

  • context (Context) (defaults to: nil)

    An optional [Context]

  • trace_context (TraceContext) (defaults to: nil)

    An optional [TraceContext] object for Distributed Tracing.

Yields:

Returns:

  • result of block



167
168
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
# File 'lib/elastic_apm.rb', line 167

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