Skip to content

Commit bf03d1e

Browse files
authored
Use config bindings (#5)
* Use new/existing configuration bindings * Use IOptions for settings Enable scopes in console log
1 parent 23177cb commit bf03d1e

17 files changed

+244
-193
lines changed

scenario1.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,18 @@ docker run -d --name jaeger \
9696
jaegertracing/all-in-one
9797
```
9898

99-
- When using Application Insights (set USE_APPLICATIONINSIGHTS=1 and USE_OPENTELEMETRY=0), ensure the instrumentation key is set (APPINSIGHTS_INSTRUMENTATIONKEY). The sample project has a simple way to provide settings to all applications. In folder ./shared create a file called `appsettings.Development.json`.
99+
- When using Application Insights, ensure the instrumentation key is set (a simpler way to provide settings to all applications is to create file appsettings.Development.json in folder ./shared):
100+
101+
```json
102+
{
103+
"SampleApp": {
104+
"UseApplicationInsights": "true",
105+
"UseOpenTelemetry": "false",
106+
"ApplicationInsightsInstrumentationKey": "<Instrumentation key>"
107+
}
108+
}
109+
```
110+
100111

101112
To run the sample start the project Sample.TimeApi. To generate load use the following script:
102113

scenario2.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,17 @@ docker run -d --name jaeger \
9393
jaegertracing/all-in-one
9494
```
9595

96-
- When using Application Insights (set USE_APPLICATIONINSIGHTS=1 and USE_OPENTELEMETRY=0), ensure the instrumentation key is set (APPINSIGHTS_INSTRUMENTATIONKEY). The sample project has a simple way to provide settings to all applications. In folder ./shared create a file called `appsettings.Development.json`.
96+
- When using Application Insights, ensure the instrumentation key is set (a simpler way to provide settings to all applications is to create file appsettings.Development.json in folder ./shared):
97+
98+
```json
99+
{
100+
"SampleApp": {
101+
"UseApplicationInsights": "true",
102+
"UseOpenTelemetry": "false",
103+
"ApplicationInsightsInstrumentationKey": "<Instrumentation key>"
104+
}
105+
}
106+
```
97107

98108
To run the sample start the projects Sample.TimeApi and Sample.MainApi. To generate load use the following script:
99109

scenario3.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,17 @@ docker-compose up
230230

231231
To visualize it, open Grafana on your browser at [http://localhost:3000](http://localhost:3000) (credentials are admin/password1). Add Prometheus as data source (URL is http://prometheus:9090).
232232

233+
- When using Application Insights, ensure the instrumentation key is set (a simpler way to provide settings to all applications is to create file appsettings.Development.json in folder ./shared):
233234

234-
- When using Application Insights (set USE_APPLICATIONINSIGHTS=1 and USE_OPENTELEMETRY=0), ensure the instrumentation key is set (APPINSIGHTS_INSTRUMENTATIONKEY). The sample project has a simple way to provide settings to all applications. In folder ./shared create a file called `appsettings.Development.json`.
235+
```json
236+
{
237+
"SampleApp": {
238+
"UseApplicationInsights": "true",
239+
"UseOpenTelemetry": "false",
240+
"ApplicationInsightsInstrumentationKey": "<Instrumentation key>"
241+
}
242+
}
243+
```
235244

236245
To run the sample start the projects Sample.TimeApi, Sample.MainApi and Sample.RabbitMQProcessor. To generate load use the following scripts:
237246

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,24 @@
11
using System;
22
using Microsoft.Extensions.Configuration;
3-
3+
using Microsoft.Extensions.DependencyInjection;
4+
45
namespace Sample.Common
56
{
6-
public static class ConfigurationExtensions
7-
{
8-
const string RabbitMQHostNameConfigKey = "RabbitMQHostName";
9-
10-
public static string GetRabbitMQHostName(this IConfiguration configuration)
11-
{
12-
var rabbitMQHostName = configuration[RabbitMQHostNameConfigKey];
13-
if (string.IsNullOrWhiteSpace(rabbitMQHostName))
14-
rabbitMQHostName = "localhost";
15-
16-
return rabbitMQHostName;
17-
}
18-
19-
20-
public static string GetZipkinUrl(this IConfiguration configuration)
21-
{
22-
return configuration["ZIPKIN_URL"];
23-
}
24-
25-
public static string GetJaegerHost(this IConfiguration configuration)
26-
{
27-
return configuration["JAEGER_HOST"];
28-
}
297

30-
public static bool UseOpenTelemetry(this IConfiguration configuration)
31-
{
32-
return configuration["USE_OPENTELEMETRY"] == "1";
33-
}
34-
35-
public static string GetApplicationInsightsInstrumentationKeyForOpenTelemetry(this IConfiguration configuration)
36-
{
37-
return configuration["OT_APPINSIGHTS_INSTRUMENTATIONKEY"];
38-
}
39-
40-
public static string GetApplicationInsightsInstrumentationKey(this IConfiguration configuration)
41-
{
42-
return configuration["APPINSIGHTS_INSTRUMENTATIONKEY"];
43-
}
44-
45-
public static bool UseApplicationInsights(this IConfiguration configuration)
46-
{
47-
return configuration["USE_APPLICATIONINSIGHTS"] == "1";
48-
}
49-
50-
public static string GetPrometheusExportURL(this IConfiguration configuration)
51-
{
52-
return configuration["PROMETHEUS_EXPORT_URL"];
53-
}
8+
public static class ConfigurationExtensions
9+
{
10+
const string SampleAppOptionsConfigSection = "SampleApp";
11+
12+
public static IServiceCollection AddSampleAppOptions(this IServiceCollection services, IConfiguration configuration)
13+
{
14+
return services.Configure<SampleAppOptions>(configuration.GetSection(SampleAppOptionsConfigSection));
15+
}
16+
17+
public static SampleAppOptions GetSampleAppOptions(this IConfiguration configuration)
18+
{
19+
var telemetryOptions = new SampleAppOptions();
20+
configuration.GetSection(SampleAppOptionsConfigSection).Bind(telemetryOptions);
21+
return telemetryOptions;
22+
}
5423
}
5524
}
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.1</TargetFramework>
@@ -8,21 +8,22 @@
88

99
<ItemGroup>
1010
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.0" />
11+
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="3.1.0" />
1112
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
1213
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.12.0" />
1314
<PackageReference Include="Microsoft.ApplicationInsights" Version="2.12.0" />
1415
<PackageReference Include="Microsoft.ApplicationInsights.WorkerService" Version="2.12.0" />
15-
<PackageReference Include="OpenTelemetry.Exporter.Prometheus" Version="0.2.0-alpha.149" />
16+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.0" />
17+
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="3.1.0" />
1618
<PackageReference Include="System.Text.Json" Version="4.7.0" />
17-
</ItemGroup>
1819

19-
<ItemGroup Condition="'a' == 'a'">
20-
<PackageReference Include="OpenTelemetry" Version="0.2.0-alpha.149" />
21-
<PackageReference Include="OpenTelemetry.Collector.Dependencies" Version="0.2.0-alpha.149" />
22-
<PackageReference Include="OpenTelemetry.Exporter.ApplicationInsights" Version="0.2.0-alpha.149" />
23-
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="0.2.0-alpha.149" />
24-
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="0.2.0-alpha.149" />
25-
<PackageReference Include="OpenTelemetry.Collector.AspNetCore" Version="0.2.0-alpha.149" />
26-
<PackageReference Include="OpenTelemetry.Hosting" Version="0.2.0-alpha.149" />
20+
<PackageReference Include="OpenTelemetry" Version="0.2.0-alpha.151" />
21+
<PackageReference Include="OpenTelemetry.Exporter.Prometheus" Version="0.2.0-alpha.151" />
22+
<PackageReference Include="OpenTelemetry.Collector.Dependencies" Version="0.2.0-alpha.151" />
23+
<PackageReference Include="OpenTelemetry.Exporter.ApplicationInsights" Version="0.2.0-alpha.151" />
24+
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="0.2.0-alpha.151" />
25+
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" Version="0.2.0-alpha.151" />
26+
<PackageReference Include="OpenTelemetry.Collector.AspNetCore" Version="0.2.0-alpha.151" />
27+
<PackageReference Include="OpenTelemetry.Hosting" Version="0.2.0-alpha.151" />
2728
</ItemGroup>
2829
</Project>

src/Sample.Common/SampleAppOptions.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace Sample.Common
2+
{
3+
public class SampleAppOptions
4+
{
5+
public string RabbitMQHostName { get; set; } = "localhost";
6+
public string TimeAPIUrl { get; set; } = "http://localhost:5002";
7+
public bool UseOpenTelemetry { get; set; }
8+
public bool UseApplicationInsights { get; set; }
9+
10+
public string ApplicationInsightsInstrumentationKey { get; set; }
11+
public string ApplicationInsightsForOpenTelemetryInstrumentationKey { get; set; }
12+
}
13+
}

0 commit comments

Comments
 (0)