-
-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathSentryTracerProviderBuilderExtensions.cs
More file actions
67 lines (61 loc) · 2.83 KB
/
Copy pathSentryTracerProviderBuilderExtensions.cs
File metadata and controls
67 lines (61 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using OpenTelemetry.Exporter;
using Sentry;
// ReSharper disable once CheckNamespace -- Discoverability
namespace OpenTelemetry.Trace;
/// <summary>
/// Contains extension methods for the <see cref="TracerProviderBuilder"/> class.
/// </summary>
public static class SentryTracerProviderBuilderExtensions
{
internal const string MissingDsnWarning = $"Invalid DSN passed to {nameof(AddSentryOtlpExporter)}.";
/// <summary>
/// <para>
/// Ensures OpenTelemetry trace information is sent to the Sentry OTLP endpoint.
/// </para>
/// <para>
/// Note that if you use this method to configure the trace builder, you will also need to call
/// <see cref="SentryOptionsExtensions.UseOtlp(Sentry.SentryOptions)"/> when initialising Sentry, for Sentry to work
/// properly with OpenTelemetry.
/// </para>
/// </summary>
/// <param name="tracerProviderBuilder">The <see cref="TracerProviderBuilder"/>.</param>
/// <param name="dsnString">The DSN for your Sentry project</param>
/// <param name="collectorUrl">A custom endpoint to export OLTP trace information to. If no url is provided, the
/// endpoint will be inferred automatically from the DSN.</param>
/// <returns>The supplied <see cref="TracerProviderBuilder"/> for chaining.</returns>
/// <remarks>
/// This method does not configure an OpenTelemetry propagator.
/// Cross-service trace propagation should be enabled via the OpenTelemetry SDK (e.g. by calling
/// <c>Sdk.SetDefaultTextMapPropagator</c>).
/// </remarks>
public static TracerProviderBuilder AddSentryOtlpExporter(this TracerProviderBuilder tracerProviderBuilder,
string dsnString, Uri? collectorUrl = null)
{
if (Dsn.IsDisabled(dsnString))
{
return tracerProviderBuilder;
}
if (Dsn.TryParse(dsnString) is not { } dsn)
{
throw new ArgumentException(MissingDsnWarning, nameof(dsnString));
}
collectorUrl ??= dsn.GetOtlpTracesEndpointUri();
tracerProviderBuilder.AddOtlpExporter(options => OtlpConfigurationCallback(options, collectorUrl, dsn.PublicKey));
return tracerProviderBuilder;
}
// Internal helper method for testing purposes
internal static void OtlpConfigurationCallback(OtlpExporterOptions options, Uri collectorUrl, string publicKey)
{
options.Endpoint = collectorUrl;
options.Protocol = OtlpExportProtocol.HttpProtobuf;
options.HttpClientFactory = () =>
{
var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-Sentry-Auth",
$"Sentry sentry_version={SentryConstants.ProtocolVersion}," +
$"sentry_client={SdkVersion.Instance.Name}/{SdkVersion.Instance.Version}," +
$"sentry_key={publicKey}");
return client;
};
}
}