Class: Datadog::OpenFeature::Metrics::FlagEvalMetrics

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/open_feature/metrics/flag_eval_metrics.rb

Overview

Records flag evaluation metrics via OpenTelemetry

Constant Summary collapse

METER_NAME =
"ddtrace.openfeature"
METRIC_NAME =
"feature_flag.evaluations"
METRIC_UNIT =
"{evaluation}"
METRIC_DESCRIPTION =
"Number of feature flag evaluations"
ATTR_FLAG_KEY =
"feature_flag.key"
ATTR_VARIANT =
"feature_flag.result.variant"
ATTR_REASON =
"feature_flag.result.reason"
ATTR_ALLOCATION_KEY =
"feature_flag.result.allocation_key"
ATTR_ERROR_TYPE =
"error.type"
DEFAULT_ERROR_TYPE =
"general"
ERROR_TYPE_MAP =
{
  "FLAG_NOT_FOUND" => "flag_not_found",
  "TYPE_MISMATCH" => "type_mismatch",
  "PARSE_ERROR" => "parse_error",
  "PROVIDER_NOT_READY" => "provider_not_ready",
  "TARGETING_KEY_MISSING" => "targeting_key_missing",
  "INVALID_CONTEXT" => "invalid_context",
  "PROVIDER_FATAL" => "provider_fatal",
  "GENERAL" => DEFAULT_ERROR_TYPE,
}.freeze
EXCLUDE_ALLOCATION_KEY_REASONS =

Reasons that should not include allocation_key in metrics

%w[error default disabled].freeze

Instance Method Summary collapse

Constructor Details

#initialize(telemetry:, logger:) ⇒ FlagEvalMetrics

Returns a new instance of FlagEvalMetrics.



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/datadog/open_feature/metrics/flag_eval_metrics.rb', line 35

def initialize(telemetry:, logger:)
  @telemetry = telemetry
  @logger = logger
  @enabled = Datadog.configuration.opentelemetry.metrics.enabled
  @counter = nil
  @mutex = Mutex.new

  unless @enabled
    @logger.debug { "OpenFeature: OTel metrics not enabled (DD_METRICS_OTEL_ENABLED=false), flag evaluation metrics disabled" }
  end
end

Instance Method Details

#record(flag_key, variant:, reason:, error_code: nil, allocation_key: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/datadog/open_feature/metrics/flag_eval_metrics.rb', line 47

def record(flag_key, variant:, reason:, error_code: nil, allocation_key: nil)
  return unless @enabled

  counter = get_or_create_counter
  return unless counter

  attributes = build_attributes(
    flag_key,
    variant: variant,
    reason: reason,
    error_code: error_code,
    allocation_key: allocation_key,
  )
  counter.add(1, attributes: attributes)
rescue => e
  @logger.debug { "OpenFeature: Failed to record evaluation metric: #{e.class}: #{e.message}" }
  @telemetry.report(e, description: "OpenFeature: Failed to record evaluation metric")
end