This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(enrolling): add open telemetry integration
OpenTelemetry beta is released. As an early adopter, I am integrating it with enrolling service first. I am also creating the first meta package of eSchool project for the integration. This package is consumed by using project reference rather NuGet package as this is a monorepo. This meta package has some extension methods which I will move to separate project once consolidated.
- Loading branch information
1 parent
a03d284
commit eb751de
Showing
18 changed files
with
259 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
kubectl create ns eschool | ||
kubectl label namespace eschool istio-injection=enabled | ||
kubectl -n eschool apply -f .\eschool-gateway.yaml | ||
helm -n eschool install mssql .\mssql\ | ||
helm -n eschool install enrolling .\enrolling\ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,6 @@ services: | |
build: | ||
context: . | ||
dockerfile: src/Web/WebStatus/Dockerfile | ||
|
||
jaeger: | ||
image: jaegertracing/all-in-one |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
using System; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenTelemetry.Context.Propagation; | ||
using OpenTelemetry.Trace; | ||
using OpenTelemetry.Trace.Samplers; | ||
|
||
namespace OpenCodeFoundation.OpenTelemetry | ||
{ | ||
public static class Extensions | ||
{ | ||
private const string SectionName = "OpenTelemetry"; | ||
|
||
public static IServiceCollection AddOpenTelemetryIntegration( | ||
this IServiceCollection services, | ||
Action<OpenTelemetryOptions> options = null, | ||
string sectionName = SectionName) | ||
{ | ||
var openTelemetryOptions = services.GetOptions(sectionName, options); | ||
|
||
if (openTelemetryOptions.Enabled) | ||
{ | ||
ConfigureOpenTelemetry(services, openTelemetryOptions); | ||
} | ||
|
||
return services; | ||
} | ||
|
||
public static T GetOptions<T>( | ||
this IServiceCollection services, | ||
string sectionName, | ||
Action<T> configure = null) | ||
where T : IConfigurationOptions, new() | ||
{ | ||
var provider = services.BuildServiceProvider(); | ||
var configuration = provider.GetRequiredService<IConfiguration>(); | ||
|
||
var options = new T(); | ||
configure?.Invoke(options); | ||
|
||
configuration.GetSection(sectionName).Bind(options); | ||
|
||
options.Validate(); | ||
return options; | ||
} | ||
|
||
private static void ConfigureOpenTelemetry(IServiceCollection services, OpenTelemetryOptions openTelemetryOptions) | ||
{ | ||
services.AddOpenTelemetry(configure => | ||
{ | ||
ConfigureSampler(openTelemetryOptions, configure); | ||
ConfigureInstrumentations(openTelemetryOptions, configure); | ||
ConfigureExporters(openTelemetryOptions, configure); | ||
}); | ||
} | ||
|
||
private static void ConfigureSampler(OpenTelemetryOptions openTelemetryOptions, TracerProviderBuilder configure) | ||
{ | ||
if (openTelemetryOptions.AlwaysOnSampler) | ||
{ | ||
configure.SetSampler(new AlwaysOnSampler()); | ||
} | ||
} | ||
|
||
private static void ConfigureExporters(OpenTelemetryOptions openTelemetryOptions, TracerProviderBuilder configure) | ||
{ | ||
if (openTelemetryOptions.Jaeger.Enabled) | ||
{ | ||
configure.UseJaegerExporter(config => | ||
{ | ||
config.ServiceName = openTelemetryOptions.Jaeger.ServiceName; | ||
config.AgentHost = openTelemetryOptions.Jaeger.Host; | ||
config.AgentPort = openTelemetryOptions.Jaeger.Port; | ||
}); | ||
} | ||
} | ||
|
||
private static void ConfigureInstrumentations(OpenTelemetryOptions openTelemetryOptions, TracerProviderBuilder configure) | ||
{ | ||
configure.AddAspNetCoreInstrumentation(config => | ||
{ | ||
config.TextFormat = GetTextFormat(openTelemetryOptions); | ||
}); | ||
|
||
configure.AddHttpClientInstrumentation(config => | ||
{ | ||
config.TextFormat = GetTextFormat(openTelemetryOptions); | ||
}); | ||
|
||
configure.AddSqlClientDependencyInstrumentation(); | ||
} | ||
|
||
private static ITextFormat GetTextFormat(OpenTelemetryOptions openTelemetryOptions) | ||
=> openTelemetryOptions.Istio | ||
? new B3Format() | ||
: (ITextFormat)new TraceContextFormat(); | ||
} | ||
} |
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,7 @@ | ||
namespace OpenCodeFoundation.OpenTelemetry | ||
{ | ||
public interface IConfigurationOptions | ||
{ | ||
public void Validate(); | ||
} | ||
} |
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,34 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
<AssemblyName>OpenCodeFoundation.OpenTelemetry</AssemblyName> | ||
<RootNamespace>OpenCodeFoundation.OpenTelemetry</RootNamespace> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
|
||
<CodeAnalysisRuleSet>..\..\..\eSchool.ruleset</CodeAnalysisRuleSet> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<!-- Asp.net required packages --> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0-preview.7.20364.11" /> | ||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="5.0.0-preview.7.20364.11" /> | ||
|
||
<!-- OpenTelemetry --> | ||
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="0.4.0-beta.2" /> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="0.4.0-beta.2" /> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="0.4.0-beta.2" /> | ||
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="0.4.0-beta.2" /> | ||
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="0.4.0-beta.2" /> | ||
<!-- <PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="0.4.0-beta.2" /> --> | ||
|
||
<!-- Analyzers --> | ||
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118" PrivateAssets="All" /> | ||
|
||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<AdditionalFiles Include="..\..\stylecop.json" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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,48 @@ | ||
using System; | ||
|
||
namespace OpenCodeFoundation.OpenTelemetry | ||
{ | ||
public class OpenTelemetryOptions | ||
: IConfigurationOptions | ||
{ | ||
public bool Enabled { get; set; } = false; | ||
|
||
public bool AlwaysOnSampler { get; set; } = true; | ||
|
||
public bool Istio { get; set; } = false; | ||
|
||
public JaegerOptions Jaeger { get; set; } = new JaegerOptions(); | ||
|
||
public void Validate() | ||
{ | ||
if (Jaeger.Enabled) | ||
{ | ||
ValidateJaeger(); | ||
} | ||
} | ||
|
||
private void ValidateJaeger() | ||
{ | ||
if (string.IsNullOrWhiteSpace(Jaeger.ServiceName)) | ||
{ | ||
throw new ArgumentException("Jaeger service name can not be null if Jaeger is enabled"); | ||
} | ||
|
||
if (string.IsNullOrWhiteSpace(Jaeger.Host)) | ||
{ | ||
throw new ArgumentException("Jaeger Host can not be null if Jaeger is enabled"); | ||
} | ||
} | ||
} | ||
|
||
public class JaegerOptions | ||
{ | ||
public bool Enabled { get; set; } = false; | ||
|
||
public string ServiceName { get; set; } | ||
|
||
public string Host { get; set; } | ||
|
||
public int Port { get; set; } | ||
} | ||
} |
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
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