Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sdk/@launchdarkly/observability-dotnet/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
**/bin
**/obj
LaunchDarkly.Observability.sln.DotSettings.user
**/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.19"/>
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\LaunchDarkly.Observability\LaunchDarkly.Observability.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@AspSampleApp_HostAddress = http://localhost:5247

GET {{AspSampleApp_HostAddress}}/weatherforecast/
Accept: application/json

###
54 changes: 54 additions & 0 deletions sdk/@launchdarkly/observability-dotnet/AspSampleApp/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using LaunchDarkly.Observability;

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddLaunchDarklyObservability(
Environment.GetEnvironmentVariable("LAUNCHDARKLY_SDK_KEY"),
ldBuilder =>
{
ldBuilder.WithServiceName("ryan-test-service");
}
);


var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}

app.UseHttpsRedirection();

var summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};

app.MapGet("/weatherforecast", () =>
{
var forecast = Enumerable.Range(1, 5).Select(index =>
new WeatherForecast
(
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
Random.Shared.Next(-20, 55),
summaries[Random.Shared.Next(summaries.Length)]
))
.ToArray();
return forecast;
})
.WithName("GetWeatherForecast")
.WithOpenApi();

app.Run();

record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
{
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Docs", "Docs", "{25034066-0
README.md = README.md
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspSampleApp", "AspSampleApp\AspSampleApp.csproj", "{84C42307-BD0F-4558-84C5-6E9B06932D8E}"
EndProject

Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -29,5 +32,9 @@ Global
{85022F8E-D264-44F4-A8AF-1117D8202FBD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85022F8E-D264-44F4-A8AF-1117D8202FBD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85022F8E-D264-44F4-A8AF-1117D8202FBD}.Release|Any CPU.Build.0 = Release|Any CPU
{84C42307-BD0F-4558-84C5-6E9B06932D8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{84C42307-BD0F-4558-84C5-6E9B06932D8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{84C42307-BD0F-4558-84C5-6E9B06932D8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{84C42307-BD0F-4558-84C5-6E9B06932D8E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<!--x-release-please-start-version-->
<Version>0.0.0</Version>
Expand Down Expand Up @@ -35,4 +34,31 @@
<AssemblyOriginatorKeyFile>../../../../../LaunchDarkly.snk</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
</PropertyGroup>

<ItemGroup>
<!-- Open Telemetry Dependencies -->
<PackageReference Include="OpenTelemetry" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.12.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Process" Version="1.12.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Quartz" Version="1.12.0-beta.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.12.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.12.0-beta.2" />
<PackageReference Include="OpenTelemetry.Instrumentation.Wcf" Version="1.12.0-beta.1" />
</ItemGroup>

<ItemGroup>
<!-- LaunchDarkly Dependencies -->
<PackageReference Include="LaunchDarkly.ServerSdk" Version="[8.10.0,9.0.0)" />
<PackageReference Include="LaunchDarkly.ServerSdk.Telemetry" Version="[1.2.0,2.0.0)" />
</ItemGroup>

<ItemGroup>
<!-- Temporary dependency until: https://github.com/open-telemetry/opentelemetry-dotnet/issues/5973 is resolved. -->
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="9.0.0" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
namespace LaunchDarkly.Observability
{
public struct ObservabilityConfig
{
/// <summary>
/// The configured OTLP endpoint.
/// </summary>
public string OtlpEndpoint { get; }

/// <summary>
/// The configured back-end URL.
/// <para>
/// This is used for non-telemetry operations such as accessing the sampling configuration.
/// </para>
/// </summary>
public string BackendUrl { get; }
/// <summary>
/// The name of the service.
/// <para>
/// The service name is used for adding resource attributes. If a service name is not defined, then the
/// service version will also not be included in the resource attributes.
/// </para>
/// </summary>
public string ServiceName { get; }
/// <summary>
/// The version of the service.
/// </summary>
public string ServiceVersion { get; }
/// <summary>
/// The environment for the service.
/// </summary>
public string Environment { get; }
/// <summary>
/// The LaunchDarkly SDK key.
/// </summary>
public string SdkKey { get; }

private ObservabilityConfig(
string otlpEndpoint,
string backendUrl,
string serviceName,
string environment,
string serviceVersion,
string sdkKey)
{
OtlpEndpoint = otlpEndpoint;
BackendUrl = backendUrl;
ServiceName = serviceName;
Environment = environment;
ServiceVersion = serviceVersion;
SdkKey = sdkKey;
}

/// <summary>
/// Create a new builder for <see cref="ObservabilityConfig"/>.
/// </summary>
/// <param name="sdkKey">The LaunchDarkly SDK key used for authentication and resource attributes.</param>
/// <returns>A new <see cref="Builder"/> instance for configuring observability.</returns>
internal static Builder CreateBuilder(string sdkKey) => new Builder(sdkKey);

/// <summary>
/// Fluent builder for <see cref="ObservabilityConfig"/>.
/// </summary>
public sealed class Builder
{
private const string DefaultOtlpEndpoint = "https://otel.observability.app.launchdarkly.com:4318";
private const string DefaultBackendUrl = "https://pub.observability.app.launchdarkly.com";
private string _otlpEndpoint = DefaultOtlpEndpoint;
private string _backendUrl = DefaultBackendUrl;
private string _serviceName = string.Empty;
private string _environment = string.Empty;
private string _serviceVersion = string.Empty;
private readonly string _sdkKey;

internal Builder(string sdkKey)
{
this._sdkKey = sdkKey;
}

/// <summary>
/// Set the OTLP endpoint.
/// <para>
/// For most configurations, the OTLP endpoint will not need to be set.
/// </para>
/// <para>
/// Setting the endpoint to null will reset the builder value to the default.
/// </para>
/// </summary>
/// <param name="otlpEndpoint">The OTLP exporter endpoint URL.</param>
/// <returns>A reference to this builder.</returns>
public Builder WithOtlpEndpoint(string otlpEndpoint)
{
_otlpEndpoint = otlpEndpoint ?? DefaultOtlpEndpoint;
return this;
}

/// <summary>
/// Set the back-end URL for non-telemetry operations.
/// <para>
/// For most configurations, the backend url will not need to be set.
/// </para>
/// <para>
/// Setting the url to null will reset the builder value to the default.
/// </para>
/// </summary>
/// <param name="backendUrl">The back-end URL used for non-telemetry operations.</param>
/// <returns>A reference to this builder.</returns>
public Builder WithBackendUrl(string backendUrl)
{
_backendUrl = backendUrl ?? DefaultBackendUrl;
return this;
}

/// <summary>
/// Set the service name.
/// </summary>
/// <param name="serviceName">The logical service name used in telemetry resource attributes.</param>
/// <returns>A reference to this builder.</returns>
public Builder WithServiceName(string serviceName)
{
_serviceName = serviceName ?? string.Empty;
return this;
}

/// <summary>
/// Set the service version.
/// </summary>
/// <param name="serviceVersion">
/// The version of the service that will be added to resource attributes when a service name is provided.
/// </param>
/// <returns>A reference to this builder.</returns>
public Builder WithServiceVersion(string serviceVersion)
{
_serviceVersion = serviceVersion ?? string.Empty;
return this;
}

/// <summary>
/// Set the environment name.
/// </summary>
/// <param name="environment">The environment name (for example, "prod" or "staging").</param>
/// <returns>A reference to this builder.</returns>
public Builder WithEnvironment(string environment)
{
_environment = environment ?? string.Empty;
return this;
}

/// <summary>
/// Build an immutable <see cref="ObservabilityConfig"/> instance.
/// </summary>
/// <returns>The constructed <see cref="ObservabilityConfig"/>.</returns>
public ObservabilityConfig Build()
{
return new ObservabilityConfig(
_otlpEndpoint,
_backendUrl,
_serviceName,
_environment,
_serviceVersion,
_sdkKey);
}
}
}
}
Loading
Loading