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

Append /v1/metrics to endpoint path if the protocol is HTTP #284

Merged
merged 5 commits into from
Sep 29, 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
3 changes: 1 addition & 2 deletions smoke-tests/collector/otel-collector-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ service:
metrics:
receivers: [otlp]
processors: [batch]
# exporters: [file, logging]
exporters: [logging]
exporters: [file, logging]
logs:
receivers: [otlp]
processors: [batch]
Expand Down
2 changes: 1 addition & 1 deletion smoke-tests/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ x-env-base: &env_base
HONEYCOMB_API_KEY: bogus_key
HONEYCOMB_DATASET: bogus_dataset
HONEYCOMB_METRICS_DATASET: bogus_dataset
OTEL_METRIC_EXPORT_INTERVAL: 1000
OTEL_METRIC_EXPORT_INTERVAL: 100
OTEL_SERVICE_NAME: "aspnetcore-example"
DEBUG: "true"

Expand Down
7 changes: 7 additions & 0 deletions smoke-tests/smoke-sdk-grpc.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ load test_helpers/utilities

CONTAINER_NAME="app-sdk-grpc"
OTEL_SERVICE_NAME="aspnetcore-example"
METRICS_DATASET="bogus_dataset"

setup_file() {
echo "# 🚧" >&3
docker-compose up --detach collector ${CONTAINER_NAME}
wait_for_ready_app ${CONTAINER_NAME}
curl --silent "http://localhost:5001/weatherforecast"
wait_for_traces
wait_for_metrics 15
}

teardown_file() {
Expand All @@ -30,4 +32,9 @@ teardown_file() {
@test "Manual instrumentation adds custom attribute" {
result=$(span_attributes_for ${OTEL_SERVICE_NAME} | jq "select(.key == \"delay_ms\").value.intValue")
assert_equal "$result" '"100"'
}

@test "Manual instrumentation produces metrics" {
result=$(metric_names_for ${METRICS_DATASET})
assert_equal "$result" '"sheep"'
}
7 changes: 7 additions & 0 deletions smoke-tests/smoke-sdk-http.bats
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ load test_helpers/utilities

CONTAINER_NAME="app-sdk-http"
OTEL_SERVICE_NAME="aspnetcore-example"
METRICS_DATASET="bogus_dataset"

setup_file() {
echo "# 🚧" >&3
docker-compose up --detach collector ${CONTAINER_NAME}
wait_for_ready_app ${CONTAINER_NAME}
curl --silent "http://localhost:5001/weatherforecast"
wait_for_traces
wait_for_metrics 15
}

teardown_file() {
Expand All @@ -31,3 +33,8 @@ teardown_file() {
result=$(span_attributes_for ${OTEL_SERVICE_NAME} | jq "select(.key == \"delay_ms\").value.intValue")
assert_equal "$result" '"100"'
}

@test "Manual instrumentation produces metrics" {
result=$(metric_names_for ${METRICS_DATASET})
assert_equal "$result" '"sheep"'
}
11 changes: 8 additions & 3 deletions src/Honeycomb.OpenTelemetry/HoneycombOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class HoneycombOptions
private const string OtelExporterOtlpProtocolHttpJson = "http/json";
private const string OtelExporterOtlpProtocolGrpc = "grpc";
private const string OtelExporterHttpTracesPath = "/v1/traces";

private const string OtelExporterHttpMetricsPath = "/v1/metrics";
private bool isHttp = false;

/// <summary>
Expand Down Expand Up @@ -230,7 +230,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 @@ -257,7 +257,12 @@ internal string GetTracesDataset()
/// </summary>
internal string GetMetricsEndpoint()
{
return new UriBuilder(MetricsEndpoint ?? Endpoint).ToString();
var endpoint = new UriBuilder(Endpoint);
if (isHttp && (string.IsNullOrWhiteSpace(endpoint.Path) || endpoint.Path == "/"))
{
endpoint.Path = OtelExporterHttpMetricsPath;
}
return MetricsEndpoint ?? endpoint.ToString();
}

/// <summary>
Expand Down
175 changes: 104 additions & 71 deletions test/Honeycomb.OpenTelemetry.Tests/HoneycombOptionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ public void UsesMetricsSpecificValuesIfSet_Config()
MetricsEndpoint = "http://collector:4318",
MetricsApiKey = "my-api-key",
};
Assert.Equal("http://collector:4318/", options.GetMetricsEndpoint());
Assert.Equal("http://collector:4318", options.GetMetricsEndpoint());
Assert.Equal("my-api-key", options.GetMetricsApiKey());
}

Expand Down Expand Up @@ -337,157 +337,190 @@ public void UseMetricsSpecificValuesOverGenericValues_EnvVars()
Assert.Equal("my-metrics-api-key-env-var", options.GetMetricsApiKey());
}

[Fact]
public void AppendsTracesPathIfProtocolIsHttpProtobuf_Config()
[Theory]
[InlineData("http/protobuf", "http://collector:4318", null)]
[InlineData("http/json", "http://collector:4318", null)]
[InlineData("http/protobuf", null, "http://collector:4318")]
[InlineData("http/json", null, "http://collector:4318")]
[InlineData("http/protobuf", "http://collector:4318", "http://collector:4318")]
[InlineData("http/json", "http://collector:4318", "http://collector:4318")]
public void AppendsTracesPathIfProtocolIsHttp(string protocol, string configEndpoint, string envVarEndpoint)
{
var options = new HoneycombOptions
{
Endpoint = "http://collector:4318/"
Endpoint = configEndpoint
};
var values = new Dictionary<string, string>
{
{"OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf"},
{"OTEL_EXPORTER_OTLP_PROTOCOL", protocol},
{"HONEYCOMB_API_ENDPOINT", envVarEndpoint},
};

options.ApplyEnvironmentOptions(new EnvironmentOptions(values));
Assert.Equal("http://collector:4318/v1/traces", options.GetTracesEndpoint());
Assert.Equal($"{options.Endpoint}/v1/traces", options.GetTracesEndpoint());
}

[Fact]
public void AppendsTracesPathIfProtocolIsHttpJson_Config()
[Theory]
[InlineData("http://collector:4317", null)]
[InlineData(null, "http://collector:4317")]
[InlineData("http://collector:4317", "http://collector:4317")]
public void DoesNotAppendTracesPathIfProtocolIsGrpc(string configEndpoint, string envVarEndpoint)
{
var options = new HoneycombOptions
{
Endpoint = "http://collector:4318/"
Endpoint = configEndpoint
};
var values = new Dictionary<string, string>
{
{"OTEL_EXPORTER_OTLP_PROTOCOL", "http/json"},
{"OTEL_EXPORTER_OTLP_PROTOCOL", "grpc"},
{"HONEYCOMB_API_ENDPOINT", envVarEndpoint},
};

options.ApplyEnvironmentOptions(new EnvironmentOptions(values));
Assert.Equal("http://collector:4318/v1/traces", options.GetTracesEndpoint());
Assert.Equal("http://collector:4317/", options.GetTracesEndpoint());
Assert.DoesNotContain("/v1/traces", options.GetTracesEndpoint());
}

[Fact]
public void DoesNotAppendTracesPathIfProtocolIsGrpc_Config()
[Theory]
[InlineData("http/protobuf", "http://collector:4318", null)]
[InlineData("http/json", "http://collector:4318", null)]
[InlineData("http/protobuf", null, "http://collector:4318")]
[InlineData("http/json", null, "http://collector:4318")]
[InlineData("http/protobuf", "http://collector:4318", "http://collector:4318")]
[InlineData("http/json", "http://collector:4318", "http://collector:4318")]
public void DoesNotAppendTracesPathToTracesEndpoints(string protocol, string configEndpoint, string envVarEndpoint)
{
var options = new HoneycombOptions
{
Endpoint = "http://collector:4317/"
TracesEndpoint = configEndpoint
};
var values = new Dictionary<string, string>
{
{"OTEL_EXPORTER_OTLP_PROTOCOL", "grpc"},
};
{"OTEL_EXPORTER_OTLP_PROTOCOL", protocol},
{"HONEYCOMB_TRACES_ENDPOINT", envVarEndpoint},

options.ApplyEnvironmentOptions(new EnvironmentOptions(values));
Assert.Equal("http://collector:4317/", options.GetTracesEndpoint());
Assert.DoesNotContain("/v1/traces", options.GetTracesEndpoint());
}

[Fact]
public void AppendsTracesPathIfProtocolIsHttpProtobuf_EnvVars()
{
var options = new HoneycombOptions { };
var values = new Dictionary<string, string>
{
{"OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf"},
{"HONEYCOMB_API_ENDPOINT", "http://collector:4318/"}
};

options.ApplyEnvironmentOptions(new EnvironmentOptions(values));
Assert.Equal("http://collector:4318/v1/traces", options.GetTracesEndpoint());
Assert.Equal("http://collector:4318", options.GetTracesEndpoint());
Assert.DoesNotContain("/v1/traces", options.GetTracesEndpoint());
}

[Fact]
public void AppendsTracesPathIfProtocolIsHttpJson_EnvVars()
[Theory]
[InlineData("http/protobuf", "http://collector:4318/my-special-path", null)]
[InlineData("http/json", "http://collector:4318/my-special-path", null)]
[InlineData("http/protobuf", null, "http://collector:4318/my-special-path")]
[InlineData("http/json", null, "http://collector:4318/my-special-path")]
[InlineData("http/protobuf", "http://collector:4318/my-special-path", "http://collector:4318/my-special-path")]
[InlineData("http/json", "http://collector:4318/my-special-path", "http://collector:4318/my-special-path")]
public void DoesNotAppendTracesPathToGenericEndpointIfPathSpecified(string protocol, string configEndpoint, string envVarEndpoint)
{
var options = new HoneycombOptions { };
var values = new Dictionary<string, string>
var options = new HoneycombOptions
{
{"OTEL_EXPORTER_OTLP_PROTOCOL", "http/json"},
{"HONEYCOMB_API_ENDPOINT", "http://collector:4318/"}
Endpoint = configEndpoint
};

options.ApplyEnvironmentOptions(new EnvironmentOptions(values));
Assert.Equal("http://collector:4318/v1/traces", options.GetTracesEndpoint());
}

[Fact]
public void DoesNotAppendTracesPathIfProtocolIsGrpc_EnvVars()
{
var options = new HoneycombOptions { };
var values = new Dictionary<string, string>
{
{"OTEL_EXPORTER_OTLP_PROTOCOL", "grpc"},
{"HONEYCOMB_API_ENDPOINT", "http://collector:4317/"}
{"OTEL_EXPORTER_OTLP_PROTOCOL", protocol},
{"HONEYCOMB_API_ENDPOINT", envVarEndpoint},
};

options.ApplyEnvironmentOptions(new EnvironmentOptions(values));
Assert.Equal("http://collector:4317/", options.GetTracesEndpoint());
Assert.Equal("http://collector:4318/my-special-path", options.GetTracesEndpoint());
Assert.DoesNotContain("/v1/traces", options.GetTracesEndpoint());
}

[Fact]
public void DoesNotAppendTracesPathToTracesEndpoint_Config()
[Theory]
[InlineData("http/protobuf", "http://collector:4318", null)]
[InlineData("http/json", "http://collector:4318", null)]
[InlineData("http/protobuf", null, "http://collector:4318")]
[InlineData("http/json", null, "http://collector:4318")]
[InlineData("http/protobuf", "http://collector:4318", "http://collector:4318")]
[InlineData("http/json", "http://collector:4318", "http://collector:4318")]
TylerHelmuth marked this conversation as resolved.
Show resolved Hide resolved
public void AppendsMetricsPathIfProtocolIsHttp(string protocol, string configEndpoint, string envVarEndpoint)
{
var options = new HoneycombOptions
{
TracesEndpoint = "http://collector:4318/"
Endpoint = configEndpoint
};
var values = new Dictionary<string, string>
{
{"OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf"},
{"OTEL_EXPORTER_OTLP_PROTOCOL", protocol},
{"HONEYCOMB_API_ENDPOINT", envVarEndpoint},
};

options.ApplyEnvironmentOptions(new EnvironmentOptions(values));
Assert.Equal("http://collector:4318/", options.GetTracesEndpoint());
Assert.Equal($"{options.Endpoint}/v1/metrics", options.GetMetricsEndpoint());
}

[Fact]
public void DoesNotAppendTracesPathToTracesEndpoint_EnvVars()
[Theory]
[InlineData("http://collector:4317", null)]
[InlineData(null, "http://collector:4317")]
[InlineData("http://collector:4317", "http://collector:4317")]
public void DoesNotAppendMetricsPathIfProtocolIsGrpc(string configEndpoint, string envVarEndpoint)
{
var options = new HoneycombOptions { };
var options = new HoneycombOptions
{
Endpoint = configEndpoint
};
var values = new Dictionary<string, string>
{
{"OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf"},
{"HONEYCOMB_TRACES_ENDPOINT", "http://collector:4318/"}
{"OTEL_EXPORTER_OTLP_PROTOCOL", "grpc"},
{"HONEYCOMB_API_ENDPOINT", envVarEndpoint},
};

options.ApplyEnvironmentOptions(new EnvironmentOptions(values));
Assert.Equal("http://collector:4318/", options.GetTracesEndpoint());

Assert.Equal("http://collector:4317/", options.GetMetricsEndpoint());
Assert.DoesNotContain("/v1/metrics", options.GetMetricsEndpoint());
}

[Fact]
public void DoesNotAppendTracesPathToGenericEndpointIfPathSpecified_Config()
[Theory]
[InlineData("http/protobuf", "http://collector:4318", null)]
[InlineData("http/json", "http://collector:4318", null)]
[InlineData("http/protobuf", null, "http://collector:4318")]
[InlineData("http/json", null, "http://collector:4318")]
[InlineData("http/protobuf", "http://collector:4318", "http://collector:4318")]
[InlineData("http/json", "http://collector:4318", "http://collector:4318")]
public void DoesNotAppendMetricsPathToMetricsEndpoints(string protocol, string configEndpoint, string envVarEndpoint)
{
var options = new HoneycombOptions
{
Endpoint = "http://collector:4318/my-special-path"
MetricsEndpoint = configEndpoint
};
var values = new Dictionary<string, string>
{
{"OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf"},
{"OTEL_EXPORTER_OTLP_PROTOCOL", protocol},
{"HONEYCOMB_METRICS_ENDPOINT", envVarEndpoint},

};

options.ApplyEnvironmentOptions(new EnvironmentOptions(values));
Assert.Equal("http://collector:4318/my-special-path", options.GetTracesEndpoint());
Assert.Equal("http://collector:4318", options.GetMetricsEndpoint());
Assert.DoesNotContain("/v1/metrics", options.GetMetricsEndpoint());
}

[Fact]
public void DoesNotAppendTracesPathToGenericEndpointIfPathSpecified_EnvVars()
[Theory]
[InlineData("http/protobuf", "http://collector:4318/my-special-path", null)]
[InlineData("http/json", "http://collector:4318/my-special-path", null)]
[InlineData("http/protobuf", null, "http://collector:4318/my-special-path")]
[InlineData("http/json", null, "http://collector:4318/my-special-path")]
[InlineData("http/protobuf", "http://collector:4318/my-special-path", "http://collector:4318/my-special-path")]
[InlineData("http/json", "http://collector:4318/my-special-path", "http://collector:4318/my-special-path")]
public void DoesNotAppendMetricsPathToGenericEndpointIfPathSpecified(string protocol, string configEndpoint, string envVarEndpoint)
{
var options = new HoneycombOptions { };
var options = new HoneycombOptions
{
Endpoint = configEndpoint
};
var values = new Dictionary<string, string>
{
{"OTEL_EXPORTER_OTLP_PROTOCOL", "http/protobuf"},
{"HONEYCOMB_API_ENDPOINT", "http://collector:4318/my-special-path"}
{"OTEL_EXPORTER_OTLP_PROTOCOL", protocol},
{"HONEYCOMB_API_ENDPOINT", envVarEndpoint},
};

options.ApplyEnvironmentOptions(new EnvironmentOptions(values));
Assert.Equal("http://collector:4318/my-special-path", options.GetTracesEndpoint());
Assert.Equal("http://collector:4318/my-special-path", options.GetMetricsEndpoint());
Assert.DoesNotContain("/v1/metrics", options.GetMetricsEndpoint());
}

[Fact]
Expand Down