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

Otlp http exporter: allow endpoint override #2492

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
b4d1d8a
#2483: added read-only option properties for traces and metrics and a…
rypdal Oct 18, 2021
292dde7
#2483: fixed text formatting to fulfill build checks
rypdal Oct 18, 2021
bfa5182
#2483: removed trailing spaces + try auto properties for successful b…
rypdal Oct 18, 2021
57a1ab9
#2492: can't avoid traces and metrics endpoints adjustment
rypdal Oct 18, 2021
d164fe0
#2492: introduced endpoint default constant
rypdal Oct 18, 2021
62516c0
#2483: changed property descriptions
rypdal Oct 18, 2021
cb9288c
#2492: Uri instead of string as default for endpoint
rypdal Oct 19, 2021
3b88117
Apply suggestions from code review
rypdal Oct 19, 2021
08b6767
#2492: initialize internal endpoint variable in constructor instead o…
rypdal Oct 19, 2021
a5cb236
Merge branch 'main' into issue/Otlp-http-exporters-allow-endpoint-ove…
rypdal Oct 20, 2021
011f615
#2492: extracted from options and stored export traces uri in export …
rypdal Oct 20, 2021
382bc7d
#2492:
rypdal Oct 20, 2021
9b2e71b
#2492: fixed misspelling
rypdal Oct 20, 2021
3b6ac6c
#2492: fixed tests
rypdal Oct 20, 2021
10fe58f
Merge branch 'main' into issue/Otlp-http-exporters-allow-endpoint-ove…
rypdal Oct 21, 2021
1940a50
#2492: renaming and rephrasing as suggested in review
rypdal Oct 21, 2021
5725b76
Merge branch 'main' into issue/Otlp-http-exporters-allow-endpoint-ove…
cijothomas Oct 27, 2021
99cf7a5
Merge branch 'main' into issue/Otlp-http-exporters-allow-endpoint-ove…
rypdal Oct 27, 2021
93ebabd
#2492: use helper methods for reading env vars + proper variable nami…
rypdal Oct 27, 2021
be2d6e7
Merge branch 'main' into issue/Otlp-http-exporters-allow-endpoint-ove…
cijothomas Oct 27, 2021
7e0f732
Merge branch 'main' into issue/Otlp-http-exporters-allow-endpoint-ove…
rypdal Nov 5, 2021
a9425ad
#2492: review suggestions + actualized test names
rypdal Nov 5, 2021
44a24bc
Merge branch 'main' into issue/Otlp-http-exporters-allow-endpoint-ove…
cijothomas Nov 11, 2021
c522b34
Merge branch 'main' into issue/Otlp-http-exporters-allow-endpoint-ove…
cijothomas Nov 16, 2021
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
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ internal sealed class OtlpHttpTraceExportClient : BaseOtlpHttpExportClient<OtlpC
public OtlpHttpTraceExportClient(OtlpExporterOptions options, HttpClient httpClient = null)
: base(options, httpClient)
{
this.exportTracesUri = this.Options.Endpoint.AppendPathIfNotPresent(OtlpExporterOptions.TracesExportPath);
this.exportTracesUri = this.Options.Endpoint;
}

protected override HttpRequestMessage CreateHttpRequest(OtlpCollector.ExportTraceServiceRequest exportRequest)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using Grpc.Core;
using OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient;
using OpenTelemetry.Internal;
#if NETSTANDARD2_1 || NET5_0_OR_GREATER
using Grpc.Net.Client;
#endif
Expand Down Expand Up @@ -103,7 +104,26 @@ public static THeaders GetHeaders<THeaders>(this OtlpExporterOptions options, Ac
_ => null,
};

public static Uri AppendPathIfNotPresent(this Uri uri, string path)
internal static void AppendExportPath(this OtlpExporterOptions options, Uri initialEndpoint, string exportRelativePath)
{
// The exportRelativePath is only appended when the options.Endpoint property wasn't set by the user,
// the protocol is HttpProtobuf and the OTEL_EXPORTER_OTLP_ENDPOINT environment variable
// is present. If the user provides a custom value for options.Endpoint that value is taken as is.
if (ReferenceEquals(initialEndpoint, options.Endpoint))
{
if (options.Protocol == OtlpExportProtocol.HttpProtobuf)
{
if (EnvironmentVariableHelper.LoadUri(OtlpExporterOptions.EndpointEnvVarName, out Uri endpoint))
{
// At this point we can conclude that endpoint was initialized from OTEL_EXPORTER_OTLP_ENDPOINT
// and has to be appended by export relative path (traces/metrics).
options.Endpoint = endpoint.AppendPathIfNotPresent(exportRelativePath);
}
}
}
}

internal static Uri AppendPathIfNotPresent(this Uri uri, string path)
{
var absoluteUri = uri.AbsoluteUri;
var separator = string.Empty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ public static MeterProviderBuilder AddOtlpExporter(this MeterProviderBuilder bui

private static MeterProviderBuilder AddOtlpExporter(MeterProviderBuilder builder, OtlpExporterOptions options, Action<OtlpExporterOptions> configure = null)
{
var initialEndpoint = options.Endpoint;

configure?.Invoke(options);

options.AppendExportPath(initialEndpoint, OtlpExporterOptions.MetricsExportPath);

var metricExporter = new OtlpMetricExporter(options);
var metricReader = new PeriodicExportingMetricReader(metricExporter, options.MetricExportIntervalMilliseconds);
metricReader.PreferredAggregationTemporality = options.AggregationTemporality;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ public static TracerProviderBuilder AddOtlpExporter(this TracerProviderBuilder b

private static TracerProviderBuilder AddOtlpExporter(TracerProviderBuilder builder, OtlpExporterOptions exporterOptions, Action<OtlpExporterOptions> configure = null)
{
var originalEndpoint = exporterOptions.Endpoint;

configure?.Invoke(exporterOptions);

exporterOptions.AppendExportPath(originalEndpoint, OtlpExporterOptions.TracesExportPath);

var otlpExporter = new OtlpTraceExporter(exporterOptions);

if (exporterOptions.ExportProcessorType == ExportProcessorType.Simple)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ void RunTest(Batch<Activity> batch)
Assert.True(result);
Assert.NotNull(httpRequest);
Assert.Equal(HttpMethod.Post, httpRequest.Method);
Assert.Equal("http://localhost:4317/v1/traces", httpRequest.RequestUri.AbsoluteUri);
Assert.Equal("http://localhost:4317/", httpRequest.RequestUri.AbsoluteUri);
Assert.Equal(2, httpRequest.Headers.Count());
Assert.Contains(httpRequest.Headers, h => h.Key == header1.Name && h.Value.First() == header1.Value);
Assert.Contains(httpRequest.Headers, h => h.Key == header2.Name && h.Value.First() == header2.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,5 +131,68 @@ public void AppendPathIfNotPresent_TracesPath_AppendsCorrectly(string inputUri,

Assert.Equal(expectedUri, resultUri.AbsoluteUri);
}

[Fact]
public void AppendExportPath_EndpointNotSet_EnvironmentVariableNotDefined_NotAppended()
{
ClearEndpointEnvVar();

var options = new OtlpExporterOptions { Protocol = OtlpExportProtocol.HttpProtobuf };

options.AppendExportPath(options.Endpoint, "test/path");

Assert.Equal("http://localhost:4317/", options.Endpoint.AbsoluteUri);
}

[Fact]
public void AppendExportPath_EndpointNotSet_EnvironmentVariableDefined_Appended()
{
Environment.SetEnvironmentVariable(OtlpExporterOptions.EndpointEnvVarName, "http://test:8888");

var options = new OtlpExporterOptions { Protocol = OtlpExportProtocol.HttpProtobuf };

options.AppendExportPath(options.Endpoint, "test/path");

Assert.Equal("http://test:8888/test/path", options.Endpoint.AbsoluteUri);

ClearEndpointEnvVar();
}

[Fact]
public void AppendExportPath_EndpointSetEqualToEnvironmentVariable_EnvironmentVariableDefined_NotAppended()
{
Environment.SetEnvironmentVariable(OtlpExporterOptions.EndpointEnvVarName, "http://test:8888");

var options = new OtlpExporterOptions { Protocol = OtlpExportProtocol.HttpProtobuf };
var originalEndpoint = options.Endpoint;
options.Endpoint = new Uri("http://test:8888");

options.AppendExportPath(originalEndpoint, "test/path");

Assert.Equal("http://test:8888/", options.Endpoint.AbsoluteUri);

ClearEndpointEnvVar();
}

[Theory]
[InlineData("http://localhost:4317/")]
[InlineData("http://test:8888/")]
public void AppendExportPath_EndpointSet_EnvironmentVariableNotDefined_NotAppended(string endpoint)
{
ClearEndpointEnvVar();

var options = new OtlpExporterOptions { Protocol = OtlpExportProtocol.HttpProtobuf };
var originalEndpoint = options.Endpoint;
options.Endpoint = new Uri(endpoint);

options.AppendExportPath(originalEndpoint, "test/path");

Assert.Equal(endpoint, options.Endpoint.AbsoluteUri);
}

private static void ClearEndpointEnvVar()
{
Environment.SetEnvironmentVariable(OtlpExporterOptions.EndpointEnvVarName, null);
}
}
}