Hooks
Read time: 6 minutes
Last edited: May 30, 2024
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 define an "evaluation" series, which is composed of two stages: beforeEvaluation
and afterEvaluation
. When you include a hook in your SDK configuration, the SDK executes these two stages before and after the evaluation of a feature flag.
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
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
.
Go
Expand Go code sample
Java
Expand Java code sample
Node.js (server-side)
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 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
.
Python
Expand Python code sample
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
.
Ruby
Expand Ruby code sample
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
.
Client-side SDKs
This feature is available in the following SDKs: