Skip to content

Commit 35390c5

Browse files
authored
feat(otlp): add custom headers support for OTLP exporter (#1)
1 parent 7e5f92b commit 35390c5

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ catch (Exception ex)
225225
| `ServiceVersion` | `string` | `"1.0.0"` | Service version |
226226
| `OtlpEndpoint` | `string` | `null` | OTLP collector endpoint (e.g., `http://localhost:4317`) |
227227
| `UseOtlpHttp` | `bool` | `false` | Use HTTP/protobuf instead of gRPC |
228+
| `OtlpHeaders` | `IDictionary<string, string>` | empty | Custom headers for OTLP requests (auth, API keys) |
228229
| `EnableConsoleExporter` | `bool` | `false` | Output telemetry to console (for debugging) |
229230
| `EnableTracing` | `bool` | `true` | Enable distributed tracing |
230231
| `EnableMetrics` | `bool` | `true` | Enable metrics collection |
@@ -256,6 +257,23 @@ VsixTelemetry.Initialize(new TelemetryConfiguration
256257
});
257258
```
258259

260+
### Example: Using Custom Headers (Honeycomb, etc.)
261+
262+
```csharp
263+
var config = new TelemetryConfiguration
264+
{
265+
ServiceName = "MyExtension",
266+
OtlpEndpoint = "https://api.honeycomb.io:443",
267+
UseOtlpHttp = true
268+
};
269+
270+
// Add authentication headers
271+
config.OtlpHeaders["x-honeycomb-team"] = "your-api-key";
272+
config.OtlpHeaders["x-honeycomb-dataset"] = "your-dataset";
273+
274+
VsixTelemetry.Initialize(config);
275+
```
276+
259277
---
260278

261279
## Dependency Injection

src/CodingWithCalvin.Otel4Vsix/TelemetryConfiguration.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,21 @@ public sealed class TelemetryConfiguration
4040
/// </remarks>
4141
public bool UseOtlpHttp { get; set; }
4242

43+
/// <summary>
44+
/// Gets custom headers to include in OTLP export requests.
45+
/// </summary>
46+
/// <remarks>
47+
/// Use this to add authentication headers (API keys, bearer tokens) or other custom headers
48+
/// required by your telemetry backend.
49+
/// <example>
50+
/// <code>
51+
/// config.OtlpHeaders["x-api-key"] = "your-api-key";
52+
/// config.OtlpHeaders["Authorization"] = "Bearer your-token";
53+
/// </code>
54+
/// </example>
55+
/// </remarks>
56+
public IDictionary<string, string> OtlpHeaders { get; } = new Dictionary<string, string>();
57+
4358
/// <summary>
4459
/// Gets or sets a value indicating whether the console exporter is enabled.
4560
/// </summary>

src/CodingWithCalvin.Otel4Vsix/VsixTelemetry.cs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Diagnostics.Metrics;
5+
using System.Linq;
56
using System.Threading;
67
using Microsoft.Extensions.Logging;
78
using OpenTelemetry;
@@ -367,11 +368,7 @@ private static TracerProvider BuildTracerProvider(ResourceBuilder resourceBuilde
367368
{
368369
builder.AddOtlpExporter(options =>
369370
{
370-
options.Endpoint = new Uri(_configuration.OtlpEndpoint);
371-
options.Protocol = _configuration.UseOtlpHttp
372-
? OtlpExportProtocol.HttpProtobuf
373-
: OtlpExportProtocol.Grpc;
374-
options.TimeoutMilliseconds = _configuration.ExportTimeoutMilliseconds;
371+
ConfigureOtlpExporter(options);
375372
});
376373
}
377374

@@ -395,11 +392,7 @@ private static OpenTelemetry.Metrics.MeterProvider BuildMeterProvider(ResourceBu
395392
{
396393
builder.AddOtlpExporter(options =>
397394
{
398-
options.Endpoint = new Uri(_configuration.OtlpEndpoint);
399-
options.Protocol = _configuration.UseOtlpHttp
400-
? OtlpExportProtocol.HttpProtobuf
401-
: OtlpExportProtocol.Grpc;
402-
options.TimeoutMilliseconds = _configuration.ExportTimeoutMilliseconds;
395+
ConfigureOtlpExporter(options);
403396
});
404397
}
405398

@@ -427,11 +420,7 @@ private static ILoggerFactory BuildLoggerFactory(ResourceBuilder resourceBuilder
427420
{
428421
options.AddOtlpExporter(exporterOptions =>
429422
{
430-
exporterOptions.Endpoint = new Uri(_configuration.OtlpEndpoint);
431-
exporterOptions.Protocol = _configuration.UseOtlpHttp
432-
? OtlpExportProtocol.HttpProtobuf
433-
: OtlpExportProtocol.Grpc;
434-
exporterOptions.TimeoutMilliseconds = _configuration.ExportTimeoutMilliseconds;
423+
ConfigureOtlpExporter(exporterOptions);
435424
});
436425
}
437426

@@ -444,6 +433,23 @@ private static ILoggerFactory BuildLoggerFactory(ResourceBuilder resourceBuilder
444433
});
445434
}
446435

436+
private static void ConfigureOtlpExporter(OtlpExporterOptions options)
437+
{
438+
options.Endpoint = new Uri(_configuration.OtlpEndpoint);
439+
options.Protocol = _configuration.UseOtlpHttp
440+
? OtlpExportProtocol.HttpProtobuf
441+
: OtlpExportProtocol.Grpc;
442+
options.TimeoutMilliseconds = _configuration.ExportTimeoutMilliseconds;
443+
444+
// Add custom headers if configured
445+
if (_configuration.OtlpHeaders.Count > 0)
446+
{
447+
var headerString = string.Join(",",
448+
_configuration.OtlpHeaders.Select(kvp => $"{kvp.Key}={kvp.Value}"));
449+
options.Headers = headerString;
450+
}
451+
}
452+
447453
private static void ThrowIfNotInitialized()
448454
{
449455
if (!_isInitialized)

0 commit comments

Comments
 (0)