Skip to content

Commit

Permalink
refactor: Reduce OTel dependencies
Browse files Browse the repository at this point in the history
The `OpenFeature.Contrib.Hooks.Otel` project currently references
(1) an exporter implementation which libraries should _not_ do
under any normal circumstances, and (2) the main OpenTelemetry
project which is excessive given all of the excellent work by
the dotnet/runtime and open-telemetry/opentelemetry-dotnet teams
have done over the last couple of years to ensure that the BCL
types in `System.Diagnostics` conform to OTel standards.

This commit removes (1) and pares down (2) to from `OpenTelemetry`
to `OpenTelemetry.Api` to shrink the dependency footprint we ship
to consumers.

At this point, I would argue that this hooks package should meet
the minimal dependency requirement for upstream inclusion in the
main OpenFeature package, but deferring that discussion for now.

Signed-off-by: Austin Drenski <austin@austindrenski.io>
  • Loading branch information
austindrenski committed Jan 11, 2024
1 parent e3d0f06 commit 26d42b9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="OpenTelemetry" Version="1.4.0" />
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.4.0" />
<PackageReference Include="OpenTelemetry.Api" Version="1.4.0" />
</ItemGroup>

</Project>
26 changes: 11 additions & 15 deletions src/OpenFeature.Contrib.Hooks.Otel/TracingHook.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using OpenFeature.Model;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Diagnostics;
using OpenTelemetry.Trace;

namespace OpenFeature.Contrib.Hooks.Otel
Expand All @@ -22,17 +23,16 @@ public class TracingHook : Hook
public override Task After<T>(HookContext<T> context, FlagEvaluationDetails<T> details,
IReadOnlyDictionary<string, object> hints = null)
{
var span = Tracer.CurrentSpan;
if (span != null)
{
var attributes = new Dictionary<string, object>
Activity.Current?
.SetTag("feature_flag.key", details.FlagKey)
.SetTag("feature_flag.variant", details.Variant)
.SetTag("feature_flag.provider_name", context.ProviderMetadata.Name)
.AddEvent(new ActivityEvent("feature_flag", tags: new ActivityTagsCollection
{
{"feature_flag.key", details.FlagKey},
{"feature_flag.variant", details.Variant},
{"feature_flag.provider_name", context.ProviderMetadata.Name}
};
span.AddEvent("feature_flag", new SpanAttributes(attributes));
}
["feature_flag.key"] = details.FlagKey,
["feature_flag.variant"] = details.Variant,
["feature_flag.provider_name"] = context.ProviderMetadata.Name
}));

return Task.CompletedTask;
}
Expand All @@ -47,11 +47,7 @@ public override Task After<T>(HookContext<T> context, FlagEvaluationDetails<T> d
public override Task Error<T>(HookContext<T> context, System.Exception error,
IReadOnlyDictionary<string, object> hints = null)
{
var span = Tracer.CurrentSpan;
if (span != null)
{
span.RecordException(error);
}
Activity.Current?.RecordException(error);

return Task.CompletedTask;
}
Expand Down

0 comments on commit 26d42b9

Please sign in to comment.