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.
LaunchDarkly SDKs provide the following series of hooks:
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.
This feature is available in the following SDKs:
To use hook functionality with the .NET (server-side) SDK, first import the 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:
To learn more, read Hooks.
To use hook functionality with the Node.js (server-side) SDK, first define a new hook. This class must extend the Hook interface. Then, reference the hook in the configuration options when you initialize the SDK client.
Here’s how:
You can also add a hook to an existing client using the addHook method:
To learn more, read Hook.
To use hook functionality with the PHP SDK, first define a new hook. This class must extend the LaunchDarkly\Hooks\Hook abstract class and implement getMetadata. Then, reference the hook in the configuration options when you initialize the SDK client.
Here’s how:
You can also add a hook to an existing client using the addHook method:
To learn more, read the SDK API documentation.
To use hook functionality with the Python SDK, first define a new hook. This class must inherit from ldclient.hook.Hook. Then, reference the hook in the configuration options when you initialize the SDK client.
Here’s how:
You can also add a hook to an existing client using the add_hook method:
To learn more, read 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.
To use hook functionality with the Ruby SDK, first define a new hook. This class must include the Hooks mixin. Then, reference the hook in the configuration options when you initialize the SDK client.
Here’s how:
You can also add a hook to an existing client using the add_hook method:
To learn more, read 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.
This feature is available in the following SDKs:
To use hook functionality with the Android SDK, first define a new hook. This class must extend the Hook class. Then, reference the hook in the configuration options when you initialize the SDK client.
Here’s how:
You can also add a hook to an existing client using the setHooks method:
To learn more, read Hook and HooksConfigurationBuilder.
To use hook functionality with the JavaScript SDK, first define a new hook. This class must extend the Hook interface. Then, reference the hook in the configuration options when you initialize the SDK client.
Here’s how:
You can also add a hook to an existing client using the addHook method:
To learn more, read Hook.
To use hook functionality with the React Native SDK, first define a new hook. This class must extend the Hook interface. Then, reference the hook in the configuration options when you initialize the SDK client.
Here’s how:
You can also add a hook to an existing client using the addHook method:
To learn more, read Hook.