Documentation Index
Fetch the complete documentation index at: https://launchdarkly-preview.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Overview
This topic explains how to define and add hooks to LaunchDarkly SDKs. Hooks are collections of developer-defined callbacks. They contain methods that the SDK executes at various points of interest. In LaunchDarkly SDKs, hooks provide entry points to observe or modify aspects of SDK operation. For example, when you enable OpenTelemetry in a server-side SDK, you add a tracing hook, provided by LaunchDarkly, that surfaces telemetry data. You might also write your own hook, for instance to support logging or reporting errors.Hooks stages
LaunchDarkly SDKs provide the following series of hooks: | Series | Stages| Execution |
|---|
beforeEvaluation , afterEvaluation
| When you include a hook in your SDK configuration, the SDK executes these two stages before and after you evaluate a feature flag . |
| Identify
| beforeIdentify , afterIdentify
| When you include a hook in your SDK configuration, the SDK executes these two stages before and after you identify a context . Only available for client-side SDKs. |
| Track
| afterTrack
| When you include a hook in your SDK configuration, the SDK executes this stage after you send a custom event . |
Not all SDKs support all of these series. In particular, the identify series is only available for client-side SDKs. Details about how to add hooks to each SDK are available in the SDK-specific sections below.
Server-side SDKs
This feature is available in the following SDKs:.NET (server-side)
Expand .NET (server-side) code sample
Expand .NET (server-side) code sample
To use hook functionality with the .NET (server-side) SDK, first import the To learn more, read
LaunchDarkly.Sdk.Server.Hooks namespace. Then, define a new hook. This class must derive from the Hook class and override its methods. Finally, reference the hook in the configuration options when you initialize the SDK client.Here’s how:Hooks.Go
Expand Go code sample
Expand Go code sample
Java
Expand Java code sample
Expand Java code sample
Node.js (server-side)
Expand Node.js (server-side) code sample
Expand Node.js (server-side) code sample
To use hook functionality with the Node.js (server-side) SDK, first define a new hook. This class must extend the You can also add a hook to an existing client using the To learn more, read
Hook interface. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:addHook method:Hook.Python
Expand Python code sample
Expand Python code sample
To use hook functionality with the Python SDK, first define a new hook. This class must inherit from You can also add a hook to an existing client using the To learn more, read
ldclient.hook.Hook. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:add_hook method:ldclient.hook.If you are working with forking processes, any configuration that you provide to the SDK must survive the forking process independently. We recommend that you add any hooks after a postfork() call, unless you are certain they can survive the forking process. To learn more, read Initialize the client and ldclient.postfork.Ruby
Expand Ruby code sample
Expand Ruby code sample
To use hook functionality with the Ruby SDK, first define a new hook. This class must include the You can also add a hook to an existing client using the To learn more, read
Hooks mixin. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:add_hook method:Hooks.If you are working with forking processes, any configuration that you provide to the SDK must survive the forking process independently. We recommend that you add any hooks after a postfork() call, unless you are certain they can survive the forking process. To learn more, read Initialize the client and postfork.Client-side SDKs
This feature is available in the following SDKs:Android
Expand Android code sample
Expand Android code sample
To use hook functionality with the Android SDK, first define a new hook. This class must extend the You can also add a hook to an existing client using the To learn more, read
Hook class. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:setHooks method:Hook and HooksConfigurationBuilder.iOS
Expand iOS code sample
Expand iOS code sample
Java
ScriptExpand JavaScript code sample
Expand JavaScript code sample
To use hook functionality with the JavaScript SDK, first define a new hook. This class must extend the You can also add a hook to an existing client using the To learn more, read
Hook interface. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:addHook method:Hook.React Native
Expand React Native code sample
Expand React Native code sample
To use hook functionality with the React Native SDK, first define a new hook. This class must extend the You can also add a hook to an existing client using the To learn more, read
Hook interface. Then, reference the hook in the configuration options when you initialize the SDK client.Here’s how:addHook method:Hook.