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

feat: Add Opentelemetry Export protocol option #2318

Closed
wants to merge 11 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using OpenTelemetry.Exporter;
using OpenTelemetry.Metrics;
using OpenTelemetry.Resources;
using Promitor.Agents.Core.Configuration.Server;
Expand Down Expand Up @@ -110,7 +111,7 @@ public static IServiceCollection AddAtlassianStatuspageClient(this IServiceColle
public static IServiceCollection DefineDependencies(this IServiceCollection services)
{
Guard.NotNull(services, nameof(services));

services.AddTransient<IMetricsDeclarationProvider, MetricsDeclarationProvider>();
services.AddTransient<IAzureScrapingSystemMetricsPublisher, AzureScrapingSystemMetricsPublisher>();
services.AddTransient<MetricScraperFactory>();
Expand Down Expand Up @@ -198,7 +199,7 @@ public static IServiceCollection UseMetricSinks(this IServiceCollection services
if (metricSinkConfiguration?.OpenTelemetryCollector != null
&& string.IsNullOrWhiteSpace(metricSinkConfiguration.OpenTelemetryCollector.CollectorUri) == false)
{
AddOpenTelemetryCollectorMetricSink(metricSinkConfiguration.OpenTelemetryCollector.CollectorUri, agentVersion, services, metricSinkAsciiTable);
AddOpenTelemetryCollectorMetricSink(metricSinkConfiguration.OpenTelemetryCollector, agentVersion, services, metricSinkAsciiTable);
}

AnsiConsole.Write(metricSinkAsciiTable);
Expand All @@ -224,10 +225,10 @@ private static void AddAtlassianStatuspageMetricSink(string pageId, IServiceColl

const string OpenTelemetryServiceName = "promitor-scraper";

private static void AddOpenTelemetryCollectorMetricSink(string collectorUri, string agentVersion, IServiceCollection services, Table metricSinkAsciiTable)
private static void AddOpenTelemetryCollectorMetricSink(Promitor.Integrations.Sinks.OpenTelemetry.Configuration.OpenTelemetryCollectorSinkConfiguration otelConfiguration, string agentVersion, IServiceCollection services, Table metricSinkAsciiTable)
{
metricSinkAsciiTable.AddRow("OpenTelemetry Collector", $"Url: {collectorUri}.");

metricSinkAsciiTable.AddRow("OpenTelemetry Collector", $"Url: {otelConfiguration.CollectorUri}.");
bunkrur marked this conversation as resolved.
Show resolved Hide resolved
metricSinkAsciiTable.AddRow("OpenTelemetry Collector", $"Protocol: {otelConfiguration.Collector.CollectorProtocol}.");
var resourceBuilder = ResourceBuilder.CreateDefault()
.AddService(OpenTelemetryServiceName, serviceVersion: agentVersion);

Expand All @@ -236,15 +237,20 @@ private static void AddOpenTelemetryCollectorMetricSink(string collectorUri, str
{
metricsBuilder.SetResourceBuilder(resourceBuilder)
.AddMeter("Promitor.Scraper.Metrics.AzureMonitor")
.AddOtlpExporter(options => options.Endpoint = new Uri(collectorUri));
.AddOtlpExporter(options =>
{
options.Endpoint = new Uri(otelConfiguration.CollectorUri);
options.Protocol = (otelConfiguration.Collector.CollectorProtocol.Equals("http")) ? OtlpExportProtocol.HttpProtobuf : OtlpExportProtocol.Grpc;
bunkrur marked this conversation as resolved.
Show resolved Hide resolved
}
);
});
services.AddTransient<IMetricSink, OpenTelemetryCollectorMetricSink>();
services.AddTransient<OpenTelemetryCollectorMetricSink>();
services.AddOpenTelemetrySystemMetrics();
}

private static void AddStatsdMetricSink(IServiceCollection services, StatsdSinkConfiguration statsdConfiguration, Table metricSinkAsciiTable)
{
{
metricSinkAsciiTable.AddRow("StatsD", $"Url: {statsdConfiguration.Host}:{statsdConfiguration.Port}.");
metricSinkAsciiTable.AddRow("", $"Format: {statsdConfiguration.MetricFormat}.");

Expand Down Expand Up @@ -308,9 +314,9 @@ public static IServiceCollection AddScrapingMutex(this IServiceCollection servic
}

var serverConfiguration = configuration.GetSection("server").Get<ServerConfiguration>();

services.TryAdd(ServiceDescriptor.Singleton<IScrapingMutex, ScrapingMutex>(_ => ScrapingMutexBuilder(serverConfiguration)));

return services;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using OpenTelemetry.Exporter;
using Promitor.Agents.Core.Validation;
using Promitor.Agents.Core.Validation.Interfaces;
using Promitor.Agents.Core.Validation.Steps;
Expand Down Expand Up @@ -33,6 +34,18 @@ public ValidationResult Run()

var errorMessages = new List<string>();
var collectorUri = openTelemetryCollectorSinkConfiguration.CollectorUri;
var collectorProtocol = openTelemetryCollectorSinkConfiguration.Collector.CollectorProtocol;

// Protocol Validation
if (string.IsNullOrWhiteSpace(collectorProtocol))
{
errorMessages.Add("No Protocol for the OpenTelemetry Collector is configured.");
}
else if (collectorProtocol != "http" && collectorProtocol != "grpc")
{
errorMessages.Add($"Configured Protocol ({collectorProtocol}) for the OpenTelemetry Collector should be either 'http' or 'grpc'.");
}
bunkrur marked this conversation as resolved.
Show resolved Hide resolved
// URI Validation
if (string.IsNullOrWhiteSpace(collectorUri))
bunkrur marked this conversation as resolved.
Show resolved Hide resolved
{
errorMessages.Add("No URI for the OpenTelemetry Collector is configured.");
Expand All @@ -48,7 +61,7 @@ public ValidationResult Run()
errorMessages.Add($"Configured URI ({collectorUri}) for the OpenTelemetry Collector is not a valid URI.");
}
}

return errorMessages.Any() ? ValidationResult.Failure(ComponentName, errorMessages) : ValidationResult.Successful(ComponentName);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
namespace Promitor.Integrations.Sinks.OpenTelemetry.Configuration
using OpenTelemetry.Exporter;

namespace Promitor.Integrations.Sinks.OpenTelemetry.Configuration
{
public class OpenTelemetryCollectorSinkConfiguration
{
public string CollectorUri { get; set; }

public CollectorInfo Collector { get; set; }

public class CollectorInfo
bunkrur marked this conversation as resolved.
Show resolved Hide resolved
{
public string CollectorProtocol { get; set; } = "grpc";
Copy link
Owner

Choose a reason for hiding this comment

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

Let's introduce Uri here as well to be consistent and deprecate the old one

Copy link
Author

Choose a reason for hiding this comment

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

Added to new class but kept filename :)

Copy link
Author

Choose a reason for hiding this comment

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

see other comment about class after I nuked the unit tests. Also, a change to the class structure or a nested class would maybe need runtime.yaml changes, which I think we should avoid?

bunkrur marked this conversation as resolved.
Show resolved Hide resolved
}
}
}