Class: Tpt::Rails::DatadogFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/tpt/rails/datadog_factory.rb

Defined Under Namespace

Classes: DatadogFactoryNotConfigured

Constant Summary collapse

DEFAULT_STATSD_PORT =
8125
DEFAULT_TRACE_PORT =
8126

Class Method Summary collapse

Class Method Details

.configure_tracing(trace_url: nil, environment: nil, trace_tags: nil, trace_debug: false) ⇒ Object

@description: Configure tracing. Just passing trace_url: is enough for a basic config.

Parameters:

  • : (trace_url)

    The url to send traces to; if nil, tracing is disabled. Form: datadog://:[port]

  • : (trace_tags)

    Set your own trace tags. If missing, default tags are used (optionally using environment param).

  • : (trace_debug)

    boolean; passed to c.tracer enabled

  • : (&block)

    Pass to set custom configuration for tracing as part of a .configure call. Accepts one parameter that is the configuration object.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tpt/rails/datadog_factory.rb', line 65

def configure_tracing(trace_url: nil, environment:nil, trace_tags: nil, trace_debug: false)
  if !Tpt::Rails::configured?
    msg = "Tpt::Rails is not configured. Either run configure_tracing after configuration, or set a lamda for config.datadog_trace_setup which will defer setup."
    raise(DatadogFactoryNotConfigured.new(msg))
  end

  uri = URI.parse(trace_url)
  tags = { 'env' => to_env(environment) }
  tags["tpt_release_version"] = Tpt::Rails.release_version if Tpt::Rails.release_version

  ::Datadog.configure do |c|
    # This will activate auto-instrumentation for Rails
    c.use :rails, {
      analytics_enabled: true,
      service_name: Tpt::Rails.app_name,
    }

    c.tracer(
      enabled: true,
      debug: trace_debug,
      hostname: uri.host,
      port: uri.port || DEFAULT_TRACE_PORT,
      tags: tags,
    )

    c.logger.level = ::Logger::WARN

    yield(c) if block_given?
  end

  ::Datadog::Pipeline.before_flush(
    ::Datadog::Pipeline::SpanFilter.new { |span|
      span.resource =~ /TptRails::HealthChecksController/
    },
  )
end

.disable_tracingObject



102
103
104
# File 'lib/tpt/rails/datadog_factory.rb', line 102

def disable_tracing
  ::Datadog.configure { |c| c.tracer(enabled: false) }
end

.make(statsd_url: nil, environment: nil, statsd_tags: nil) ⇒ Datadog::Statsd

@description: Instantiates a datadog statsd instance. Note: has side effects, as it configures tracing.

Parameters:

  • : (statsd_url)

    The url to sends stats to; if nil, a dummy url is used. Form: statsd://:[port]

  • : (environment)

    Wrapped in “env:#environment” and added to tags; if nil, Tpt::Rails.app_env is used; If using eg. reporting env this is where you put it

  • : (statsd_tags)

    Set your own tags. If nil, default tags are used. If missing, no env tag is supplied, a default (optionally using environment param) is pushed into it. tpt-rails version is always pushed into it.

Returns:

  • (Datadog::Statsd)


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/tpt/rails/datadog_factory.rb', line 31

def make(statsd_url: nil, environment: nil, statsd_tags: nil)
  if !Tpt::Rails::configured?
    msg = "Tpt::Rails is not configured. Either run make after configuration, or set a lamda for config.datadog_statsd_setup which will defer setup."
    raise(DatadogFactoryNotConfigured.new(msg))
  end

  statsd_tags = Array.wrap(statsd_tags)

  # Fall back to a dummy client so callers don't have to worry about null checking the client
  url = statsd_url || 'statsd://localhost'
  uri = URI.parse(url)

  tags = statsd_tags.nil? ? [] : statsd_tags
  tags << "env:#{to_env(environment)}" unless tags.map { |t| t.split(':')[0] }.include?("env")
  tags << "tpt-rails_release_version:#{Tpt::Rails.release_version}" if Tpt::Rails.release_version

  ::Datadog::Statsd.new(
    uri.host,
    uri.port || DEFAULT_STATSD_PORT,
    namespace: Tpt::Rails.app_name,
    tags: tags,
  )
end

.make_dummyObject



55
56
57
# File 'lib/tpt/rails/datadog_factory.rb', line 55

def make_dummy
  make
end