From f98f8fe166d59de008309e6e1ea3afaa5379afd5 Mon Sep 17 00:00:00 2001 From: Tom Tan Date: Tue, 10 Jan 2023 13:00:14 -0800 Subject: [PATCH] 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> --- examples/Console/Program.cs | 8 +++++++- examples/Console/TestLogs.cs | 27 ++++++++++++++------------- examples/Console/TestMetrics.cs | 7 ++++++- 3 files changed, 27 insertions(+), 15 deletions(-) diff --git a/examples/Console/Program.cs b/examples/Console/Program.cs index b7adda5f75c..8dffaef6a03 100644 --- a/examples/Console/Program.cs +++ b/examples/Console/Program.cs @@ -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; } } @@ -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")] @@ -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; } } diff --git a/examples/Console/TestLogs.cs b/examples/Console/TestLogs.cs index a805c4447f2..98d60ab21e8 100644 --- a/examples/Console/TestLogs.cs +++ b/examples/Console/TestLogs.cs @@ -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. * @@ -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 { diff --git a/examples/Console/TestMetrics.cs b/examples/Console/TestMetrics.cs index 1aafd87387b..213423e6161 100644 --- a/examples/Console/TestMetrics.cs +++ b/examples/Console/TestMetrics.cs @@ -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. * @@ -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; });