Skip to content

Commit

Permalink
refactor: Replace OtelHook with TracingHook. Deprecating OtelHook (#116)
Browse files Browse the repository at this point in the history
Signed-off-by: André Silva <[email protected]>
  • Loading branch information
askpt authored Dec 18, 2023
1 parent 7697585 commit 755c549
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 26 deletions.
28 changes: 8 additions & 20 deletions src/OpenFeature.Contrib.Hooks.Otel/OtelHook.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
using OpenFeature;
using OpenFeature.Model;
using OpenFeature.Model;
using System.Threading.Tasks;
using System.Collections.Generic;
using OpenTelemetry.Context.Propagation;
using OpenTelemetry.Trace;
using System.Diagnostics.CodeAnalysis;
using System;

namespace OpenFeature.Contrib.Hooks.Otel

{
/// <summary>
/// Stub.
/// </summary>
[ExcludeFromCodeCoverage]
[Obsolete("This class is obsolete and will be removed in a future version. Please use TracingHook instead.")]
public class OtelHook : Hook
{
private readonly TracingHook _tracingHook = new TracingHook();

/// <summary>
/// After is executed after a feature flag has been evaluated.
Expand All @@ -24,17 +26,7 @@ public class OtelHook : 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>
{
{"feature_flag.key", details.FlagKey},
{"feature_flag.variant", details.Variant},
{"feature_flag.provider_name", context.ProviderMetadata.Name}
};
span.AddEvent("feature_flag", new SpanAttributes(attributes));
}
_tracingHook.After(context, details, hints);

return Task.CompletedTask;
}
Expand All @@ -49,11 +41,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);
}
_tracingHook.Error(context, error, hints);

return Task.CompletedTask;
}
Expand Down
3 changes: 2 additions & 1 deletion src/OpenFeature.Contrib.Hooks.Otel/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ The `open telemetry hook` taps into the after and error methods of the hook life
For this, an active span must be set in the `Tracer`, otherwise the hook will no-op.

### Example

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`.

```csharp
Expand All @@ -38,7 +39,7 @@ namespace OpenFeatureTestApp
.Build();

// add the Otel Hook to the OpenFeature instance
OpenFeature.Api.Instance.AddHooks(new OtelHook());
OpenFeature.Api.Instance.AddHooks(new TracingHook());

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

Expand Down
62 changes: 62 additions & 0 deletions src/OpenFeature.Contrib.Hooks.Otel/TracingHook.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using OpenFeature.Model;
using System.Threading.Tasks;
using System.Collections.Generic;
using OpenTelemetry.Trace;

namespace OpenFeature.Contrib.Hooks.Otel

{
/// <summary>
/// Stub.
/// </summary>
public class TracingHook : Hook
{

/// <summary>
/// After is executed after a feature flag has been evaluated.
/// </summary>
/// <param name="context">The hook context</param>
/// <param name="details">The result of the feature flag evaluation</param>
/// <param name="hints">Hints for the feature flag evaluation</param>
/// <returns>An awaitable Task object</returns>
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>
{
{"feature_flag.key", details.FlagKey},
{"feature_flag.variant", details.Variant},
{"feature_flag.provider_name", context.ProviderMetadata.Name}
};
span.AddEvent("feature_flag", new SpanAttributes(attributes));
}

return Task.CompletedTask;
}

/// <summary>
/// Error is executed when an error during a feature flag evaluation occured.
/// </summary>
/// <param name="context">The hook context</param>
/// <param name="error">The exception thrown by feature flag provider</param>
/// <param name="hints">Hints for the feature flag evaluation</param>
/// <returns>An awaitable Task object</returns>
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);
}

return Task.CompletedTask;
}

}
}


Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace OpenFeature.Contrib.Hooks.Otel.Test
{
public class OtelHookTest
public class TracingHookTest
{
[Fact]
public void TestAfter()
Expand All @@ -32,7 +32,7 @@ public void TestAfter()

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

var otelHook = new OtelHook();
var otelHook = new TracingHook();

var evaluationContext = OpenFeature.Model.EvaluationContext.Empty;

Expand Down Expand Up @@ -93,7 +93,7 @@ public void TestAfterNoSpan()

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

var otelHook = new OtelHook();
var otelHook = new TracingHook();

var evaluationContext = OpenFeature.Model.EvaluationContext.Empty;

Expand Down Expand Up @@ -126,7 +126,7 @@ public void TestError()

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

var otelHook = new OtelHook();
var otelHook = new TracingHook();

var evaluationContext = OpenFeature.Model.EvaluationContext.Empty;

Expand Down Expand Up @@ -175,7 +175,7 @@ public void TestErrorNoSpan()

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

var otelHook = new OtelHook();
var otelHook = new TracingHook();

var evaluationContext = OpenFeature.Model.EvaluationContext.Empty;

Expand Down

0 comments on commit 755c549

Please sign in to comment.