Skip to content

Commit

Permalink
Add env var support and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cartermp committed Sep 20, 2022
1 parent eb94e44 commit 03cacea
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 5 deletions.
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
3 changes: 2 additions & 1 deletion src/Honeycomb.OpenTelemetry/HoneycombOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,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 @@ -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
}
}

0 comments on commit 03cacea

Please sign in to comment.