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

Support customized OTLP endpoint for log example #4066

Merged
merged 6 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
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
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);
}
Comment on lines +79 to +82
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I guess TestMetrics.cs is also missing this functionality. At a minimum, probably makes sense to add it there too. Though, seems like there may be an opportunity to share some code between these examples, but maybe makes sense to consider that separately.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the same Endpoint command line option to metrics example. I did some minor cleanup to remove dup code, but agree that further cleanup could be done separately.

});
}
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