Skip to content

Commit

Permalink
Make tracing sources configureable
Browse files Browse the repository at this point in the history
Signed-off-by: jan.jansen <jan.jansen@gdata.de>
  • Loading branch information
farodin91 committed Jan 13, 2021
1 parent 04fc8aa commit 9eac6b0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion shared.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<PropertyGroup>
<Version>0.5.2</Version>
<Version>0.5.3</Version>
<LangVersion>9</LangVersion>
<Nullable>enable</Nullable>
<WarningsAsErrors>CS8600;CS8602;CS8625;CS8618;CS8604;CS8601</WarningsAsErrors>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System.Collections.Generic;

namespace Motor.Extensions.Diagnostics.Tracing
{
public record OpenTelemetryOptions
{
public static readonly string DefaultActivitySourceName = "MotorNet.OpenTelemetry";
public double SamplingProbability { get; set; } = 0.0001;
public List<string> Sources { get; set; } = new() {DefaultActivitySourceName};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ public class TracingDelegatingMessageHandler<TInput> : DelegatingMessageHandler<
{
private readonly ILogger<TracingDelegatingMessageHandler<TInput>> _logger;

private static readonly ActivitySource _activitySource =
new(typeof(TracingDelegatingMessageHandler<>).FullName!);
private static readonly ActivitySource ActivitySource =
new(OpenTelemetryOptions.DefaultActivitySourceName);

public TracingDelegatingMessageHandler(ILogger<TracingDelegatingMessageHandler<TInput>> logger)
{
Expand All @@ -33,7 +33,7 @@ public override async Task<ProcessedMessageStatus> HandleMessageAsync(MotorCloud
{
parentContext = extension.GetActivityContext();
}
using var activity = _activitySource.StartActivity(nameof(HandleMessageAsync), ActivityKind.Server, parentContext);
using var activity = ActivitySource.StartActivity(nameof(HandleMessageAsync), ActivityKind.Server, parentContext);
if (activity == null) return await base.HandleMessageAsync(dataCloudEvent, token);

using (activity.Start())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static IMotorHostBuilder ConfigureJaegerTracing(this IMotorHostBuilder ho

builder
.AddAspNetCoreInstrumentation()
.AddSource(typeof(TracingDelegatingMessageHandler<>).FullName!)
.AddSource(openTelemetryOptions.Sources.ToArray())
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(applicationNameService.GetFullName(), serviceVersion: applicationNameService.GetVersion()))
.SetMotorSampler(openTelemetryOptions)
.AddExporter(logger, jaegerOptions.Value, applicationNameService, hostContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public GenericHostingTests(RabbitMQFixture fixture, ITestOutputHelper outputHelp
_outputHelper = outputHelper;
_listener = new ActivityListener
{
ShouldListenTo = source => source.Name == typeof(TracingDelegatingMessageHandler<>).FullName,
ShouldListenTo = source => source.Name == OpenTelemetryOptions.DefaultActivitySourceName,
Sample = (ref ActivityCreationOptions<ActivityContext> options) =>
ActivitySamplingResult.AllDataAndRecorded,
ActivityStarted = _ => { _outputHelper.WriteLine("test"); },
Expand Down Expand Up @@ -301,6 +301,7 @@ protected class ReverseStringConverter : ISingleOutputService<string, string>
{
private readonly ILogger<ReverseStringConverter> _logger;
private readonly IMetricFamily<ISummary> _summary;
private static ActivitySource ActivitySource = new(OpenTelemetryOptions.DefaultActivitySourceName);

public ReverseStringConverter(ILogger<ReverseStringConverter> logger,
IMetricsFactory<ReverseStringConverter> metricsFactory)
Expand All @@ -314,6 +315,10 @@ public Task<MotorCloudEvent<string>> ConvertMessageAsync(MotorCloudEvent<string>
{
_logger.LogInformation("log your request");
var tmpChar = dataCloudEvent.TypedData.ToCharArray();
if (!ActivitySource.HasListeners())
{
throw new ArgumentException();
}
var reversed = tmpChar.Reverse().ToArray();
_summary.WithLabels("collect_your_metrics").Observe(1.0);
return Task.FromResult(dataCloudEvent.CreateNew(new string(reversed)));
Expand Down

0 comments on commit 9eac6b0

Please sign in to comment.