Skip to content

Commit

Permalink
Don't bundle instrumentation packages in distro (#250)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeGoldsmith authored Sep 20, 2022
1 parent 40ad7a8 commit a954d12
Show file tree
Hide file tree
Showing 14 changed files with 215 additions and 288 deletions.
37 changes: 29 additions & 8 deletions examples/aspnetcore-redis/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Trace;
using Honeycomb.OpenTelemetry;
using StackExchange.Redis;

Expand All @@ -29,16 +31,35 @@ public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();

// configure OpenTelemetry SDK to send data to Honeycomb
services.AddHoneycomb(Configuration);

ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(
new ConfigurationOptions
{
EndPoints = { "localhost:6379" },
AbortOnConnectFail = false, // allow for reconnects if redis is not available
});
new ConfigurationOptions
{
EndPoints = { "localhost:6379" },
AbortOnConnectFail = false, // allow for reconnects if redis is not available
}
);
services.AddSingleton<IConnectionMultiplexer>(redis);

// configure OpenTelemetry SDK to send data to Honeycomb
services.AddOpenTelemetryTracing(builder => builder
.AddHoneycomb(Configuration)
.AddAspNetCoreInstrumentation(opts =>
{
opts.RecordException = true;
opts.Enrich = (activity, eventName, _) =>
{
if (eventName == "OnStartActivity")
{
foreach (var entry in Baggage.Current)
{
activity.SetTag(entry.Key, entry.Value);
}
}
};
}
)
.AddRedisInstrumentation(redis)
);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Expand Down
3 changes: 1 addition & 2 deletions examples/aspnetcore-redis/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"AllowedHosts": "*",
"Honeycomb": {
"ServiceName": "my-web-app-with-redis",
"ApiKey": "{apikey}",
"InstrumentStackExchangeRedisClient": "true"
"ApiKey": "{apikey}"
}
}
6 changes: 6 additions & 0 deletions examples/aspnetcore-redis/aspnetcoreredis.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@
<ProjectReference Include="..\..\src\Honeycomb.OpenTelemetry\Honeycomb.OpenTelemetry.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc9.6" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.6" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.0.0-rc9.7" />
</ItemGroup>

</Project>
21 changes: 20 additions & 1 deletion examples/aspnetcore/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
using Microsoft.Extensions.Logging;
using Honeycomb.OpenTelemetry;
using System.Diagnostics.Metrics;
using OpenTelemetry;
using OpenTelemetry.Trace;

namespace aspnetcore
{
Expand All @@ -30,7 +32,24 @@ public void ConfigureServices(IServiceCollection services)
services.AddControllers();

// configure OpenTelemetry SDK to send data to Honeycomb
services.AddHoneycomb(Configuration);
services.AddOpenTelemetryTracing(builder => builder
.AddHoneycomb(Configuration)
.AddAspNetCoreInstrumentation(opts =>
{
opts.RecordException = true;
opts.Enrich = (activity, eventName, _) =>
{
if (eventName == "OnStartActivity")
{
foreach (var entry in Baggage.Current)
{
activity.SetTag(entry.Key, entry.Value);
}
}
};
}
)
);

// (optional metrics setup)
// meter name used here must be configured in the OpenTelemetry SDK
Expand Down
5 changes: 5 additions & 0 deletions examples/aspnetcore/aspnetcore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@
<ProjectReference Include="..\..\src\Honeycomb.OpenTelemetry\Honeycomb.OpenTelemetry.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc9.6" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.6" />
</ItemGroup>

</Project>
15 changes: 15 additions & 0 deletions honeycomb-opentelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "console", "examples\console
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aspnetcore", "examples\aspnetcore\aspnetcore.csproj", "{37D377D0-7C5A-4F44-B535-C9DA60FDB723}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aspnetcoreredis", "examples\aspnetcore-redis\aspnetcoreredis.csproj", "{41B05546-CC58-4546-AC73-2497F7CD8FD8}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -78,11 +80,24 @@ Global
{37D377D0-7C5A-4F44-B535-C9DA60FDB723}.Release|x64.Build.0 = Release|Any CPU
{37D377D0-7C5A-4F44-B535-C9DA60FDB723}.Release|x86.ActiveCfg = Release|Any CPU
{37D377D0-7C5A-4F44-B535-C9DA60FDB723}.Release|x86.Build.0 = Release|Any CPU
{41B05546-CC58-4546-AC73-2497F7CD8FD8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{41B05546-CC58-4546-AC73-2497F7CD8FD8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{41B05546-CC58-4546-AC73-2497F7CD8FD8}.Debug|x64.ActiveCfg = Debug|Any CPU
{41B05546-CC58-4546-AC73-2497F7CD8FD8}.Debug|x64.Build.0 = Debug|Any CPU
{41B05546-CC58-4546-AC73-2497F7CD8FD8}.Debug|x86.ActiveCfg = Debug|Any CPU
{41B05546-CC58-4546-AC73-2497F7CD8FD8}.Debug|x86.Build.0 = Debug|Any CPU
{41B05546-CC58-4546-AC73-2497F7CD8FD8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{41B05546-CC58-4546-AC73-2497F7CD8FD8}.Release|Any CPU.Build.0 = Release|Any CPU
{41B05546-CC58-4546-AC73-2497F7CD8FD8}.Release|x64.ActiveCfg = Release|Any CPU
{41B05546-CC58-4546-AC73-2497F7CD8FD8}.Release|x64.Build.0 = Release|Any CPU
{41B05546-CC58-4546-AC73-2497F7CD8FD8}.Release|x86.ActiveCfg = Release|Any CPU
{41B05546-CC58-4546-AC73-2497F7CD8FD8}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{061494E1-D5D7-479E-BE88-FE0E2F94D9F5} = {3E7EDB35-637A-402C-ABA6-EF8E4C55C051}
{DE41D67B-F18B-4EAD-A1AC-D7AAB1DEB55E} = {B622C013-E4C5-4632-A263-B652C38D2E27}
{DC70B54C-4F4E-4826-A41B-99E976E9475A} = {B2B54384-A507-44F4-B611-039F7CC27AC0}
{37D377D0-7C5A-4F44-B535-C9DA60FDB723} = {B2B54384-A507-44F4-B611-039F7CC27AC0}
{41B05546-CC58-4546-AC73-2497F7CD8FD8} = {B2B54384-A507-44F4-B611-039F7CC27AC0}
EndGlobalSection
EndGlobal
24 changes: 1 addition & 23 deletions src/Honeycomb.OpenTelemetry/Honeycomb.OpenTelemetry.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0;net462;</TargetFrameworks>
<TargetFrameworks>net6.0;netstandard2.1;netstandard2.0;net462</TargetFrameworks>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<!-- For SourceLink. See: https://github.com/dotnet/sourcelink#using-source-link-in-net-projects -->
Expand Down Expand Up @@ -29,34 +29,12 @@
<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.3.0 " />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.3.0" />
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.0.0-rc9.4" />
<PackageReference Include="OpenTelemetry.Instrumentation.SqlClient" Version="1.0.0-rc9.4" />
<PackageReference Include="OpenTelemetry.Instrumentation.StackExchangeRedis" Version="1.0.0-rc9.4" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.1.1" PrivateAssets="All" />
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="System.Text.Json" Version="6.0.5" />
</ItemGroup>

<Choose>
<When Condition=" '$(TargetFramework)' == 'net462' ">
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi" Version="5.2.7" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNet" Version="1.0.0-rc9.5" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.0.0-rc9.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.0.0-rc9.1" />
</ItemGroup>
</Otherwise>
</Choose>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.1' ">
<PackageReference Include="OpenTelemetry.Instrumentation.GrpcNetClient" Version="1.0.0-rc9.1" />
</ItemGroup>

<ItemGroup>
<None Include="../../honeycomb.png" Pack="true" Visible="false" PackagePath="" />
</ItemGroup>
Expand Down
119 changes: 63 additions & 56 deletions src/Honeycomb.OpenTelemetry/HoneycombOptions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
using Microsoft.Extensions.Configuration;
using OpenTelemetry.Instrumentation.Http;
using OpenTelemetry.Instrumentation.SqlClient;
using OpenTelemetry.Instrumentation.StackExchangeRedis;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
Expand All @@ -16,6 +12,7 @@ namespace Honeycomb.OpenTelemetry
/// </summary>
public class HoneycombOptions
{
private const string OtlpVersion = "0.16.0";

/// <summary>
/// Default service name if service name is not provided.
Expand Down Expand Up @@ -56,15 +53,23 @@ public class HoneycombOptions
/// <remarks>
/// Legacy keys have 32 characters.
/// </remarks>
internal bool IsTracesLegacyKey() => TracesApiKey?.Length == 32;
internal bool IsTracesLegacyKey() => IsClassicKey(TracesApiKey);

/// <summary>
/// Returns whether API key used to send metrics telemetry is a legacy key.
/// </summary>
/// <remarks>
/// Legacy keys have 32 characters.
/// </remarks>
internal bool IsMetricsLegacyKey() => MetricsApiKey?.Length == 32;
internal bool IsMetricsLegacyKey() => IsClassicKey(MetricsApiKey);

/// <summary>
/// Returns whether the provided API key is a legacy key.
/// </summary>
/// <remarks>
/// Legacy keys have 32 characters.
/// </remarks>
internal static bool IsClassicKey(string apikey) => apikey?.Length == 32;

/// <summary>
/// Write links to honeycomb traces as they come in
Expand Down Expand Up @@ -152,52 +157,6 @@ public string MetricsEndpoint
/// </summary>
public string ServiceVersion { get; set; } = SDefaultServiceVersion;

/// <summary>
/// Redis <see cref="IConnectionMultiplexer"/>. Set this if you aren't using a DI Container.
/// If you're using a DI Container, then setting this isn't necessary as it will be resolved from the <see cref="IServiceProvider"/>.
/// </summary>
public IConnectionMultiplexer RedisConnection { get; set; }

/// <summary>
/// Controls whether to instrument HttpClient calls.
/// </summary>
public bool InstrumentHttpClient { get; set; } = true;

/// <summary>
/// Controls whether to instrument SqlClient calls.
/// </summary>
public bool InstrumentSqlClient { get; set; } = true;

/// <summary>
/// Controls whether to instrument GrpcClient calls when running on .NET Standard 2.1 or greater.
/// Requires <see cref="InstrumentHttpClient" /> to be <see langword="true"/> due to the underlying implementation.
/// </summary>
public bool InstrumentGrpcClient { get; set; } = true;

/// <summary>
/// Controls whether the Stack Exchange Redis Client is instrumented.
/// Requires that either <see cref="RedisConnection"/> is set, if you're not using a DI Container, or
/// if you are using a DI Container, then it requires that an <see cref="IConnectionMultiplexer"/> has been registered with the <see cref="IServiceProvider"/>.
/// </summary>
public bool InstrumentStackExchangeRedisClient { get; set; } = true;

/// <summary>
/// (Optional) Options delegate to configure HttpClient instrumentation.
/// </summary>
public Action<HttpClientInstrumentationOptions> ConfigureHttpClientInstrumentationOptions { get; set; }

/// <summary>
/// (Optional) Options delegate to configure SqlClient instrumentation.
/// </summary>
public Action<SqlClientInstrumentationOptions> ConfigureSqlClientInstrumentationOptions { get; set; }

/// <summary>
/// (Optional) Options delegate to configure StackExchange.Redis instrumentation.
/// </summary>
public Action<StackExchangeRedisCallsInstrumentationOptions>
ConfigureStackExchangeRedisClientInstrumentationOptions
{ get; set; }

/// <summary>
/// (Optional) Additional <see cref="Meter"/> names for generating metrics.
/// <see cref="ServiceName"/> is configured as a meter name by default.
Expand All @@ -211,6 +170,15 @@ public Action<StackExchangeRedisCallsInstrumentationOptions>
/// </summary>
public ResourceBuilder ResourceBuilder { get; set; } = ResourceBuilder.CreateDefault();

/// <summary>
/// Determines whether the <see cref="BaggageSpanProcessor"/> is added when configuring a <see cref="TracerProviderBuilder"/>.
/// </summary>
public bool AddBaggageSpanProcessor { get; set; } = true;

/// <summary>
/// Determines whether the <see cref="DeterministicSampler"/> sampler is added when configuring a <see cref="TracerProviderBuilder"/>.
/// </summary>
public bool AddDeterministicSampler { get; set; } = true;
private static readonly Dictionary<string, string> CommandLineSwitchMap = new Dictionary<string, string>
{
{ "--honeycomb-apikey", "apikey" },
Expand All @@ -224,12 +192,10 @@ public Action<StackExchangeRedisCallsInstrumentationOptions>
{ "--honeycomb-metrics-endpoint", "metricsendpoint" },
{ "--honeycomb-samplerate", "samplerate" },
{ "--honeycomb-enable-local-visualizations", "enablelocalvisualizations" },
{ "--honeycomb-add-baggage-span-processor", "addBaggageSpanProcessor" },
{ "--honeycomb-add-determinisitc-sampler", "addDeterministicSampler" },
{ "--service-name", "servicename" },
{ "--service-version", "serviceversion" },
{ "--instrument-http", "instrumenthttpclient" },
{ "--instrument-sql", "instrumentsqlclient" },
{ "--instrument-grpc", "instrumentgrpcclient" },
{ "--instrument-redis", "instrumentstackexchangeredisclient" },
{ "--meter-names", "meternames" }
};

Expand All @@ -251,5 +217,46 @@ public static HoneycombOptions FromArgs(params string[] args)

return honeycombOptions;
}

internal string GetTraceHeaders() {
return GetTraceHeaders(TracesApiKey, TracesDataset);
}

internal static string GetTraceHeaders(string apikey, string dataset) {
var headers = new List<string>
{
$"x-otlp-version={OtlpVersion}",
$"x-honeycomb-team={apikey}"
};
if (IsClassicKey(apikey))
{
// if the key is legacy, add dataset to the header
if (!string.IsNullOrWhiteSpace(dataset))
{
headers.Add($"x-honeycomb-dataset={dataset}");
}
else
{
// if legacy key and missing dataset, warn on missing dataset
Console.WriteLine($"WARN: {EnvironmentOptions.GetErrorMessage("dataset", "HONEYCOMB_DATASET")}.");
}
}
return string.Join(",", headers);
}

internal string GetMetricsHeaders()
{
return GetMetricsHeaders(MetricsApiKey, MetricsDataset);
}

internal static string GetMetricsHeaders(string apikey, string dataset) {
var headers = new List<string>
{
$"x-otlp-version={OtlpVersion}",
$"x-honeycomb-team={apikey}",
$"x-honeycomb-dataset={dataset}"
};
return string.Join(",", headers);
}
}
}
Loading

0 comments on commit a954d12

Please sign in to comment.