Skip to content

Commit 755c549

Browse files
authored
refactor: Replace OtelHook with TracingHook. Deprecating OtelHook (#116)
Signed-off-by: André Silva <[email protected]>
1 parent 7697585 commit 755c549

File tree

4 files changed

+77
-26
lines changed

4 files changed

+77
-26
lines changed

src/OpenFeature.Contrib.Hooks.Otel/OtelHook.cs

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
using OpenFeature;
2-
using OpenFeature.Model;
1+
using OpenFeature.Model;
32
using System.Threading.Tasks;
43
using System.Collections.Generic;
5-
using OpenTelemetry.Context.Propagation;
6-
using OpenTelemetry.Trace;
4+
using System.Diagnostics.CodeAnalysis;
5+
using System;
76

87
namespace OpenFeature.Contrib.Hooks.Otel
98

109
{
1110
/// <summary>
1211
/// Stub.
1312
/// </summary>
13+
[ExcludeFromCodeCoverage]
14+
[Obsolete("This class is obsolete and will be removed in a future version. Please use TracingHook instead.")]
1415
public class OtelHook : Hook
1516
{
17+
private readonly TracingHook _tracingHook = new TracingHook();
1618

1719
/// <summary>
1820
/// After is executed after a feature flag has been evaluated.
@@ -24,17 +26,7 @@ public class OtelHook : Hook
2426
public override Task After<T>(HookContext<T> context, FlagEvaluationDetails<T> details,
2527
IReadOnlyDictionary<string, object> hints = null)
2628
{
27-
var span = Tracer.CurrentSpan;
28-
if (span != null)
29-
{
30-
var attributes = new Dictionary<string, object>
31-
{
32-
{"feature_flag.key", details.FlagKey},
33-
{"feature_flag.variant", details.Variant},
34-
{"feature_flag.provider_name", context.ProviderMetadata.Name}
35-
};
36-
span.AddEvent("feature_flag", new SpanAttributes(attributes));
37-
}
29+
_tracingHook.After(context, details, hints);
3830

3931
return Task.CompletedTask;
4032
}
@@ -49,11 +41,7 @@ public override Task After<T>(HookContext<T> context, FlagEvaluationDetails<T> d
4941
public override Task Error<T>(HookContext<T> context, System.Exception error,
5042
IReadOnlyDictionary<string, object> hints = null)
5143
{
52-
var span = Tracer.CurrentSpan;
53-
if (span != null)
54-
{
55-
span.RecordException(error);
56-
}
44+
_tracingHook.Error(context, error, hints);
5745

5846
return Task.CompletedTask;
5947
}

src/OpenFeature.Contrib.Hooks.Otel/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ The `open telemetry hook` taps into the after and error methods of the hook life
1212
For this, an active span must be set in the `Tracer`, otherwise the hook will no-op.
1313

1414
### Example
15+
1516
The following example demonstrates the use of the `OpenTelemetry hook` with the `OpenFeature dotnet-sdk`. The traces are sent to a `jaeger` OTLP collector running at `localhost:4317`.
1617

1718
```csharp
@@ -38,7 +39,7 @@ namespace OpenFeatureTestApp
3839
.Build();
3940

4041
// add the Otel Hook to the OpenFeature instance
41-
OpenFeature.Api.Instance.AddHooks(new OtelHook());
42+
OpenFeature.Api.Instance.AddHooks(new TracingHook());
4243

4344
var flagdProvider = new FlagdProvider(new Uri("http://localhost:8013"));
4445

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
using OpenFeature.Model;
2+
using System.Threading.Tasks;
3+
using System.Collections.Generic;
4+
using OpenTelemetry.Trace;
5+
6+
namespace OpenFeature.Contrib.Hooks.Otel
7+
8+
{
9+
/// <summary>
10+
/// Stub.
11+
/// </summary>
12+
public class TracingHook : Hook
13+
{
14+
15+
/// <summary>
16+
/// After is executed after a feature flag has been evaluated.
17+
/// </summary>
18+
/// <param name="context">The hook context</param>
19+
/// <param name="details">The result of the feature flag evaluation</param>
20+
/// <param name="hints">Hints for the feature flag evaluation</param>
21+
/// <returns>An awaitable Task object</returns>
22+
public override Task After<T>(HookContext<T> context, FlagEvaluationDetails<T> details,
23+
IReadOnlyDictionary<string, object> hints = null)
24+
{
25+
var span = Tracer.CurrentSpan;
26+
if (span != null)
27+
{
28+
var attributes = new Dictionary<string, object>
29+
{
30+
{"feature_flag.key", details.FlagKey},
31+
{"feature_flag.variant", details.Variant},
32+
{"feature_flag.provider_name", context.ProviderMetadata.Name}
33+
};
34+
span.AddEvent("feature_flag", new SpanAttributes(attributes));
35+
}
36+
37+
return Task.CompletedTask;
38+
}
39+
40+
/// <summary>
41+
/// Error is executed when an error during a feature flag evaluation occured.
42+
/// </summary>
43+
/// <param name="context">The hook context</param>
44+
/// <param name="error">The exception thrown by feature flag provider</param>
45+
/// <param name="hints">Hints for the feature flag evaluation</param>
46+
/// <returns>An awaitable Task object</returns>
47+
public override Task Error<T>(HookContext<T> context, System.Exception error,
48+
IReadOnlyDictionary<string, object> hints = null)
49+
{
50+
var span = Tracer.CurrentSpan;
51+
if (span != null)
52+
{
53+
span.RecordException(error);
54+
}
55+
56+
return Task.CompletedTask;
57+
}
58+
59+
}
60+
}
61+
62+

test/OpenFeature.Contrib.Hooks.Otel.Test/OtelHookTest.cs renamed to test/OpenFeature.Contrib.Hooks.Otel.Test/TracingHookTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace OpenFeature.Contrib.Hooks.Otel.Test
1212
{
13-
public class OtelHookTest
13+
public class TracingHookTest
1414
{
1515
[Fact]
1616
public void TestAfter()
@@ -32,7 +32,7 @@ public void TestAfter()
3232

3333
var span = tracer.StartActiveSpan("my-span");
3434

35-
var otelHook = new OtelHook();
35+
var otelHook = new TracingHook();
3636

3737
var evaluationContext = OpenFeature.Model.EvaluationContext.Empty;
3838

@@ -93,7 +93,7 @@ public void TestAfterNoSpan()
9393

9494
var tracer = tracerProvider.GetTracer("my-tracer");
9595

96-
var otelHook = new OtelHook();
96+
var otelHook = new TracingHook();
9797

9898
var evaluationContext = OpenFeature.Model.EvaluationContext.Empty;
9999

@@ -126,7 +126,7 @@ public void TestError()
126126

127127
var span = tracer.StartActiveSpan("my-span");
128128

129-
var otelHook = new OtelHook();
129+
var otelHook = new TracingHook();
130130

131131
var evaluationContext = OpenFeature.Model.EvaluationContext.Empty;
132132

@@ -175,7 +175,7 @@ public void TestErrorNoSpan()
175175

176176
var tracer = tracerProvider.GetTracer("my-tracer");
177177

178-
var otelHook = new OtelHook();
178+
var otelHook = new TracingHook();
179179

180180
var evaluationContext = OpenFeature.Model.EvaluationContext.Empty;
181181

0 commit comments

Comments
 (0)