Class: Datadog::OpenFeature::Hooks::ExposureLoggingHook

Inherits:
Object
  • Object
show all
Includes:
OpenFeature::SDK::Hooks::Hook
Defined in:
lib/datadog/open_feature/hooks/exposure_logging_hook.rb

Overview

Makes the one exposure decision of an evaluation: records it in the exposure cache, sends new or changed exposures to Datadog, and stamps the verdict into the flag metadata as __dd_exposure_cache_hit for hooks that run after it.

It runs at the finally stage because an application after hook can still raise and turn the result into the default value. The SDK runs provider finally hooks before application ones, so ExposureHook callbacks see the final verdict.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(reporter, telemetry:, logger:) ⇒ ExposureLoggingHook

Returns a new instance of ExposureLoggingHook.



23
24
25
26
27
# File 'lib/datadog/open_feature/hooks/exposure_logging_hook.rb', line 23

def initialize(reporter, telemetry:, logger:)
  @reporter = reporter
  @telemetry = telemetry
  @logger = logger
end

Class Method Details

.available? ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/datadog/open_feature/hooks/exposure_logging_hook.rb', line 19

def self.available?
  !!defined?(::OpenFeature::SDK::Hooks::Hook)
end

Instance Method Details

#finally(hook_context:, evaluation_details:, **_opts) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/datadog/open_feature/hooks/exposure_logging_hook.rb', line 29

def finally(hook_context:, evaluation_details:, **_opts)
  return if evaluation_details.error_code || evaluation_details.reason == Ext::ERROR

  # The SDK may hand hooks frozen copies; the verdict goes into the details' own metadata.
   = evaluation_details.resolution_details[:flag_metadata]
  return unless .is_a?(Hash) && !.frozen?
  return unless [Ext::METADATA_DO_LOG] == true

  allocation_key = [Ext::METADATA_ALLOCATION_KEY]
  variant = evaluation_details.variant
  context = hook_context.evaluation_context
  return unless allocation_key.is_a?(String) && variant && context

  [Ext::METADATA_EXPOSURE_CACHE_HIT] = @reporter.record(
    flag_key: hook_context.flag_key,
    allocation_key: allocation_key,
    variant: variant,
    serial_id: [Ext::METADATA_SPLIT_SERIAL_ID],
    context: context,
    features: SplitFeatures.read(, Ext::METADATA_EXPOSURE_FEATURE_PREFIX),
  )
rescue => e
  # Exposure reporting must never change the evaluation result.
  @logger.debug { "OpenFeature: Failed to report exposure: #{e.class}: #{e.message}" }
  @telemetry.report(e, description: "OpenFeature: Failed to report exposure")
end