Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support debug option #254

Merged
merged 4 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Honeycomb.OpenTelemetry/EnvironmentOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal class EnvironmentOptions
private const string ServiceNameKey = "OTEL_SERVICE_NAME";
private const string ServiceVersionKey = "SERVICE_VERSION";
private const string EnableLocalVisualizationsKey = "ENABLE_LOCAL_VISUALIZATIONS";
private const string DebugKey = "DEBUG";
private const uint DefaultSampleRate = 1;
private const string DefaultApiEndpoint = "https://api.honeycomb.io:443";
private readonly IDictionary _environmentService;
Expand All @@ -38,6 +39,7 @@ internal EnvironmentOptions(IDictionary service)
internal string ServiceName => GetEnvironmentVariable(ServiceNameKey);
internal string ServiceVersion => GetEnvironmentVariable(ServiceVersionKey);
internal bool EnableLocalVisualizations => bool.TryParse(GetEnvironmentVariable(EnableLocalVisualizationsKey), out var enableLocalVisualizations) ? enableLocalVisualizations : false;
internal bool Debug => bool.TryParse(GetEnvironmentVariable(DebugKey), out var debug) ? debug : false;
internal uint SampleRate => uint.TryParse(GetEnvironmentVariable(SampleRateKey), out var sampleRate) ? sampleRate : DefaultSampleRate;

private string GetEnvironmentVariable(string key, string defaultValue = "")
Expand Down
5 changes: 3 additions & 2 deletions src/Honeycomb.OpenTelemetry/Honeycomb.OpenTelemetry.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.3.0 " />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.3.0" />
<PackageReference Include="OpenTelemetry" Version="1.3.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.3.1" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.3.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
Expand Down
9 changes: 8 additions & 1 deletion src/Honeycomb.OpenTelemetry/HoneycombOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,12 @@ public string MetricsEndpoint
/// Determines whether the <see cref="DeterministicSampler"/> sampler is added when configuring a <see cref="TracerProviderBuilder"/>.
/// </summary>
public bool AddDeterministicSampler { get; set; } = true;

/// <summary>
/// If set to true, enables the console span exporter for local debugging.
/// </summary>
public bool Debug { get; set; } = false;

private static readonly Dictionary<string, string> CommandLineSwitchMap = new Dictionary<string, string>
{
{ "--honeycomb-apikey", "apikey" },
Expand All @@ -196,7 +202,8 @@ public string MetricsEndpoint
{ "--honeycomb-add-determinisitc-sampler", "addDeterministicSampler" },
{ "--service-name", "servicename" },
{ "--service-version", "serviceversion" },
{ "--meter-names", "meternames" }
{ "--meter-names", "meternames" },
{ "--debug", "debug" }
};

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ public static MeterProviderBuilder AddHoneycomb(this MeterProviderBuilder builde
{
builder.AddMeter(meterName);
}

if (options.Debug)
{
builder.AddConsoleExporter();
}
}

return builder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ public static TracerProviderBuilder AddHoneycomb(this TracerProviderBuilder buil
builder.AddProcessor(new SimpleActivityExportProcessor(new ConsoleTraceLinkExporter(options)));
}

if (options.Debug)
{
builder.AddConsoleExporter();
}

// heads up: even if dataset is set, it will be ignored
if (!string.IsNullOrWhiteSpace(options.TracesApiKey) & !options.IsTracesLegacyKey() & (!string.IsNullOrWhiteSpace(options.TracesDataset)))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public void Can_get_options_from_env_vars()
{"HONEYCOMB_SAMPLE_RATE", "10"},
{"OTEL_SERVICE_NAME", "my-service-name"},
{"SERVICE_VERSION", "my-service-version"},
{"ENABLE_LOCAL_VISUALIZATIONS", "true" }
{"ENABLE_LOCAL_VISUALIZATIONS", "true" },
{"DEBUG", "true"}
};
var options = new EnvironmentOptions(values);
Assert.Equal("my-api-key", options.ApiKey);
Expand All @@ -38,6 +39,7 @@ public void Can_get_options_from_env_vars()
Assert.Equal("my-service-name", options.ServiceName);
Assert.Equal("my-service-version", options.ServiceVersion);
Assert.True(options.EnableLocalVisualizations);
Assert.True(options.Debug);
}

[Fact]
Expand Down
10 changes: 8 additions & 2 deletions test/Honeycomb.OpenTelemetry.Tests/HoneycombOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public void DefaultsOptionalArgs()
Assert.Equal(HoneycombOptions.DefaultEndpoint, options.TracesEndpoint);
Assert.Equal(HoneycombOptions.DefaultEndpoint, options.MetricsEndpoint);
Assert.Empty(options.MeterNames);
Assert.False(options.Debug);
}

[Fact]
Expand All @@ -43,7 +44,8 @@ public void CanParseOptionsFromSpacedCommandLineArgs()
"--honeycomb-metrics-endpoint", "my-metrics-endpoint",
"--meter-names", "meter1,meter2",
"--service-name", "my-service",
"--service-version", "my-version"
"--service-version", "my-version",
"--debug", "true"
);

Assert.Equal("my-apikey", options.ApiKey);
Expand All @@ -59,6 +61,7 @@ public void CanParseOptionsFromSpacedCommandLineArgs()
Assert.Equal("my-service", options.ServiceName);
Assert.Equal("my-version", options.ServiceVersion);
Assert.Equal(new List<string> { "meter1", "meter2" }, options.MeterNames);
Assert.True(options.Debug);
}

[Fact]
Expand All @@ -77,7 +80,8 @@ public void CanParseOptionsFromInlineCommandLineArgs()
"--honeycomb-metrics-endpoint=my-metrics-endpoint",
"--meter-names=meter1,meter2",
"--service-name=my-service",
"--service-version=my-version"
"--service-version=my-version",
"--debug=true"
);

Assert.Equal("my-apikey", options.ApiKey);
Expand All @@ -93,6 +97,7 @@ public void CanParseOptionsFromInlineCommandLineArgs()
Assert.Equal("my-service", options.ServiceName);
Assert.Equal("my-version", options.ServiceVersion);
Assert.Equal(new List<string> { "meter1", "meter2" }, options.MeterNames);
Assert.True(options.Debug);
}

[Fact]
Expand Down Expand Up @@ -120,6 +125,7 @@ public void CanParseOptionsFromConfiguration()
Assert.Equal("my-version", options.ServiceVersion);
Assert.Equal(new List<string> { "meter1", "meter2" }, options.MeterNames);
Assert.True(options.EnableLocalVisualizations);
Assert.True(options.Debug);
}

[Fact]
Expand Down
3 changes: 2 additions & 1 deletion test/Honeycomb.OpenTelemetry.Tests/appsettings.test.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
],
"ServiceName": "my-service",
"ServiceVersion": "my-version",
"EnableLocalVisualizations": true
"EnableLocalVisualizations": true,
"Debug": true
}
}