Class: SemanticLogger::Metric::Signalfx

Inherits:
Appender::Http show all
Defined in:
lib/semantic_logger/metric/signalfx.rb

Constant Summary collapse

END_POINT =
"v2/datapoint".freeze

Constants inherited from Appender::Http

Appender::Http::MAX_LOGGED_RESPONSE_BODY, Appender::Http::NOT_SENT_ERRORS, Appender::Http::URL_FORMAT_HINT

Constants inherited from Subscriber

Subscriber::MESSAGE_ERRORS

Instance Attribute Summary collapse

Attributes inherited from Appender::Http

#compress, #continue_timeout, #header, #http, #open_timeout, #path, #port, #proxy_url, #query, #read_timeout, #server, #ssl_options, #url, #username

Attributes inherited from Subscriber

#application, #environment, #formatter, #host, #logger, #metrics

Instance Method Summary collapse

Methods inherited from Appender::Http

#batch_all_or_nothing?, #batch_by_default?, #close, #fails_any_batch?, #inspect_details, #message_error?, #not_sent?, #redacted_url, #reopen, #wrote_nothing?

Methods inherited from Subscriber

#batch_all_or_nothing?, #batch_by_default?, #close, #console_output?, #console_stream, #default_formatter, #fails_any_batch?, #flush, #inspect, #inspect_details, #message_error?, #not_sent?, #rejected_entry, #wrote_nothing?

Constructor Details

#initialize(token:, dimensions: nil, url: "https://ingest.signalfx.com", formatter: nil, **args) ⇒ Signalfx

Create SignalFx metrics appender.

Parameters:

token: [String]
Access Token to use for sending metrics.
Obtain the Signalfx token via the Signalfx Web UI under `Organization` -> `Access Tokens`.

dimensions: [Array<Symbol|String>]
Dimensions to forward to signalfx when they are present, and not blank, in the named tags of
any log message whose metric has no dimensions of its own. A metric logged with `dimensions:`
is sent with those instead, so add the named tags it needs to them.
By default `application`, `host`, and, when it is set, `environment` are always included as
dimensions in all forwarded metrics.
Each value of a dimension makes a new metric time series, so list only named tags whose
values are bounded, never a user or request id (see docs/metrics.md).
Example: [:region, :plan]

filter: [Regexp|Proc]
RegExp: Only include log messages where the class name matches the supplied
        regular expression. All other messages will be ignored.
Proc: Only include log messages where the supplied Proc returns true.
      The Proc must return true or false.

host: [String]
Name of this host to send as a dimension.
Default: SemanticLogger.host

application: [String]
Name of this application to send as a dimension.
Default: SemanticLogger.application

url: [String]
Override the SignalFx service url. The metrics are posted to `v2/datapoint` under its path.
Default: https://ingest.signalfx.com

Notes:

When sending a metric to Signalfx, it is necessary to send both a gauge and a counter when a duration is included in the metric, otherwise it is not possible to chart counts of the metric. Unfortunately this doubles the number of metrics, but it is the way Signalfx works. Using a count of a gauge in a chart will significantly under-count the number of occurrences.

If dimensions are added to the metric, then the metric will be sent as-is and the above logic will not be applied. A dimension whose value is blank (nil, or text that is empty or only whitespace) is left out, and dimensions that are empty, such as {}, or all blank, such as {state: ""}, count as none.

Example, Counter metric:

logger.info(metric: 'Filters.count', dimensions: {plan: 'premium'})

Example, Counter metric with a count other than 1:

logger.info(metric: 'Filters.count', metric_amount: 23, dimensions: {plan: 'premium'})

When a duration is supplied and no dimensions are supplied:

logger.info(metric: 'Common/User/authorize', duration: 1.4)

Then it is translated into the following 2 log entries under the covers:

logger.info(metric: 'Application.average', metric_amount: 1.4,
          dimensions: {class: 'Common::User', action: 'authorize'})
logger.info(metric: 'Application.counter', metric_amount: 1,
          dimensions: {class: 'Common::User', action: 'authorize'})

Similarly with a measure block which automatically supplies the duration:

logger.measure_info(metric: 'Common/User/authorize') do
sleep 1
end


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/semantic_logger/metric/signalfx.rb', line 81

def initialize(token:,
               dimensions: nil,
               url: "https://ingest.signalfx.com",
               formatter: nil,
               **args,
               &)
  formatter ||= SemanticLogger::Formatters::Signalfx.new(token: token, dimensions: dimensions)

  super(url: url, formatter: formatter, **args, &)

  replace_header("X-SF-TOKEN", token)
  # Built from the parsed path, not the supplied url, so that a query string in the
  # url stays at the end of the request uri. See Appender::Http#uri_with_query.
  @full_path = "#{@path.end_with?('/') ? @path : "#{@path}/"}#{END_POINT}"
end

Instance Attribute Details

#full_path ⇒ Object (readonly)

Returns the value of attribute full_path.



11
12
13
# File 'lib/semantic_logger/metric/signalfx.rb', line 11

def full_path
  @full_path
end

Instance Method Details

#batch(logs) ⇒ Object

Logs in batches



104
105
106
107
108
# File 'lib/semantic_logger/metric/signalfx.rb', line 104

def batch(logs)
  message = format_batch(logs)
  logger.trace(message)
  post(message, full_path)
end

#batch_refusal ⇒ Object

The #batch above formats the whole batch with #format_batch, so it answers as Appender::Http#batch_refusal does for its own #batch: only while it is the one in use.



112
113
114
# File 'lib/semantic_logger/metric/signalfx.rb', line 112

def batch_refusal
  format_batch_refusal if batch_defined_by?(Signalfx)
end

#log(log) ⇒ Object



97
98
99
100
101
# File 'lib/semantic_logger/metric/signalfx.rb', line 97

def log(log)
  message = format_log(log)
  logger.trace(message)
  post(message, full_path)
end

#should_log?(log) ⇒ Boolean

Only forward log entries that contain metrics.

Returns:

  • (Boolean)


117
118
119
# File 'lib/semantic_logger/metric/signalfx.rb', line 117

def should_log?(log)
  log.metric && meets_log_level?(log) && !filtered?(log)
end