This is an Instrumentation Library for .NET, providing OpenTelemetry (OTEL) metrics and traces for LDAP client operations via the TraceableLdapConnection
implementation. To enable instrumentation, you must use TraceableLdapConnection
instead of the standard LdapConnection
.
TraceableLdapConnection
is a drop-in compatible wrapper for LdapConnection
—it implements the same interface and can be used as a direct replacement in your code. This is necessary because the .NET Framework's LdapConnection
cannot be instrumented directly.
- Automatic instrumentation of LDAP client operations
- Collection of OTEL-compliant metrics and traces for LDAP requests
- Enrichment and filtering APIs for customizing telemetry
- Support for context propagation
- Compatible with OpenTelemetry SDK and exporters
Add a reference to the AlbusKavaliro.TraceableLdapClient package. Also add any other instrumentations & exporters you need.
dotnet add package AlbusKavaliro.TraceableLdapClient
Change your code to use TraceableLdapConnection
everywhere you would use LdapConnection
. The API is compatible, so you can simply replace:
// Old:
var ldap = new LdapConnection(server);
// New:
var ldap = new TraceableLdapConnection(server);
This enables automatic collection of metrics and traces for all LDAP operations performed through the wrapper.
You do not need to call any special registration method for this package. Instead, ensure that your OpenTelemetry configuration includes the following:
- Register the ActivitySource and Meter for
TraceableLdapClient.LdapConnection
. - Use your application's standard OpenTelemetry setup (for example, via a helper like
ConfigureOpenTelemetry
).
const string ldapConnectionInstrumentation = "TraceableLdapClient.LdapConnection";
builder.Services.AddOpenTelemetry()
.WithMetrics(metrics =>
{
metrics.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddRuntimeInstrumentation()
.AddMeter(ldapConnectionInstrumentation);
})
.WithTracing(tracing =>
{
tracing.AddSource(builder.Environment.ApplicationName)
.AddAspNetCoreInstrumentation()
.AddHttpClientInstrumentation()
.AddSource(ldapConnectionInstrumentation);
});
This pattern matches the approach used in .NET Aspire and the OpenTelemetry.Instrumentation packages: you only need to register the ActivitySource and Meter names, and the library will emit telemetry automatically.
The following metrics are produced by this instrumentation:
network.client.requests
(Counter): Total number of LDAP requests madenetwork.client.errors
(Counter): Number of failed LDAP requestsnetwork.client.duration
(Histogram, milliseconds): Duration of LDAP requestsldap.search.entries_returned
(Counter): Number of entries returned by LDAP search operations
Each metric includes relevant attributes from the LDAP operation context, such as server address, operation type, and result codes.
Each LDAP request is traced as an activity/span with the following base attributes:
network.protocol.name
— Always set to "ldap"network.transport
— Always set to "tcp"server.address
— The LDAP server addressserver.port
— The LDAP server portldap.operation
— The LDAP operation typerequestType
— The specific type of LDAP requestdistinguishedName
— The DN involved in the request (where applicable)ldap.response.type
— Type of response receivedldap.response.result_code
— Result code from the serverldap.response.error_message
— Error message if any
For search operations, additional attributes are included:
ldap.search.base
— Search base DNldap.search.scope
— Search scopeldap.search.filter
— Search filterldap.search.attributes
— Requested attributesldap.search.size_limit
— Size limitldap.search.time_limit
— Time limit
For errors, exception details are added:
exception.type
— Type of the exceptionexception.message
— Exception messageexception.stacktrace
— Stack trace
You can customize the telemetry by filtering or enriching activities and metrics in your OpenTelemetry pipeline. For example, you can use processors, views, or enrichers provided by the OpenTelemetry SDK to:
- Add custom tags (e.g., tenant ID, correlation ID) to LDAP activities.
- Filter out requests based on operation, DN, or server.
- Only record failed or slow requests.
See the OpenTelemetry documentation for details on customizing telemetry.
Activity.Duration
andnetwork.client.duration
represent the time taken to complete the LDAP request, up to receiving the server response.
This project is licensed under the MIT License. See the LICENSE file for details.