Skip to content

Commit

Permalink
Support customized OTLP endpoint for log example (#4066)
Browse files Browse the repository at this point in the history
* Support customized OTLP endpoint for log example

* Add Endpoint command line option to TestMetrics

* Fix dotnet format

Co-authored-by: Alan West <3676547+alanwest@users.noreply.github.com>
  • Loading branch information
ThomsonTan and alanwest authored Jan 10, 2023
1 parent 1c69788 commit f98f8fe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
8 changes: 7 additions & 1 deletion examples/Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ internal class MetricsOptions
[Option("useExporter", Default = "console", HelpText = "Options include otlp or console.", Required = false)]
public string UseExporter { get; set; }

[Option('e', "endpoint", HelpText = "Target to which the exporter is going to send metrics (default value depends on protocol).", Default = null)]
public string Endpoint { get; set; }

[Option('p', "useGrpc", HelpText = "Use gRPC or HTTP when using the OTLP exporter", Required = false, Default = true)]
public bool UseGrpc { get; set; }
}
Expand Down Expand Up @@ -149,7 +152,7 @@ internal class OpenTracingShimOptions
[Verb("otlp", HelpText = "Specify the options required to test OpenTelemetry Protocol (OTLP)")]
internal class OtlpOptions
{
[Option('e', "endpoint", HelpText = "Target to which the exporter is going to send traces or metrics (default value depends on protocol).", Default = null)]
[Option('e', "endpoint", HelpText = "Target to which the exporter is going to send traces (default value depends on protocol).", Default = null)]
public string Endpoint { get; set; }

[Option('p', "protocol", HelpText = "Transport protocol used by exporter. Supported values: grpc and http/protobuf.", Default = "grpc")]
Expand All @@ -162,6 +165,9 @@ internal class LogsOptions
[Option("useExporter", Default = "otlp", HelpText = "Options include otlp or console.", Required = false)]
public string UseExporter { get; set; }

[Option('e', "endpoint", HelpText = "Target to which the OTLP exporter is going to send logs (default value depends on protocol).", Default = null)]
public string Endpoint { get; set; }

[Option('p', "protocol", HelpText = "Transport protocol used by OTLP exporter. Supported values: grpc and http/protobuf. Only applicable if Exporter is OTLP", Default = "grpc")]
public string Protocol { get; set; }
}
Expand Down
27 changes: 14 additions & 13 deletions examples/Console/TestLogs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ internal static object Run(LogsOptions options)
* Open another terminal window at the examples/Console/ directory and
* launch the OTLP example by running:
*
* dotnet run logs --useExporter otlp
* dotnet run logs --useExporter otlp -e http://localhost:4317
*
* The OpenTelemetry Collector will output all received logs to the stdout of its terminal.
*
Expand All @@ -58,28 +58,29 @@ internal static object Run(LogsOptions options)
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client
AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);

var protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;

if (options.Protocol.Trim().ToLower().Equals("grpc"))
{
opt.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
});
protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
}
else if (options.Protocol.Trim().ToLower().Equals("http/protobuf"))
{
opt.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
});
protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
}
else
{
System.Console.WriteLine($"Export protocol {options.Protocol} is not supported. Default protocol 'grpc' will be used.");
opt.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
});
}

opt.AddOtlpExporter(otlpOptions =>
{
otlpOptions.Protocol = protocol;
if (!string.IsNullOrWhiteSpace(options.Endpoint))
{
otlpOptions.Endpoint = new Uri(options.Endpoint);
}
});
}
else
{
Expand Down
7 changes: 6 additions & 1 deletion examples/Console/TestMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ internal static object Run(MetricsOptions options)
* Open another terminal window at the examples/Console/ directory and
* launch the OTLP example by running:
*
* dotnet run metrics --useExporter otlp
* dotnet run metrics --useExporter otlp -e http://localhost:4317
*
* The OpenTelemetry Collector will output all received metrics to the stdout of its terminal.
*
Expand All @@ -67,6 +67,11 @@ internal static object Run(MetricsOptions options)
{
exporterOptions.Protocol = options.UseGrpc ? OtlpExportProtocol.Grpc : OtlpExportProtocol.HttpProtobuf;

if (!string.IsNullOrWhiteSpace(options.Endpoint))
{
exporterOptions.Endpoint = new Uri(options.Endpoint);
}

metricReaderOptions.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = options.DefaultCollectionPeriodMilliseconds;
metricReaderOptions.TemporalityPreference = options.IsDelta ? MetricReaderTemporalityPreference.Delta : MetricReaderTemporalityPreference.Cumulative;
});
Expand Down

0 comments on commit f98f8fe

Please sign in to comment.