Module: Epsagon

Defined in:
lib/epsagon.rb

Overview

Epsagon tracing main entry point

Constant Summary collapse

DEFAULT_BACKEND =
'opentelemetry.tc.epsagon.com:443/traces'
DEFAULT_IGNORE_DOMAINS =
['newrelic.com'].freeze
MUTABLE_CONF_KEYS =
Set.new([:metadata_only, :max_attribute_size, :ignore_domains, :ignored_keys])
@@epsagon_config =
nil

Class Method Summary collapse

Class Method Details

.add_ignored_key(key) ⇒ Object



62
63
64
# File 'lib/epsagon.rb', line 62

def add_ignored_key(key)
  get_config[:ignored_keys].push(key).uniq
end

.epsagon_confs(configurator) ⇒ Object

config opentelemetry with epsaon extensions:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/epsagon.rb', line 104

def epsagon_confs(configurator)
  otel_resource = {
    'application' => get_config[:app_name],
    'epsagon.version' => EpsagonConstants::VERSION,
    'epsagon.metadata_only' => get_config[:metadata_only]
  }.merge()

  configurator.resource = OpenTelemetry::SDK::Resources::Resource.telemetry_sdk.merge(
    OpenTelemetry::SDK::Resources::Resource.create(otel_resource)
  )

  configurator.use 'EpsagonSinatraInstrumentation', { epsagon: get_config }
  configurator.use 'EpsagonNetHTTPInstrumentation', { epsagon: get_config }
  configurator.use 'EpsagonFaradayInstrumentation', { epsagon: get_config }
  configurator.use 'EpsagonAwsSdkInstrumentation', { epsagon: get_config }
  configurator.use 'EpsagonRailsInstrumentation', { epsagon: get_config }
  configurator.use 'OpenTelemetry::Instrumentation::Sidekiq', { epsagon: get_config }
  configurator.use 'EpsagonPostgresInstrumentation', { epsagon: get_config }
  configurator.use 'EpsagonResqueInstrumentation', { epsagon: get_config }


  if get_config[:debug]
    configurator.add_span_processor OpenTelemetry::SDK::Trace::Export::SimpleSpanProcessor.new(
      OpenTelemetry::SDK::Trace::Export::ConsoleSpanExporter.new
    )
  end

  configurator.add_span_processor OpenTelemetry::SDK::Trace::Export::BatchSpanProcessor.new(
    exporter: OpenTelemetry::Exporter::OTLP::Exporter.new(headers: {
                                                            'x-epsagon-token' => get_config[:token]
                                                          },
                                                          endpoint: get_config[:backend],
                                                          insecure: get_config[:insecure] || false)
  )
end

.get_configObject



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/epsagon.rb', line 70

def get_config
  @@epsagon_config ||= {
    metadata_only: ENV['EPSAGON_METADATA']&.to_s&.downcase != 'false',
    debug: ENV['EPSAGON_DEBUG']&.to_s&.downcase == 'true',
    token: ENV['EPSAGON_TOKEN'] || '',
    app_name: ENV['EPSAGON_APP_NAME'] || '',
    max_attribute_size: ENV['EPSAGON_MAX_ATTRIBUTE_SIZE'] || 5000,
    backend: ENV['EPSAGON_BACKEND'] || DEFAULT_BACKEND,
    ignore_domains: ENV['EPSAGON_IGNORE_DOMAINS']&.split(',') || DEFAULT_IGNORE_DOMAINS,
    ignored_keys: ENV['EPSAGON_IGNORED_KEYS']&.split(',') || []
  }
end

.init(**args) ⇒ Object



35
36
37
38
39
40
# File 'lib/epsagon.rb', line 35

def init(**args)
  get_config.merge!(args)
  validate(get_config)
  OpenTelemetry::SDK.configure
  @@initialized = true
end

.remove_ignored_key(key) ⇒ Object



66
67
68
# File 'lib/epsagon.rb', line 66

def remove_ignored_key(key)
  get_config[:ignored_keys].delete(key)
end

.set_config(**args) ⇒ Object



52
53
54
55
56
57
58
59
60
# File 'lib/epsagon.rb', line 52

def set_config(**args)
  unless args.keys.all? {|a| MUTABLE_CONF_KEYS.include?(a)}
    raise ArgumentError.new("only #{MUTABLE_CONF_KEYS.to_a} are mutable after `Epsagon.init`")
  end
  Epsagon.init unless @@initialized
  new_conf = get_config.merge(args)
  validate(new_conf)
  @@epsagon_config = new_conf
end

.set_ecs_metadataObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/epsagon.rb', line 83

def 
   = ENV['ECS_CONTAINER_METADATA_URI']
  return {} if .nil?

  response = Net::HTTP.get(URI())
   = JSON.parse(response)
  arn = Arn.parse(['Labels']['com.amazonaws.ecs.task-arn'])

  {
    'aws.account_id' => arn.,
    'aws.region' => arn.region,
    'aws.ecs.cluster' => ['Labels']['com.amazonaws.ecs.cluster'],
    'aws.ecs.task_arn' => ['Labels']['com.amazonaws.ecs.task-arn'],
    'aws.ecs.container_name' => ['Labels']['com.amazonaws.ecs.container-name'],
    'aws.ecs.task.family' => ['Labels']['com.amazonaws.ecs.task-definition-family'],
    'aws.ecs.task.revision' => ['Labels']['com.amazonaws.ecs.task-definition-version']
  }
end

.validate(config) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/epsagon.rb', line 42

def validate(config)
  Util.validate_value(config, :metadata_only, 'Must be a boolean') {|v| !!v == v}
  Util.validate_value(config, :debug, 'Must be a boolean') {|v| !!v == v}
  Util.validate_value(config, :token, 'Must be a valid Epsagon token') {|v| (v.is_a? String) && (v.size > 10) }
  Util.validate_value(config, :app_name, 'Must be a String') {|v| (v.is_a? String) && (v.size > 0) }
  Util.validate_value(config, :max_attribute_size, 'Must be an Integer') {|v| v.is_a? Integer}
  Util.validate_value(config, :ignore_domains, 'Must be iterable') {|v| v.respond_to?(:each)}
  Util.validate_value(config, :ignored_keys, 'Must be iterable') {|v| v.respond_to?(:each)}
end