-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(api): Replace Elastic APM with Open Telemetry
- Loading branch information
1 parent
de5d5f7
commit f53ddf9
Showing
24 changed files
with
932 additions
and
1,796 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
source/Gnomeshade.WebApi/Configuration/ConfigureOtlpExporterOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2021 Valters Melnalksnis | ||
// Licensed under the GNU Affero General Public License v3.0 or later. | ||
// See LICENSE.txt file in the project root for full license information. | ||
|
||
using Gnomeshade.WebApi.Configuration.Options; | ||
|
||
using Microsoft.Extensions.Options; | ||
|
||
using OpenTelemetry.Exporter; | ||
|
||
namespace Gnomeshade.WebApi.Configuration; | ||
|
||
internal sealed class ConfigureOtlpExporterOptions : IConfigureOptions<OtlpExporterOptions> | ||
{ | ||
private readonly IOptionsMonitor<OpenTelemetryOptions> _optionsMonitor; | ||
|
||
public ConfigureOtlpExporterOptions(IOptionsMonitor<OpenTelemetryOptions> optionsMonitor) | ||
{ | ||
_optionsMonitor = optionsMonitor; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public void Configure(OtlpExporterOptions options) | ||
{ | ||
var telemetryOptions = _optionsMonitor.CurrentValue; | ||
|
||
options.Endpoint = telemetryOptions.ExporterEndpoint; | ||
options.Protocol = OtlpExportProtocol.Grpc; | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
source/Gnomeshade.WebApi/Configuration/OpenTelemetryExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// Copyright 2021 Valters Melnalksnis | ||
// Licensed under the GNU Affero General Public License v3.0 or later. | ||
// See LICENSE.txt file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
|
||
using Gnomeshade.WebApi.Configuration.Options; | ||
|
||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
|
||
using Npgsql; | ||
|
||
using OpenTelemetry.Exporter; | ||
using OpenTelemetry.Logs; | ||
using OpenTelemetry.Metrics; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
|
||
namespace Gnomeshade.WebApi.Configuration; | ||
|
||
internal static class OpenTelemetryExtensions | ||
{ | ||
internal static IServiceCollection AddGnomeshadeOpenTelemetry( | ||
this IServiceCollection services, | ||
IConfiguration configuration) | ||
{ | ||
var telemetryOptions = configuration.GetValid<OpenTelemetryOptions>(); | ||
if (!telemetryOptions.Enabled) | ||
{ | ||
return services; | ||
} | ||
|
||
var serviceName = telemetryOptions.ServiceName; | ||
var serviceVersion = telemetryOptions.ServiceVersion; | ||
var environment = configuration.GetValue<string>("environment") ?? "Development"; | ||
|
||
var resourceBuilder = ResourceBuilder | ||
.CreateEmpty() | ||
.AddTelemetrySdk() | ||
.AddService(serviceName, serviceVersion: serviceVersion) | ||
.AddAttributes(new Dictionary<string, object> { { "deployment.environment", environment } }); | ||
|
||
services | ||
.AddSingleton<IConfigureOptions<OtlpExporterOptions>, ConfigureOtlpExporterOptions>() | ||
.AddOpenTelemetry() | ||
.WithTracing(builder => | ||
{ | ||
builder.AddOtlpExporter(); | ||
builder.SetResourceBuilder(resourceBuilder); | ||
builder.AddSource(serviceName); | ||
|
||
builder | ||
.AddHttpClientInstrumentation(options => options.RecordException = true) | ||
.AddAspNetCoreInstrumentation(options => options.RecordException = true) | ||
.AddNpgsql(); | ||
}) | ||
.WithMetrics(builder => | ||
{ | ||
builder.AddOtlpExporter(); | ||
builder.SetResourceBuilder(resourceBuilder); | ||
|
||
builder | ||
.AddProcessInstrumentation() | ||
.AddHttpClientInstrumentation() | ||
.AddAspNetCoreInstrumentation() | ||
.AddRuntimeInstrumentation(); | ||
}); | ||
|
||
services.AddLogging(builder => builder.AddOpenTelemetry(options => | ||
{ | ||
options.AddOtlpExporter(); | ||
options.SetResourceBuilder(resourceBuilder); | ||
options.IncludeScopes = true; | ||
options.IncludeFormattedMessage = true; | ||
options.ParseStateValues = true; | ||
})); | ||
|
||
return services; | ||
} | ||
} |
38 changes: 0 additions & 38 deletions
38
source/Gnomeshade.WebApi/Configuration/Options/ElasticSearchLoggingOptions.cs
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
source/Gnomeshade.WebApi/Configuration/Options/OpenTelemetryOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2021 Valters Melnalksnis | ||
// Licensed under the GNU Affero General Public License v3.0 or later. | ||
// See LICENSE.txt file in the project root for full license information. | ||
|
||
using System; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Reflection; | ||
|
||
namespace Gnomeshade.WebApi.Configuration.Options; | ||
|
||
/// <summary>Options for configuring Open Telemetry.</summary> | ||
public sealed class OpenTelemetryOptions | ||
{ | ||
internal const string SectionName = "OpenTelemetry"; | ||
|
||
/// <summary>Gets a value indicating whether to enable Open Telemetry.</summary> | ||
public bool Enabled { get; init; } = true; | ||
|
||
/// <summary>Gets the name of the service.</summary> | ||
[Required] | ||
public string ServiceName { get; init; } = "Gnomeshade"; | ||
|
||
/// <summary>Gets the version of the service. Defaults to the assembly version.</summary> | ||
[Required] | ||
public string ServiceVersion { get; init; } = Assembly.GetExecutingAssembly().GetName().Version?.ToString()!; | ||
|
||
/// <summary>Gets the endpoint to which to send the telemetry. Defaults to localhost.</summary> | ||
[Required] | ||
public Uri ExporterEndpoint { get; init; } = new("http://localhost:4317"); | ||
} |
Oops, something went wrong.