Skip to content

Commit

Permalink
update matrics specific options
Browse files Browse the repository at this point in the history
  • Loading branch information
pkanal committed Sep 27, 2022
1 parent 917066e commit a49f444
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 9 deletions.
27 changes: 23 additions & 4 deletions src/Honeycomb.OpenTelemetry/HoneycombOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ public class HoneycombOptions
internal static readonly string SDefaultServiceName = $"unknown_service:{System.Diagnostics.Process.GetCurrentProcess().ProcessName}";
internal static readonly string SDefaultServiceVersion = "{unknown_service_version}";

private string _metricsApiKey;
private string _metricsEndpoint;

/// <summary>
/// Name of the Honeycomb section of IConfiguration
/// </summary>
Expand Down Expand Up @@ -232,7 +229,7 @@ internal void ApplyEnvironmentOptions(EnvironmentOptions environmentOptions)
}

/// <summary>
/// Computes the final traces endpoint.
/// Gets the <see cref="TracesEndpoint" /> or falls back to the generic <see cref="Endpoint" />.
/// </summary>
internal string GetTracesEndpoint()
{
Expand All @@ -244,16 +241,38 @@ internal string GetTracesEndpoint()
return TracesEndpoint ?? endpoint.ToString();
}

/// <summary>
/// Gets the <see cref="TracesApiKey" /> or falls back to the generic <see cref="ApiKey" />.
/// </summary>
internal string GetTracesApiKey()
{
return TracesApiKey ?? ApiKey;
}

/// <summary>
/// Gets the <see cref="TracesDataset" /> or falls back to the generic <see cref="Dataset" />.
/// </summary>
internal string GetTracesDataset()
{
return TracesDataset ?? Dataset;
}

/// <summary>
/// Gets the <see cref="MetricsEndpoint" /> or falls back to the generic <see cref="Endpoint" />.
/// </summary>
internal string GetMetricsEndpoint()
{
return new UriBuilder(MetricsEndpoint ?? Endpoint).ToString();
}

/// <summary>
/// Gets the <see cref="MetricsApiKey" /> or falls back to the generic <see cref="ApiKey" />.
/// </summary>
internal string GetMetricsApiKey()
{
return MetricsApiKey ?? ApiKey;
}

private static readonly Dictionary<string, string> CommandLineSwitchMap = new Dictionary<string, string>
{
{ "--honeycomb-apikey", "apikey" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ public static MeterProviderBuilder AddHoneycomb(this MeterProviderBuilder builde
Console.WriteLine("WARN: missing metrics API key");
}

var _metricsEndpoint = options.MetricsEndpoint ?? options.Endpoint;
var _metricsApiKey = options.MetricsApiKey ?? options.ApiKey;
var _metricsDataset = options.MetricsDataset;

builder
.SetResourceBuilder(
ResourceBuilder
Expand All @@ -63,7 +59,7 @@ public static MeterProviderBuilder AddHoneycomb(this MeterProviderBuilder builde
.AddEnvironmentVariableDetector()
.AddService(serviceName: options.ServiceName, serviceVersion: options.ServiceVersion)
)
.AddHoneycombOtlpExporter(_metricsApiKey, _metricsDataset, _metricsEndpoint);
.AddHoneycombOtlpExporter(options.GetMetricsApiKey(), options.MetricsDataset, options.GetMetricsEndpoint());

builder.AddMeter(options.ServiceName);
foreach (var meterName in options.MeterNames)
Expand Down
91 changes: 91 additions & 0 deletions test/Honeycomb.OpenTelemetry.Tests/HoneycombOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,22 @@ public void UsesGenericValuesIfTracesSpecificValuesAreNotSet_Config()
Assert.Equal("my-dataset", options.GetTracesDataset());
}

[Fact]
public void UsesGenericValuesIfMetricsSpecificValuesAreNotSet_Config()
{
var options = new HoneycombOptions
{
Endpoint = "http://collector:4318",
ApiKey = "my-api-key",
Dataset = "my-dataset"
};

Assert.Equal("http://collector:4318/", options.GetMetricsEndpoint());
Assert.Equal("my-api-key", options.GetMetricsApiKey());
// Should not fall override metrics dataset
Assert.NotEqual("my-dataset", options.MetricsDataset);
}

[Fact]
public void UsesTracesSpecificValuesIfSet_Config()
{
Expand All @@ -246,6 +262,18 @@ public void UsesTracesSpecificValuesIfSet_Config()
Assert.Equal("my-dataset", options.GetTracesDataset());
}

[Fact]
public void UsesMetricsSpecificValuesIfSet_Config()
{
var options = new HoneycombOptions
{
MetricsEndpoint = "http://collector:4318",
MetricsApiKey = "my-api-key",
};
Assert.Equal("http://collector:4318/", options.GetMetricsEndpoint());
Assert.Equal("my-api-key", options.GetMetricsApiKey());
}

[Fact]
public void UseTracesSpecificValuesOverGenericValues_Config()
{
Expand All @@ -264,6 +292,21 @@ public void UseTracesSpecificValuesOverGenericValues_Config()
Assert.Equal("my-dataset-traces", options.GetTracesDataset());
}

[Fact]
public void UseMetricsSpecificValuesOverGenericValues_Config()
{
var options = new HoneycombOptions
{
Endpoint = "http://collector:4318",
ApiKey = "my-api-key",
MetricsEndpoint = "http://collector:4318/v1/metrics",
MetricsApiKey = "my-api-key-metrics",
};

Assert.Equal("http://collector:4318/v1/metrics", options.GetMetricsEndpoint());
Assert.Equal("my-api-key-metrics", options.GetMetricsApiKey());
}

[Fact]
public void UsesGenericValuesIfTracesSpecificValuesAreNotSet_EnvVars()
{
Expand All @@ -281,6 +324,22 @@ public void UsesGenericValuesIfTracesSpecificValuesAreNotSet_EnvVars()
Assert.Equal("my-dataset-env-var", options.GetTracesDataset());
}

[Fact]
public void UsesGenericValuesIfMetricsSpecificValuesAreNotSet_EnvVars()
{
var options = new HoneycombOptions { };
var values = new Dictionary<string, string>
{
{"HONEYCOMB_API_KEY", "my-api-key-env-var"},
{"HONEYCOMB_DATASET", "my-dataset-env-var"},
{"HONEYCOMB_API_ENDPOINT", "http://collector:4318/"},
};
options.ApplyEnvironmentOptions(new EnvironmentOptions(values));

Assert.Equal("http://collector:4318/", options.GetMetricsEndpoint());
Assert.Equal("my-api-key-env-var", options.GetMetricsApiKey());
}

[Fact]
public void UsesTracesSpecificValuesIfSet_EnvVars()
{
Expand All @@ -298,6 +357,21 @@ public void UsesTracesSpecificValuesIfSet_EnvVars()
Assert.Equal("my-traces-dataset-env-var", options.GetTracesDataset());
}

[Fact]
public void UsesMetricsSpecificValuesIfSet_EnvVars()
{
var options = new HoneycombOptions { };
var values = new Dictionary<string, string>
{
{"HONEYCOMB_METRICS_API_KEY", "my-metrics-api-key-env-var"},
{"HONEYCOMB_METRICS_ENDPOINT", "http://collector:4318/v1/metrics"},
};
options.ApplyEnvironmentOptions(new EnvironmentOptions(values));

Assert.Equal("http://collector:4318/v1/metrics", options.GetMetricsEndpoint());
Assert.Equal("my-metrics-api-key-env-var", options.GetMetricsApiKey());
}

[Fact]
public void UseTracesSpecificValuesOverGenericValues_EnvVars()
{
Expand All @@ -318,6 +392,23 @@ public void UseTracesSpecificValuesOverGenericValues_EnvVars()
Assert.Equal("my-traces-dataset-env-var", options.GetTracesDataset());
}

[Fact]
public void UseMetricsSpecificValuesOverGenericValues_EnvVars()
{
var options = new HoneycombOptions { };
var values = new Dictionary<string, string>
{
{"HONEYCOMB_API_KEY", "my-api-key-env-var"},
{"HONEYCOMB_API_ENDPOINT", "http://collector:4318/"},
{"HONEYCOMB_METRICS_API_KEY", "my-metrics-api-key-env-var"},
{"HONEYCOMB_METRICS_ENDPOINT", "http://collector:4318/v1/metrics"},
};
options.ApplyEnvironmentOptions(new EnvironmentOptions(values));

Assert.Equal("http://collector:4318/v1/metrics", options.GetMetricsEndpoint());
Assert.Equal("my-metrics-api-key-env-var", options.GetMetricsApiKey());
}

[Fact]
public void AppendsTracesPathIfProtocolIsHttp_Config()
{
Expand Down

0 comments on commit a49f444

Please sign in to comment.