Skip to content

Commit 3203e82

Browse files
authored
feat: Add basic config for dotnet plugin. (#167)
## Summary This PR adds a basic config for the dotnet plugin and an initial otel setup. This otel setup does not include being packaged as a plugin or the export sampling configuration. It also includes an ASP.Net application to use for development. Currently this is in the same solution for ease of development. It could move to the e2e projects if needed later. ## How did you test this change? <!-- Frontend - Leave a screencast or a screenshot to visually describe the changes. --> ## Are there any deployment considerations? <!-- Backend - Do we need to consider migrations or backfilling data? -->
1 parent 1beae4d commit 3203e82

File tree

11 files changed

+531
-2
lines changed

11 files changed

+531
-2
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
**/bin
22
**/obj
3+
LaunchDarkly.Observability.sln.DotSettings.user
4+
**/launchSettings.json
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.19"/>
11+
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\src\LaunchDarkly.Observability\LaunchDarkly.Observability.csproj" />
16+
</ItemGroup>
17+
18+
</Project>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
@AspSampleApp_HostAddress = http://localhost:5247
2+
3+
GET {{AspSampleApp_HostAddress}}/weatherforecast/
4+
Accept: application/json
5+
6+
###
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
using LaunchDarkly.Observability;
2+
3+
var builder = WebApplication.CreateBuilder(args);
4+
5+
// Add services to the container.
6+
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
7+
builder.Services.AddEndpointsApiExplorer();
8+
builder.Services.AddSwaggerGen();
9+
builder.Services.AddLaunchDarklyObservability(
10+
Environment.GetEnvironmentVariable("LAUNCHDARKLY_SDK_KEY"),
11+
ldBuilder =>
12+
{
13+
ldBuilder.WithServiceName("ryan-test-service");
14+
}
15+
);
16+
17+
18+
var app = builder.Build();
19+
20+
// Configure the HTTP request pipeline.
21+
if (app.Environment.IsDevelopment())
22+
{
23+
app.UseSwagger();
24+
app.UseSwaggerUI();
25+
}
26+
27+
app.UseHttpsRedirection();
28+
29+
var summaries = new[]
30+
{
31+
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
32+
};
33+
34+
app.MapGet("/weatherforecast", () =>
35+
{
36+
var forecast = Enumerable.Range(1, 5).Select(index =>
37+
new WeatherForecast
38+
(
39+
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
40+
Random.Shared.Next(-20, 55),
41+
summaries[Random.Shared.Next(summaries.Length)]
42+
))
43+
.ToArray();
44+
return forecast;
45+
})
46+
.WithName("GetWeatherForecast")
47+
.WithOpenApi();
48+
49+
app.Run();
50+
51+
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
52+
{
53+
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
54+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

sdk/@launchdarkly/observability-dotnet/LaunchDarkly.Observability.sln

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{25034066-0
1212
README.md = README.md
1313
EndProjectSection
1414
EndProject
15+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspSampleApp", "AspSampleApp\AspSampleApp.csproj", "{84C42307-BD0F-4558-84C5-6E9B06932D8E}"
16+
EndProject
17+
1518
Global
1619
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1720
Debug|Any CPU = Debug|Any CPU
@@ -29,5 +32,9 @@ Global
2932
{85022F8E-D264-44F4-A8AF-1117D8202FBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
3033
{85022F8E-D264-44F4-A8AF-1117D8202FBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
3134
{85022F8E-D264-44F4-A8AF-1117D8202FBD}.Release|Any CPU.Build.0 = Release|Any CPU
35+
{84C42307-BD0F-4558-84C5-6E9B06932D8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
36+
{84C42307-BD0F-4558-84C5-6E9B06932D8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
37+
{84C42307-BD0F-4558-84C5-6E9B06932D8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
38+
{84C42307-BD0F-4558-84C5-6E9B06932D8E}.Release|Any CPU.Build.0 = Release|Any CPU
3239
EndGlobalSection
3340
EndGlobal

sdk/@launchdarkly/observability-dotnet/src/LaunchDarkly.Observability/LaunchDarkly.Observability.csproj

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
2-
1+
<Project Sdk="Microsoft.NET.Sdk">
32
<PropertyGroup>
43
<!--x-release-please-start-version-->
54
<Version>0.0.0</Version>
@@ -35,4 +34,31 @@
3534
<AssemblyOriginatorKeyFile>../../../../../LaunchDarkly.snk</AssemblyOriginatorKeyFile>
3635
<SignAssembly>true</SignAssembly>
3736
</PropertyGroup>
37+
38+
<ItemGroup>
39+
<!-- Open Telemetry Dependencies -->
40+
<PackageReference Include="OpenTelemetry" Version="1.12.0" />
41+
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.12.0" />
42+
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
43+
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
44+
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0" />
45+
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.12.0-beta.1" />
46+
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0" />
47+
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="1.12.0-beta.1" />
48+
<PackageReference Include="OpenTelemetry.Instrumentation.Quartz" Version="1.12.0-beta.1" />
49+
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.12.0" />
50+
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.12.0-beta.2" />
51+
<PackageReference Include="OpenTelemetry.Instrumentation.Wcf" Version="1.12.0-beta.1" />
52+
</ItemGroup>
53+
54+
<ItemGroup>
55+
<!-- LaunchDarkly Dependencies -->
56+
<PackageReference Include="LaunchDarkly.ServerSdk" Version="[8.10.0,9.0.0)" />
57+
<PackageReference Include="LaunchDarkly.ServerSdk.Telemetry" Version="[1.2.0,2.0.0)" />
58+
</ItemGroup>
59+
60+
<ItemGroup>
61+
<!-- Temporary dependency until: https://github.com/open-telemetry/opentelemetry-dotnet/issues/5973 is resolved. -->
62+
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="9.0.0" />
63+
</ItemGroup>
3864
</Project>
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
using System;
2+
3+
namespace LaunchDarkly.Observability
4+
{
5+
public struct ObservabilityConfig
6+
{
7+
/// <summary>
8+
/// The configured OTLP endpoint.
9+
/// </summary>
10+
public string OtlpEndpoint { get; }
11+
12+
/// <summary>
13+
/// The configured back-end URL.
14+
/// <para>
15+
/// This is used for non-telemetry operations such as accessing the sampling configuration.
16+
/// </para>
17+
/// </summary>
18+
public string BackendUrl { get; }
19+
/// <summary>
20+
/// The name of the service.
21+
/// <para>
22+
/// The service name is used for adding resource attributes. If a service name is not defined, then the
23+
/// service version will also not be included in the resource attributes.
24+
/// </para>
25+
/// </summary>
26+
public string ServiceName { get; }
27+
/// <summary>
28+
/// The version of the service.
29+
/// </summary>
30+
public string ServiceVersion { get; }
31+
/// <summary>
32+
/// The environment for the service.
33+
/// </summary>
34+
public string Environment { get; }
35+
/// <summary>
36+
/// The LaunchDarkly SDK key.
37+
/// </summary>
38+
public string SdkKey { get; }
39+
40+
private ObservabilityConfig(
41+
string otlpEndpoint,
42+
string backendUrl,
43+
string serviceName,
44+
string environment,
45+
string serviceVersion,
46+
string sdkKey)
47+
{
48+
OtlpEndpoint = otlpEndpoint;
49+
BackendUrl = backendUrl;
50+
ServiceName = serviceName;
51+
Environment = environment;
52+
ServiceVersion = serviceVersion;
53+
SdkKey = sdkKey;
54+
}
55+
56+
/// <summary>
57+
/// Create a new builder for <see cref="ObservabilityConfig"/>.
58+
/// </summary>
59+
/// <param name="sdkKey">The LaunchDarkly SDK key used for authentication and resource attributes.</param>
60+
/// <returns>A new <see cref="Builder"/> instance for configuring observability.</returns>
61+
internal static Builder CreateBuilder(string sdkKey) => new Builder(sdkKey);
62+
63+
/// <summary>
64+
/// Fluent builder for <see cref="ObservabilityConfig"/>.
65+
/// </summary>
66+
public sealed class Builder
67+
{
68+
private const string DefaultOtlpEndpoint = "https://otel.observability.app.launchdarkly.com:4318";
69+
private const string DefaultBackendUrl = "https://pub.observability.app.launchdarkly.com";
70+
private string _otlpEndpoint = DefaultOtlpEndpoint;
71+
private string _backendUrl = DefaultBackendUrl;
72+
private string _serviceName = string.Empty;
73+
private string _environment = string.Empty;
74+
private string _serviceVersion = string.Empty;
75+
private readonly string _sdkKey;
76+
77+
internal Builder(string sdkKey)
78+
{
79+
_sdkKey = sdkKey ?? throw new ArgumentNullException(nameof(sdkKey), "SDK key cannot be null when creating an ObservabilityConfig builder.");
80+
}
81+
82+
/// <summary>
83+
/// Set the OTLP endpoint.
84+
/// <para>
85+
/// For most configurations, the OTLP endpoint will not need to be set.
86+
/// </para>
87+
/// <para>
88+
/// Setting the endpoint to null will reset the builder value to the default.
89+
/// </para>
90+
/// </summary>
91+
/// <param name="otlpEndpoint">The OTLP exporter endpoint URL.</param>
92+
/// <returns>A reference to this builder.</returns>
93+
public Builder WithOtlpEndpoint(string otlpEndpoint)
94+
{
95+
_otlpEndpoint = otlpEndpoint ?? DefaultOtlpEndpoint;
96+
return this;
97+
}
98+
99+
/// <summary>
100+
/// Set the back-end URL for non-telemetry operations.
101+
/// <para>
102+
/// For most configurations, the backend url will not need to be set.
103+
/// </para>
104+
/// <para>
105+
/// Setting the url to null will reset the builder value to the default.
106+
/// </para>
107+
/// </summary>
108+
/// <param name="backendUrl">The back-end URL used for non-telemetry operations.</param>
109+
/// <returns>A reference to this builder.</returns>
110+
public Builder WithBackendUrl(string backendUrl)
111+
{
112+
_backendUrl = backendUrl ?? DefaultBackendUrl;
113+
return this;
114+
}
115+
116+
/// <summary>
117+
/// Set the service name.
118+
/// </summary>
119+
/// <param name="serviceName">The logical service name used in telemetry resource attributes.</param>
120+
/// <returns>A reference to this builder.</returns>
121+
public Builder WithServiceName(string serviceName)
122+
{
123+
_serviceName = serviceName ?? string.Empty;
124+
return this;
125+
}
126+
127+
/// <summary>
128+
/// Set the service version.
129+
/// </summary>
130+
/// <param name="serviceVersion">
131+
/// The version of the service that will be added to resource attributes when a service name is provided.
132+
/// </param>
133+
/// <returns>A reference to this builder.</returns>
134+
public Builder WithServiceVersion(string serviceVersion)
135+
{
136+
_serviceVersion = serviceVersion ?? string.Empty;
137+
return this;
138+
}
139+
140+
/// <summary>
141+
/// Set the environment name.
142+
/// </summary>
143+
/// <param name="environment">The environment name (for example, "prod" or "staging").</param>
144+
/// <returns>A reference to this builder.</returns>
145+
public Builder WithEnvironment(string environment)
146+
{
147+
_environment = environment ?? string.Empty;
148+
return this;
149+
}
150+
151+
/// <summary>
152+
/// Build an immutable <see cref="ObservabilityConfig"/> instance.
153+
/// </summary>
154+
/// <returns>The constructed <see cref="ObservabilityConfig"/>.</returns>
155+
public ObservabilityConfig Build()
156+
{
157+
return new ObservabilityConfig(
158+
_otlpEndpoint,
159+
_backendUrl,
160+
_serviceName,
161+
_environment,
162+
_serviceVersion,
163+
_sdkKey);
164+
}
165+
}
166+
}
167+
}

0 commit comments

Comments
 (0)