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 9 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 @@ -21,3 +21,5 @@ static OpenTelemetry.Trace.OtlpTraceExporterHelperExtensions.AddOtlpExporter(thi
OpenTelemetry.Exporter.OtlpExportProtocol
OpenTelemetry.Exporter.OtlpExportProtocol.Grpc = 0 -> OpenTelemetry.Exporter.OtlpExportProtocol
OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf = 1 -> OpenTelemetry.Exporter.OtlpExportProtocol
OpenTelemetry.Exporter.OtlpExporterOptions.TracesEndpoint.get -> System.Uri
OpenTelemetry.Exporter.OtlpExporterOptions.MetricsEndpoint.get -> System.Uri
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ OpenTelemetry.Exporter.OtlpExporterOptions.Protocol.set -> void
OpenTelemetry.Exporter.OtlpExportProtocol
OpenTelemetry.Exporter.OtlpExportProtocol.Grpc = 0 -> OpenTelemetry.Exporter.OtlpExportProtocol
OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf = 1 -> OpenTelemetry.Exporter.OtlpExportProtocol
OpenTelemetry.Exporter.OtlpExporterOptions.TracesEndpoint.get -> System.Uri
OpenTelemetry.Exporter.OtlpExporterOptions.MetricsEndpoint.get -> System.Uri
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ OpenTelemetry.Exporter.OtlpExporterOptions.Protocol.set -> void
OpenTelemetry.Exporter.OtlpExportProtocol
OpenTelemetry.Exporter.OtlpExportProtocol.Grpc = 0 -> OpenTelemetry.Exporter.OtlpExportProtocol
OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf = 1 -> OpenTelemetry.Exporter.OtlpExportProtocol
OpenTelemetry.Exporter.OtlpExporterOptions.TracesEndpoint.get -> System.Uri
OpenTelemetry.Exporter.OtlpExporterOptions.MetricsEndpoint.get -> System.Uri
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// limitations under the License.
// </copyright>

using System;
using System.IO;
using System.Net;
using System.Net.Http;
Expand All @@ -33,17 +32,15 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClie
internal sealed class OtlpHttpTraceExportClient : BaseOtlpHttpExportClient<OtlpCollector.ExportTraceServiceRequest>
{
internal const string MediaContentType = "application/x-protobuf";
private readonly Uri exportTracesUri;

public OtlpHttpTraceExportClient(OtlpExporterOptions options, HttpClient httpClient = null)
: base(options, httpClient)
{
this.exportTracesUri = this.Options.Endpoint.AppendPathIfNotPresent(OtlpExporterOptions.TracesExportPath);
}

protected override HttpRequestMessage CreateHttpRequest(OtlpCollector.ExportTraceServiceRequest exportRequest)
{
var request = new HttpRequestMessage(HttpMethod.Post, this.exportTracesUri);
var request = new HttpRequestMessage(HttpMethod.Post, this.Options.TracesEndpoint);
utpilla marked this conversation as resolved.
Show resolved Hide resolved
foreach (var header in this.Headers)
{
request.Headers.Add(header.Key, header.Value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,46 @@ public class OtlpExporterOptions
internal const string TracesExportPath = "v1/traces";
internal const string MetricsExportPath = "v1/metrics";

private static readonly Uri DefaultEndpoint = new Uri("http://localhost:4317");

private Uri endpoint = DefaultEndpoint;

/// <summary>
/// Initializes a new instance of the <see cref="OtlpExporterOptions"/> class.
/// </summary>
public OtlpExporterOptions()
{
try
{
// Protocol initialization should come before the endpoint as endpoint's value may be adjusted according to the protocols value.
string protocolEnvVar = Environment.GetEnvironmentVariable(ProtocolEnvVarName);
if (!string.IsNullOrEmpty(protocolEnvVar))
{
var protocol = protocolEnvVar.ToOtlpExportProtocol();
if (protocol.HasValue)
{
this.Protocol = protocol.Value;
}
else
{
OpenTelemetryProtocolExporterEventSource.Log.UnsupportedProtocol(protocolEnvVar);
}
}

string endpointEnvVar = Environment.GetEnvironmentVariable(EndpointEnvVarName);
if (!string.IsNullOrEmpty(endpointEnvVar))
{
if (Uri.TryCreate(endpointEnvVar, UriKind.Absolute, out var endpoint))
{
this.Endpoint = endpoint;
this.endpoint = endpoint;
this.TracesEndpoint = endpoint;
this.MetricsEndpoint = endpoint;

if (this.Protocol == OtlpExportProtocol.HttpProtobuf)
{
this.TracesEndpoint = endpoint.AppendPathIfNotPresent(TracesExportPath);
this.MetricsEndpoint = endpoint.AppendPathIfNotPresent(MetricsExportPath);
}
}
else
{
Expand All @@ -74,20 +101,6 @@ public OtlpExporterOptions()
OpenTelemetryProtocolExporterEventSource.Log.FailedToParseEnvironmentVariable(TimeoutEnvVarName, timeoutEnvVar);
}
}

string protocolEnvVar = Environment.GetEnvironmentVariable(ProtocolEnvVarName);
if (!string.IsNullOrEmpty(protocolEnvVar))
{
var protocol = protocolEnvVar.ToOtlpExportProtocol();
if (protocol.HasValue)
{
this.Protocol = protocol.Value;
}
else
{
OpenTelemetryProtocolExporterEventSource.Log.UnsupportedProtocol(protocolEnvVar);
}
}
}
catch (SecurityException ex)
{
Expand All @@ -102,7 +115,36 @@ public OtlpExporterOptions()
/// Must be a valid Uri with scheme (http or https) and host, and
/// may contain a port and path. The default value is http://localhost:4317.
/// </summary>
public Uri Endpoint { get; set; } = new Uri("http://localhost:4317");
public Uri Endpoint
{
get
{
return this.endpoint;
}

set
{
this.endpoint = value;
this.TracesEndpoint = value;
this.MetricsEndpoint = value;
}
}

/// <summary>
/// Gets the target to which the exporter is going to send traces. The default value is http://localhost:4317.
/// Initially, this property is derived from the OTEL_EXPORTER_OTLP_ENDPOINT environment variable and adjusted according to the specification:
/// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#endpoint-urls-for-otlphttp.
/// If the <see cref="Endpoint"/> is set, then this property will be set with the same value.
/// </summary>
public Uri TracesEndpoint { get; private set; } = DefaultEndpoint;

/// <summary>
/// Gets the target to which the exporter is going to send metrics. The default value is http://localhost:4317.
/// Initially, this property is derived from the OTEL_EXPORTER_OTLP_ENDPOINT environment variable and adjusted according to the specification:
/// https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#endpoint-urls-for-otlphttp.
/// If the <see cref="Endpoint"/> is set, then this property will be set with the same value.
/// </summary>
public Uri MetricsEndpoint { get; private set; } = DefaultEndpoint;

/// <summary>
/// Gets or sets optional headers for the connection. Refer to the <a href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/protocol/exporter.md#specifying-headers-via-environment-variables">
Expand Down
8 changes: 8 additions & 0 deletions src/OpenTelemetry.Exporter.OpenTelemetryProtocol/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ setters take precedence over the environment variables.
* `Endpoint`: Target to which the exporter is going to send traces or metrics.
The endpoint must be a valid Uri with scheme (http or https) and host, and MAY
contain a port and path.
* `TracesEndpoint` (read only): Target to which the exporter is going to send
traces. Initially, the property is derived from the `OTEL_EXPORTER_OTLP_ENDPOINT`
environment variable and adjusted according to the specification.
If the Endpoint is set, then this property will be set with the same value.
* `MetricsEndpoint` (read only): Target to which the exporter is going to send
metrics. Initially, this property is derived from the `OTEL_EXPORTER_OTLP_ENDPOINT`
environment variable and adjusted according to the specification.
If the Endpoint is set, then this property will be set with the same value.
* `Headers`: Optional headers for the connection.
* `TimeoutMilliseconds` : Max waiting time for the backend to process a batch.
* `Protocol`: OTLP transport protocol. Supported values:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public void OtlpExporterOptions_Defaults()
var options = new OtlpExporterOptions();

Assert.Equal(new Uri("http://localhost:4317"), options.Endpoint);
Assert.Equal(new Uri("http://localhost:4317"), options.TracesEndpoint);
Assert.Equal(new Uri("http://localhost:4317"), options.MetricsEndpoint);
Assert.Null(options.Headers);
Assert.Equal(10000, options.TimeoutMilliseconds);
Assert.Equal(OtlpExportProtocol.Grpc, options.Protocol);
Expand All @@ -53,6 +55,8 @@ public void OtlpExporterOptions_EnvironmentVariableOverride()
var options = new OtlpExporterOptions();

Assert.Equal(new Uri("http://test:8888"), options.Endpoint);
Assert.Equal(new Uri($"http://test:8888/{OtlpExporterOptions.TracesExportPath}"), options.TracesEndpoint);
Assert.Equal(new Uri($"http://test:8888/{OtlpExporterOptions.MetricsExportPath}"), options.MetricsEndpoint);
Assert.Equal("A=2,B=3", options.Headers);
Assert.Equal(2000, options.TimeoutMilliseconds);
Assert.Equal(OtlpExportProtocol.HttpProtobuf, options.Protocol);
Expand Down Expand Up @@ -105,6 +109,8 @@ public void OtlpExporterOptions_SetterOverridesEnvironmentVariable()
};

Assert.Equal(new Uri("http://localhost:200"), options.Endpoint);
Assert.Equal(new Uri("http://localhost:200"), options.TracesEndpoint);
Assert.Equal(new Uri("http://localhost:200"), options.MetricsEndpoint);
Assert.Equal("C=3", options.Headers);
Assert.Equal(40000, options.TimeoutMilliseconds);
Assert.Equal(OtlpExportProtocol.HttpProtobuf, options.Protocol);
Expand Down