module Datadog::Tracing::Diagnostics::EnvironmentCollector

def agent_error(responses)

Returns:
  • (String) - concatenated list of transport errors
def agent_error(responses)
  error_responses = responses.reject(&:ok?)
  return nil if error_responses.empty?
  error_responses.map(&:inspect).join(',')
end

def agent_url

Returns:
  • (String, nil) - target agent URL for trace flushing
def agent_url
  # Retrieve the effect agent URL, regardless of how it was configured
  transport = Tracing.send(:tracer).writer.transport
  # return `nil` with IO transport
  return unless transport.respond_to?(:client)
  adapter = transport.client.api.adapter
  adapter.url
end

def analytics_enabled

Returns:
  • (Boolean, nil) - analytics enabled in configuration
def analytics_enabled
  !!Datadog.configuration.tracing.analytics.enabled
end

def collect_config!

def collect_config!
  {
    enabled: enabled,
    agent_url: agent_url,
    analytics_enabled: analytics_enabled,
    sample_rate: sample_rate,
    sampling_rules: sampling_rules,
    integrations_loaded: integrations_loaded,
    partial_flushing_enabled: partial_flushing_enabled,
    priority_sampling_enabled: priority_sampling_enabled,
    **instrumented_integrations_settings
  }
end

def collect_errors!(responses)

def collect_errors!(responses)
  {
    agent_error: agent_error(responses)
  }
end

def enabled

Returns:
  • (Boolean, nil) -
def enabled
  !!Datadog.configuration.tracing.enabled
end

def instrumented_integrations

def instrumented_integrations
  # `instrumented_integrations` method is added only if tracing contrib extensions are required.
  # If tracing is used in manual mode without integrations enabled this method does not exist.
  # CI visibility gem datadog-ci uses Datadog::Tracer without requiring datadog/tracing/contrib folder
  # nor enabling any integrations by default which causes EnvironmentCollector to fail.
  return {} unless Datadog.configuration.tracing.respond_to?(:instrumented_integrations)
  Datadog.configuration.tracing.instrumented_integrations
end

def instrumented_integrations_settings

Capture all active integration settings into "integrationName_settingName: value" entries.
def instrumented_integrations_settings
  instrumented_integrations.flat_map do |name, integration|
    integration.configuration.to_h.flat_map do |setting, value|
      next [] if setting == :tracer # Skip internal Ruby objects
      # Convert value to a string to avoid custom #to_json
      # handlers possibly causing errors.
      [[:"integration_#{name}_#{setting}", value.to_s]]
    end
  end.to_h
end

def integrations_loaded

Returns:
  • (String, nil) -
def integrations_loaded
  integrations = instrumented_integrations
  return if integrations.empty?
  integrations.map { |name, integration| "#{name}@#{integration.class.version}" }.join(',')
end

def partial_flushing_enabled

Returns:
  • (Boolean, nil) - partial flushing enabled in configuration
def partial_flushing_enabled
  !!Datadog.configuration.tracing.partial_flush.enabled
end

def priority_sampling_enabled

Returns:
  • (Boolean, nil) - priority sampling enabled in configuration
def priority_sampling_enabled
  !!Datadog.configuration.tracing.priority_sampling
end

def sample_rate

Returns:
  • (Numeric, nil) - tracer sample rate configured
def sample_rate
  sampler = Datadog.configuration.tracing.sampler
  return nil unless sampler
  sampler.sample_rate(nil) rescue nil
end

def sampling_rules

Returns:
  • (Hash, nil) - sample rules configured
def sampling_rules
  sampler = Datadog.configuration.tracing.sampler
  return nil unless sampler.is_a?(Tracing::Sampling::PrioritySampler) &&
    sampler.priority_sampler.is_a?(Tracing::Sampling::RuleSampler)
  sampler.priority_sampler.rules.map do |rule|
    next unless rule.is_a?(Tracing::Sampling::SimpleRule)
    {
      name: rule.matcher.name,
      service: rule.matcher.service,
      sample_rate: rule.sampler.sample_rate(nil)
    }
  end.compact
end