Skip to content

Commit

Permalink
feat: open telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Mar 26, 2022
1 parent 34d3726 commit 1888a8a
Show file tree
Hide file tree
Showing 94 changed files with 74,861 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,13 @@
<ProjectReference Include="..\IdentityServer\Duende\Aguacongas.IdentityServer.WsFederation.Duende\Aguacongas.IdentityServer.WsFederation.Duende.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.2.0-rc3" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.2.0-rc3" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc9" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.0.0-rc9" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions src/Aguacongas.TheIdServer.Duende/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,19 @@
}
]
},
"OpenTelemetryOptions": {
"ConsoleEnabled": false,
"Service": {
"Name": "TheIdServer.Duende",
"Version": "Next"
},
"Exporter": {
"Honeycomb": {
"ServiceName": "TheIdServer.Duende",
"Dataset": "TheIdServer.Duende"
}
}
},
"WsFederationOptions": {
"ClaimTypesOffered": [
{
Expand Down
13 changes: 13 additions & 0 deletions src/Aguacongas.TheIdServer.IS4/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -386,5 +386,18 @@
]
}
]
},
"OpenTelemetryOptions": {
"ConsoleEnabled": false,
"Service": {
"Name": "TheIdServer.IS4",
"Version": "Next"
},
"Exporter": {
"Honeycomb": {
"ServiceName": "TheIdServer.IS4",
"Dataset": "TheIdServer.IS4"
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
using Aguacongas.IdentityServer.EntityFramework.Store;
using Aguacongas.TheIdServer.Data;
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Aguacongas.TheIdServer.Options.OpenTelemetry;

namespace Microsoft.Extensions.DependencyInjection
{
Expand Down Expand Up @@ -189,6 +190,7 @@ public static IServiceCollection AddTheIdServer(this IServiceCollection services
.AddRazorPages(options => options.Conventions.AuthorizeAreaFolder("Identity", "/Account"));

ConfigureHealthChecks(services, dbType, isProxy, configurationManager);

return services;
}

Expand Down
11 changes: 11 additions & 0 deletions src/Aguacongas.TheIdServer.Shared/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// Project: Aguafrommars/TheIdServer
// Copyright (c) 2022 @Olivier Lefebvre
using Aguacongas.TheIdServer;
using Aguacongas.TheIdServer.Options.OpenTelemetry;
using Microsoft.AspNetCore.Builder;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using System.Diagnostics;
using System.Linq;

var builder = WebApplication.CreateBuilder(args);
Expand All @@ -25,6 +27,8 @@
args = args.Except(new[] { "/seed" }).ToArray();
}

services.AddOpenTelemetry(configuration.GetSection(nameof(OpenTelemetryOptions)));

var app = builder.Build();

if (seed)
Expand All @@ -34,6 +38,13 @@
return;
}

var activitySource = new ActivitySource("TheIdServer");

app.Use(async (context, next) =>
{
using var activity = activitySource.StartActivity("Request");
await next().ConfigureAwait(false);
});
app.UseTheIdServer(app.Environment, configuration);

app.Run();
11 changes: 11 additions & 0 deletions src/Aguacongas.TheIdServer/Aguacongas.TheIdServer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<PackageReference Include="Azure.Extensions.AspNetCore.DataProtection.Blobs" Version="1.2.1" />
<PackageReference Include="Azure.Extensions.AspNetCore.DataProtection.Keys" Version="1.1.0" />
<PackageReference Include="Azure.Identity" Version="1.5.0" />
<PackageReference Include="Honeycomb.OpenTelemetry" Version="0.20.0-beta" />
<PackageReference Include="IdentityModel.AspNetCore.OAuth2Introspection" Version="6.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.Server" Version="6.0.3" />
<PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="6.0.3" />
Expand All @@ -49,6 +50,16 @@
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.15.0" />
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="6.0.2" PrivateAssets="All" />
<PackageReference Include="OpenTelemetry" Version="1.2.0-rc3" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol.Logs" Version="1.0.0-rc9" />
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="1.1.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc9" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.0.0-rc9" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.0.0-rc9" />
<PackageReference Include="SendGrid" Version="9.27.0" />
<PackageReference Include="Serilog" Version="2.10.0" />
<PackageReference Include="Serilog.AspNetCore" Version="5.0.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Aguacongas.TheIdServer.Options.OpenTelemetry;
using Microsoft.Extensions.Configuration;
using OpenTelemetry.Trace;

namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddOpenTelemetry(this IServiceCollection services, IConfiguration configuration)
{
var options = configuration.Get<OpenTelemetryOptions>();
return services.AddOpenTelemetry(options);
}

public static IServiceCollection AddOpenTelemetry(this IServiceCollection services, OpenTelemetryOptions options)
=> services.AddOpenTelemetryTracing(builder =>
{
builder.AddTheIdServerTelemetry(options);
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
using Aguacongas.TheIdServer.Options.OpenTelemetry;
using Honeycomb.OpenTelemetry;
using OpenTelemetry.Resources;
using StackExchange.Redis;

namespace OpenTelemetry.Trace
{
public static class TracerProviderBuilderExtensions
{
public static TracerProviderBuilder AddTheIdServerTelemetry(this TracerProviderBuilder builder, OpenTelemetryOptions options)
{
if (options.ConsoleEnabled)
{
builder = builder.AddConsoleExporter();
}

return builder.AddExporters(options.Exporter)
.AddSource(options.Service.Name)
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService(options.Service.Name,
options.Service.Namespace,
options.Service.Version,
options.Service.AutoGenerateServiceInstanceId,
options.Service.InstanceId))
.AddInstrumentation(options.Instrumentation);
}

public static TracerProviderBuilder AddInstrumentation(this TracerProviderBuilder builder, InstrumentationOptions options)
{
builder = builder.AddHttpClientInstrumentation(o =>
{
var httpClientOptions = options?.HttpClient;
if (httpClientOptions is null)
{
return;
}
o.SetHttpFlavor = httpClientOptions.SetHttpFlavor;
o.RecordException = httpClientOptions.RecordException;
})
.AddAspNetCoreInstrumentation(o =>
{
var aspOptions = options?.AspNetCore;
if (aspOptions is null)
{
return;
}
o.RecordException = aspOptions.RecordException;
o.EnableGrpcAspNetCoreSupport = aspOptions.EnableGrpcAspNetCoreSupport;
})
.AddSqlClientInstrumentation(o =>
{
var sqlClientOptions = options?.SqlClient;
if (sqlClientOptions is null)
{
return;
}
o.RecordException = sqlClientOptions.RecordException;
o.EnableConnectionLevelAttributes = sqlClientOptions.EnableConnectionLevelAttributes;
o.SetDbStatementForText = sqlClientOptions.SetDbStatementForText;
o.SetDbStatementForStoredProcedure = sqlClientOptions.SetDbStatementForStoredProcedure;
});

if (!string.IsNullOrEmpty(options?.Redis?.ConnectionString))
{
var connection = ConnectionMultiplexer.Connect(options.Redis.ConnectionString);
builder = builder.AddRedisInstrumentation(connection, o =>
{
var redisOptions = options?.Redis;
if (redisOptions is null)
{
return;
}
o.FlushInterval = redisOptions.FlushInterval;
o.SetVerboseDatabaseStatements = redisOptions.SetVerboseDatabaseStatements;
});
}

return builder;
}
private static TracerProviderBuilder AddExporters(this TracerProviderBuilder builder, ExporterOptions options)
{
if (options is null)
{
return builder;
}

if (!string.IsNullOrEmpty(options.Jaeger?.AgentHost))
{
builder = builder.AddJaegerExporter(o =>
{
var jaegerOptions = options.Jaeger;
o.AgentPort = jaegerOptions.AgentPort;
o.AgentHost = jaegerOptions.AgentHost;
o.BatchExportProcessorOptions = jaegerOptions.BatchExportProcessorOptions;
o.ExportProcessorType = jaegerOptions.ExportProcessorType;
});
}

if (options.OpenTelemetryProtocol?.Endpoint is not null)
{
builder = builder.AddOtlpExporter(o =>
{
var otlpOptions = options.OpenTelemetryProtocol;
o.BatchExportProcessorOptions = otlpOptions.BatchExportProcessorOptions;
o.ExportProcessorType = otlpOptions.ExportProcessorType;
o.Endpoint = otlpOptions.Endpoint;
o.Headers = otlpOptions.Headers;
});
}

if (options.Zipkin?.Endpoint is not null)
{
builder = builder.AddZipkinExporter(o =>
{
var zipkinOptions = options.Zipkin;
o.ExportProcessorType = zipkinOptions.ExportProcessorType;
o.BatchExportProcessorOptions = zipkinOptions.BatchExportProcessorOptions;
o.Endpoint = zipkinOptions.Endpoint;
o.MaxPayloadSizeInBytes = zipkinOptions.MaxPayloadSizeInBytes;
o.UseShortTraceIds = zipkinOptions.UseShortTraceIds;
});
}

if (options.Honeycomb?.ApiKey is not null)
{
builder = builder.AddHoneycomb(o =>
{
var honeycombOptions = options.Honeycomb;
o.ApiKey = honeycombOptions.ApiKey;
o.TracesApiKey = honeycombOptions.TracesApiKey;
o.MetricsApiKey = honeycombOptions.MetricsApiKey;
o.Dataset = honeycombOptions.Dataset;
o.TracesDataset = honeycombOptions.TracesDataset;
o.MetricsDataset = honeycombOptions.MetricsDataset;
o.Endpoint = honeycombOptions.Endpoint;
o.TracesEndpoint = honeycombOptions.TracesEndpoint;
o.MetricsEndpoint = honeycombOptions.MetricsEndpoint;
o.SampleRate = honeycombOptions.SampleRate;
o.ServiceName = honeycombOptions.ServiceName;
o.ServiceVersion = honeycombOptions.ServiceVersion;
o.InstrumentHttpClient = honeycombOptions.InstrumentHttpClient;
o.InstrumentSqlClient = honeycombOptions.InstrumentSqlClient;
o.InstrumentGrpcClient = honeycombOptions.InstrumentGrpcClient;
o.InstrumentStackExchangeRedisClient = honeycombOptions.InstrumentStackExchangeRedisClient;
o.MeterNames = honeycombOptions.MeterNames;
});
}

return builder;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Honeycomb.OpenTelemetry;
using OpenTelemetry.Exporter;

namespace Aguacongas.TheIdServer.Options.OpenTelemetry
{
public class ExporterOptions
{
public JaegerExporterOptions Jaeger { get; set; }
public OtlpExporterOptions OpenTelemetryProtocol { get; set; }
public ZipkinExporterOptions Zipkin { get; set; }

public HoneycombOptions Honeycomb { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using OpenTelemetry.Instrumentation.AspNetCore;
using OpenTelemetry.Instrumentation.Http;
using OpenTelemetry.Instrumentation.SqlClient;
using OpenTelemetry.Instrumentation.StackExchangeRedis;

namespace Aguacongas.TheIdServer.Options.OpenTelemetry
{
public class InstrumentationOptions
{
public AspNetCoreInstrumentationOptions AspNetCore{ get; set; }
public HttpClientInstrumentationOptions HttpClient { get; set; }
public SqlClientInstrumentationOptions SqlClient { get; set; }
public RedisOptions Redis { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


namespace Aguacongas.TheIdServer.Options.OpenTelemetry
{
public class OpenTelemetryOptions
{
public ServiceOptions Service { get; set; }
public bool ConsoleEnabled { get; set; }



public InstrumentationOptions Instrumentation { get; set; }

public ExporterOptions Exporter { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using OpenTelemetry.Instrumentation.StackExchangeRedis;

namespace Aguacongas.TheIdServer.Options.OpenTelemetry
{
public class RedisOptions : StackExchangeRedisCallsInstrumentationOptions
{
public string ConnectionString { get; set; }
}
}
11 changes: 11 additions & 0 deletions src/Aguacongas.TheIdServer/Options/OpenTelemetry/ServiceOptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Aguacongas.TheIdServer.Options.OpenTelemetry
{
public class ServiceOptions
{
public string Name { get; set; }
public string Namespace { get; set; }
public string Version { get; set; }
public bool AutoGenerateServiceInstanceId { get; set; } = true;
public string InstanceId { get; set; }
}
}
Loading

0 comments on commit 1888a8a

Please sign in to comment.