Skip to content

Commit

Permalink
refactor: Reduce OTel dependencies (open-feature#132)
Browse files Browse the repository at this point in the history
Signed-off-by: Austin Drenski <austin@austindrenski.io>
Co-authored-by: Todd Baert <todd.baert@dynatrace.com>
  • Loading branch information
austindrenski and toddbaert authored Jan 11, 2024
1 parent d3aea9e commit de86b0e
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 de86b0e

Please sign in to comment.