Class: LaunchDarklyObservability::Plugin

Inherits:
Object
  • Object
show all
Includes:
LaunchDarkly::Interfaces::Plugins::Plugin
Defined in:
lib/launchdarkly_observability/plugin.rb

Overview

LaunchDarkly SDK Plugin that provides observability instrumentation.

This plugin integrates with the LaunchDarkly Ruby SDK to automatically instrument flag evaluations with OpenTelemetry traces, logs, and metrics.

Examples:

Basic usage (SDK key and environment automatically extracted)

plugin = LaunchDarklyObservability::Plugin.new
config = LaunchDarkly::Config.new(plugins: [plugin])
client = LaunchDarkly::LDClient.new(ENV['LAUNCHDARKLY_SDK_KEY'], config)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_id: nil, sdk_key: nil, otlp_endpoint: DEFAULT_ENDPOINT, environment: nil, **options) ⇒ Plugin

Initialize a new observability plugin

Parameters:

  • project_id (String, nil) (defaults to: nil)

    LaunchDarkly project ID for routing telemetry. If not provided, the SDK key from the client will be used automatically.

  • sdk_key (String, nil) (defaults to: nil)

    LaunchDarkly SDK key (optional - will be extracted from client if not provided). The backend will derive the project and environment from the SDK key.

  • otlp_endpoint (String) (defaults to: DEFAULT_ENDPOINT)

    OTLP collector endpoint (default: LaunchDarkly’s endpoint)

  • environment (String, nil) (defaults to: nil)

    Deployment environment name (optional - inferred from SDK key by default). Only specify this for advanced scenarios like deployment-specific suffixes (e.g., ‘production-canary’).

  • options (Hash)

    Additional configuration options

Options Hash (**options):

  • :service_name (String)

    Service name for resource attributes

  • :service_version (String)

    Service version for resource attributes

  • :instrumentations (Hash)

    Configuration for OpenTelemetry auto-instrumentations

  • :enable_traces (Boolean)

    Enable trace instrumentation (default: true)

  • :enable_logs (Boolean)

    Enable log instrumentation (default: true)

  • :enable_metrics (Boolean)

    Enable metrics instrumentation (default: true)



56
57
58
59
60
61
62
63
64
# File 'lib/launchdarkly_observability/plugin.rb', line 56

def initialize(project_id: nil, sdk_key: nil, otlp_endpoint: DEFAULT_ENDPOINT, environment: nil, **options)
  @project_id = project_id || sdk_key
  @otlp_endpoint = otlp_endpoint
  @environment = environment&.to_s
  @options = default_options.merge(options)
  @hook = Hook.new
  @otel_config = nil
  @registered = false
end

Instance Attribute Details

#environmentString (readonly)

Returns The deployment environment.

Returns:

  • (String)

    The deployment environment



35
36
37
# File 'lib/launchdarkly_observability/plugin.rb', line 35

def environment
  @environment
end

#optionsHash (readonly)

Returns Additional options.

Returns:

  • (Hash)

    Additional options



38
39
40
# File 'lib/launchdarkly_observability/plugin.rb', line 38

def options
  @options
end

#otlp_endpointString (readonly)

Returns The OTLP endpoint URL.

Returns:

  • (String)

    The OTLP endpoint URL



32
33
34
# File 'lib/launchdarkly_observability/plugin.rb', line 32

def otlp_endpoint
  @otlp_endpoint
end

#project_idString (readonly)

Returns The LaunchDarkly project ID.

Returns:

  • (String)

    The LaunchDarkly project ID



29
30
31
# File 'lib/launchdarkly_observability/plugin.rb', line 29

def project_id
  @project_id
end

Instance Method Details

#flushObject

Flush all pending telemetry data



119
120
121
# File 'lib/launchdarkly_observability/plugin.rb', line 119

def flush
  @otel_config&.flush
end

#get_hooks(_environment_metadata) ⇒ Array<LaunchDarkly::Interfaces::Hooks::Hook>

Returns the hooks provided by this plugin

Parameters:

  • _environment_metadata (LaunchDarkly::Interfaces::Plugins::EnvironmentMetadata)

Returns:

  • (Array<LaunchDarkly::Interfaces::Hooks::Hook>)


77
78
79
# File 'lib/launchdarkly_observability/plugin.rb', line 77

def get_hooks()
  [@hook]
end

#metadataLaunchDarkly::Interfaces::Plugins::PluginMetadata

Returns metadata about this plugin

Returns:

  • (LaunchDarkly::Interfaces::Plugins::PluginMetadata)


69
70
71
# File 'lib/launchdarkly_observability/plugin.rb', line 69

def 
  LaunchDarkly::Interfaces::Plugins::PluginMetadata.new('launchdarkly-observability')
end

#register(_client, environment_metadata) ⇒ Object

Register the plugin with the LaunchDarkly client

This method is called during SDK initialization. It sets up the OpenTelemetry SDK with appropriate providers and exporters.

Parameters:

  • _client (LaunchDarkly::LDClient)

    The LaunchDarkly client instance

  • environment_metadata (LaunchDarkly::Interfaces::Plugins::EnvironmentMetadata)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/launchdarkly_observability/plugin.rb', line 88

def register(_client, )
  return if @registered

  # Use provided project_id, or extract SDK key from the client
  project_id = @project_id || &.sdk_key

  if project_id.nil? || project_id.empty?
    raise ArgumentError, 'Unable to determine project_id: no project_id or sdk_key provided, and client SDK key is unavailable'
  end

  @otel_config = OpenTelemetryConfig.new(
    project_id: project_id,
    otlp_endpoint: @otlp_endpoint,
    environment: @environment,
    sdk_metadata: &.sdk,
    **@options
  )

  @otel_config.configure

  @registered = true
end

#registered?Boolean

Check if the plugin has been registered

Returns:

  • (Boolean)


114
115
116
# File 'lib/launchdarkly_observability/plugin.rb', line 114

def registered?
  @registered
end

#shutdownObject

Shutdown the plugin and flush remaining data



124
125
126
127
# File 'lib/launchdarkly_observability/plugin.rb', line 124

def shutdown
  @otel_config&.shutdown
  @registered = false
end