Support customized OTLP endpoint for log example (#4066)

* 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>
This commit is contained in:
Tom Tan 2023-01-10 13:00:14 -08:00 committed by GitHub
parent 1c69788184
commit f98f8fe166
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 15 deletions

View File

@ -112,6 +112,9 @@ namespace Examples.Console
[Option("useExporter", Default = "console", HelpText = "Options include otlp or console.", Required = false)] [Option("useExporter", Default = "console", HelpText = "Options include otlp or console.", Required = false)]
public string UseExporter { get; set; } 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)] [Option('p', "useGrpc", HelpText = "Use gRPC or HTTP when using the OTLP exporter", Required = false, Default = true)]
public bool UseGrpc { get; set; } public bool UseGrpc { get; set; }
} }
@ -149,7 +152,7 @@ namespace Examples.Console
[Verb("otlp", HelpText = "Specify the options required to test OpenTelemetry Protocol (OTLP)")] [Verb("otlp", HelpText = "Specify the options required to test OpenTelemetry Protocol (OTLP)")]
internal class OtlpOptions 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; } public string Endpoint { get; set; }
[Option('p', "protocol", HelpText = "Transport protocol used by exporter. Supported values: grpc and http/protobuf.", Default = "grpc")] [Option('p', "protocol", HelpText = "Transport protocol used by exporter. Supported values: grpc and http/protobuf.", Default = "grpc")]
@ -162,6 +165,9 @@ namespace Examples.Console
[Option("useExporter", Default = "otlp", HelpText = "Options include otlp or console.", Required = false)] [Option("useExporter", Default = "otlp", HelpText = "Options include otlp or console.", Required = false)]
public string UseExporter { get; set; } 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")] [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; } public string Protocol { get; set; }
} }

View File

@ -47,7 +47,7 @@ namespace Examples.Console
* Open another terminal window at the examples/Console/ directory and * Open another terminal window at the examples/Console/ directory and
* launch the OTLP example by running: * 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. * The OpenTelemetry Collector will output all received logs to the stdout of its terminal.
* *
@ -58,28 +58,29 @@ namespace Examples.Console
// See: https://docs.microsoft.com/aspnet/core/grpc/troubleshoot#call-insecure-grpc-services-with-net-core-client // 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); AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true);
var protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
if (options.Protocol.Trim().ToLower().Equals("grpc")) if (options.Protocol.Trim().ToLower().Equals("grpc"))
{ {
opt.AddOtlpExporter(otlpOptions => protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
{
otlpOptions.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.Grpc;
});
} }
else if (options.Protocol.Trim().ToLower().Equals("http/protobuf")) else if (options.Protocol.Trim().ToLower().Equals("http/protobuf"))
{ {
opt.AddOtlpExporter(otlpOptions => protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
{
otlpOptions.Protocol = OpenTelemetry.Exporter.OtlpExportProtocol.HttpProtobuf;
});
} }
else else
{ {
System.Console.WriteLine($"Export protocol {options.Protocol} is not supported. Default protocol 'grpc' will be used."); 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 else
{ {

View File

@ -51,7 +51,7 @@ namespace Examples.Console
* Open another terminal window at the examples/Console/ directory and * Open another terminal window at the examples/Console/ directory and
* launch the OTLP example by running: * 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. * The OpenTelemetry Collector will output all received metrics to the stdout of its terminal.
* *
@ -67,6 +67,11 @@ namespace Examples.Console
{ {
exporterOptions.Protocol = options.UseGrpc ? OtlpExportProtocol.Grpc : OtlpExportProtocol.HttpProtobuf; 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.PeriodicExportingMetricReaderOptions.ExportIntervalMilliseconds = options.DefaultCollectionPeriodMilliseconds;
metricReaderOptions.TemporalityPreference = options.IsDelta ? MetricReaderTemporalityPreference.Delta : MetricReaderTemporalityPreference.Cumulative; metricReaderOptions.TemporalityPreference = options.IsDelta ? MetricReaderTemporalityPreference.Delta : MetricReaderTemporalityPreference.Cumulative;
}); });