Skip to content

Commit

Permalink
renaming and changing spec version
Browse files Browse the repository at this point in the history
Signed-off-by: Kavindu Dodanduwa <kavindudodanduwa@gmail.com>
  • Loading branch information
Kavindu-Dodan committed Jul 14, 2023
1 parent d933cd7 commit 7315e80
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 57 deletions.
2 changes: 1 addition & 1 deletion hooks/open-telemetry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>
<groupId>dev.openfeature.contrib.hooks</groupId>
<artifactId>otel</artifactId>
<version>1.0.3</version> <!--x-release-please-version -->
<version>2.0.0</version> <!--x-release-please-version -->

<name>open-telemetry-hook</name>
<description>Open Telemetry Hook</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import lombok.Getter;

/**
* Represents a Key and Type of dimension.
* Represents an OTel dimension(attribute) Key and Type of the dimension.
*/
@Getter
@AllArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,18 @@
import java.util.Map;
import java.util.Optional;

import static dev.openfeature.contrib.hooks.otel.OtelCommons.ERROR_KEY;
import static dev.openfeature.contrib.hooks.otel.OtelCommons.REASON_KEY;
import static dev.openfeature.contrib.hooks.otel.OtelCommons.flagKeyAttributeKey;
import static dev.openfeature.contrib.hooks.otel.OtelCommons.providerNameAttributeKey;
import static dev.openfeature.contrib.hooks.otel.OtelCommons.variantAttributeKey;

import static dev.openfeature.contrib.hooks.otel.OTelCommons.ERROR_KEY;
import static dev.openfeature.contrib.hooks.otel.OTelCommons.REASON_KEY;
import static dev.openfeature.contrib.hooks.otel.OTelCommons.flagKeyAttributeKey;
import static dev.openfeature.contrib.hooks.otel.OTelCommons.providerNameAttributeKey;
import static dev.openfeature.contrib.hooks.otel.OTelCommons.variantAttributeKey;

/**
* OpenTelemetry metric hook records metrics at different {@link Hook} stages.
*/
@Slf4j
@SuppressWarnings("PMD.TooManyStaticImports")
public class OpenTelemetryMetricHook implements Hook {
public class MetricsHook implements Hook {

private static final String METER_NAME = "java.openfeature.dev";
private static final String EVALUATION_ACTIVE_COUNT = "feature_flag.evaluation_active_count";
Expand All @@ -47,15 +46,15 @@ public class OpenTelemetryMetricHook implements Hook {
/**
* Construct a metric hook by providing an {@link OpenTelemetry} instance.
*/
public OpenTelemetryMetricHook(final OpenTelemetry openTelemetry) {
public MetricsHook(final OpenTelemetry openTelemetry) {
this(openTelemetry, Collections.emptyList());
}

/**
* Construct a metric hook with {@link OpenTelemetry} instance and a list of {@link DimensionDescription}.
* Provided dimensions are attempted to be extracted from flagMetadata attached to {@link FlagEvaluationDetails}.
* Provided dimensions are attempted to be extracted from ImmutableMetadata attached to {@link FlagEvaluationDetails}.
*/
public OpenTelemetryMetricHook(final OpenTelemetry openTelemetry, final List<DimensionDescription> dimensions) {
public MetricsHook(final OpenTelemetry openTelemetry, final List<DimensionDescription> dimensions) {
final Meter meter = openTelemetry.getMeter(METER_NAME);

activeFlagEvaluationsCounter =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import io.opentelemetry.api.common.AttributeKey;

class OtelCommons {
class OTelCommons {
// Define semantic conventions
// Refer - https://opentelemetry.io/docs/specs/otel/logs/semantic_conventions/feature-flags/
static final String EVENT_NAME = "feature_flag";
Expand All @@ -11,9 +11,6 @@ class OtelCommons {
static final AttributeKey<String> variantAttributeKey = AttributeKey.stringKey(EVENT_NAME + ".variant");

// Define non convention attribute keys


static final String REASON_KEY = "reason";
static final String ERROR_KEY = "exception";

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,29 @@

import java.util.Map;

import static dev.openfeature.contrib.hooks.otel.OtelCommons.EVENT_NAME;
import static dev.openfeature.contrib.hooks.otel.OtelCommons.flagKeyAttributeKey;
import static dev.openfeature.contrib.hooks.otel.OtelCommons.providerNameAttributeKey;
import static dev.openfeature.contrib.hooks.otel.OtelCommons.variantAttributeKey;
import static dev.openfeature.contrib.hooks.otel.OTelCommons.EVENT_NAME;
import static dev.openfeature.contrib.hooks.otel.OTelCommons.flagKeyAttributeKey;
import static dev.openfeature.contrib.hooks.otel.OTelCommons.providerNameAttributeKey;
import static dev.openfeature.contrib.hooks.otel.OTelCommons.variantAttributeKey;

/**
* The OpenTelemetry hook provides a way to automatically add a feature flag evaluation to a span as a span event.
* Refer to <a href="https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/feature-flags.md">OpenTelemetry</a>
*/
public class OpenTelemetryHook implements Hook {
public class TracesHook implements Hook {
private final boolean setSpanErrorStatus;

/**
* Create a new OpenTelemetryHook instance with default options.
*/
public OpenTelemetryHook() {
this(OpenTelemetryHookOptions.builder().build());
public TracesHook() {
this(TracesHookOptions.builder().build());
}

/**
* Create a new OpenTelemetryHook instance with options.
*/
public OpenTelemetryHook(OpenTelemetryHookOptions options) {
public TracesHook(TracesHookOptions options) {
setSpanErrorStatus = options.isSetSpanErrorStatus();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
*/
@Builder
@Getter
public class OpenTelemetryHookOptions {

public class TracesHookOptions {
/**
* Control Span error status. Default is false - Span status is unchanged for hook error
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
import java.util.List;
import java.util.Optional;

import static dev.openfeature.contrib.hooks.otel.OtelCommons.ERROR_KEY;
import static dev.openfeature.contrib.hooks.otel.OtelCommons.REASON_KEY;
import static dev.openfeature.contrib.hooks.otel.OtelCommons.flagKeyAttributeKey;
import static dev.openfeature.contrib.hooks.otel.OtelCommons.providerNameAttributeKey;
import static dev.openfeature.contrib.hooks.otel.OtelCommons.variantAttributeKey;
import static dev.openfeature.contrib.hooks.otel.OTelCommons.ERROR_KEY;
import static dev.openfeature.contrib.hooks.otel.OTelCommons.REASON_KEY;
import static dev.openfeature.contrib.hooks.otel.OTelCommons.flagKeyAttributeKey;
import static dev.openfeature.contrib.hooks.otel.OTelCommons.providerNameAttributeKey;
import static dev.openfeature.contrib.hooks.otel.OTelCommons.variantAttributeKey;
import static org.assertj.core.api.Assertions.assertThat;

class OpenTelemetryMetricHookTest {
class MetricsHookTest {
private final HookContext<String> commonHookContext = HookContext.<String>builder()
.flagKey("key")
.type(FlagValueType.STRING)
Expand All @@ -45,8 +45,7 @@ public void setup() {
@Test
public void before_stage_validation() {
// given
final OpenTelemetryMetricHook metricHook =
new OpenTelemetryMetricHook(telemetryExtension.getOpenTelemetry());
final MetricsHook metricHook = new MetricsHook(telemetryExtension.getOpenTelemetry());

final List<String> metricNames =
Arrays.asList("feature_flag.evaluation_active_count", "feature_flag.evaluation_requests_total");
Expand All @@ -63,8 +62,7 @@ public void before_stage_validation() {
@Test
public void after_stage_validation() {
// given
final OpenTelemetryMetricHook metricHook =
new OpenTelemetryMetricHook(telemetryExtension.getOpenTelemetry());
final MetricsHook metricHook = new MetricsHook(telemetryExtension.getOpenTelemetry());

final FlagEvaluationDetails<String> evaluationDetails = FlagEvaluationDetails.<String>builder()
.flagKey("key")
Expand Down Expand Up @@ -107,8 +105,7 @@ public void after_stage_validation_of_custom_dimensions(){
dimensionList.add(new DimensionDescription("double", Double.class));
dimensionList.add(new DimensionDescription("string", String.class));

final OpenTelemetryMetricHook metricHook =
new OpenTelemetryMetricHook(telemetryExtension.getOpenTelemetry(), dimensionList);
final MetricsHook metricHook = new MetricsHook(telemetryExtension.getOpenTelemetry(), dimensionList);

final ImmutableMetadata metadata = ImmutableMetadata.builder()
.addBoolean("boolean", true)
Expand Down Expand Up @@ -152,8 +149,7 @@ public void after_stage_validation_of_custom_dimensions(){
@Test
public void error_stage_validation() {
// given
final OpenTelemetryMetricHook metricHook =
new OpenTelemetryMetricHook(telemetryExtension.getOpenTelemetry());
final MetricsHook metricHook = new MetricsHook(telemetryExtension.getOpenTelemetry());

final Exception exception = new Exception("some_exception");

Expand Down Expand Up @@ -183,9 +179,7 @@ public void error_stage_validation() {
@Test
public void finally_stage_validation() {
// given
final OpenTelemetryMetricHook metricHook =
new OpenTelemetryMetricHook(telemetryExtension.getOpenTelemetry());

final MetricsHook metricHook = new MetricsHook(telemetryExtension.getOpenTelemetry());

// when
metricHook.finallyAfter(commonHookContext, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import static org.mockito.Mockito.verifyNoInteractions;

@ExtendWith(MockitoExtension.class)
class OpenTelemetryHookTest {
class TracesHookTest {
private static MockedStatic<Span> mockedSpan;

@BeforeAll
Expand Down Expand Up @@ -60,8 +60,8 @@ void should_add_event_in_span_during_after_method_execution() {
.build();
mockedSpan.when(Span::current).thenReturn(span);

OpenTelemetryHook openTelemetryHook = new OpenTelemetryHook();
openTelemetryHook.after(hookContext, details, null);
TracesHook tracesHook = new TracesHook();
tracesHook.after(hookContext, details, null);

Attributes expectedAttr = Attributes.of(flagKeyAttributeKey, "test_key",
providerNameAttributeKey, "test provider",
Expand All @@ -79,8 +79,8 @@ void attribute_should_fallback_to_value_field_when_variant_is_null() {
mockedSpan.when(Span::current).thenReturn(span);


OpenTelemetryHook openTelemetryHook = new OpenTelemetryHook();
openTelemetryHook.after(hookContext, details, null);
TracesHook tracesHook = new TracesHook();
tracesHook.after(hookContext, details, null);

Attributes expectedAttr = Attributes.of(flagKeyAttributeKey, "test_key",
providerNameAttributeKey, "test provider",
Expand All @@ -105,8 +105,8 @@ void should_not_call_add_event_when_no_active_span() {
.build();
mockedSpan.when(Span::current).thenReturn(null);

OpenTelemetryHook openTelemetryHook = new OpenTelemetryHook();
openTelemetryHook.after(hookContext, details, null);
TracesHook tracesHook = new TracesHook();
tracesHook.after(hookContext, details, null);

verifyNoInteractions(span);
}
Expand All @@ -117,8 +117,8 @@ void should_record_exception_and_status_in_span_during_error_method_execution()
RuntimeException runtimeException = new RuntimeException("could not resolve the flag");
mockedSpan.when(Span::current).thenReturn(span);

OpenTelemetryHook openTelemetryHook = new OpenTelemetryHook();
openTelemetryHook.error(hookContext, runtimeException, null);
TracesHook tracesHook = new TracesHook();
tracesHook.error(hookContext, runtimeException, null);

Attributes expectedAttr = Attributes.of(flagKeyAttributeKey, "test_key",
providerNameAttributeKey, "test provider");
Expand All @@ -133,12 +133,12 @@ void should_record_exception_but_not_status_in_span_with_options() {
RuntimeException runtimeException = new RuntimeException("could not resolve the flag");
mockedSpan.when(Span::current).thenReturn(span);

OpenTelemetryHook openTelemetryHook =
new OpenTelemetryHook(OpenTelemetryHookOptions
TracesHook tracesHook =
new TracesHook(TracesHookOptions
.builder()
.setSpanErrorStatus(true)
.build());
openTelemetryHook.error(hookContext, runtimeException, null);
tracesHook.error(hookContext, runtimeException, null);

Attributes expectedAttr = Attributes.of(flagKeyAttributeKey, "test_key",
providerNameAttributeKey, "test provider");
Expand All @@ -153,8 +153,8 @@ void should_not_call_record_exception_when_no_active_span() {
RuntimeException runtimeException = new RuntimeException("could not resolve the flag");
mockedSpan.when(Span::current).thenReturn(null);

OpenTelemetryHook openTelemetryHook = new OpenTelemetryHook();
openTelemetryHook.error(hookContext, runtimeException, null);
TracesHook tracesHook = new TracesHook();
tracesHook.error(hookContext, runtimeException, null);

verifyNoInteractions(span);
}
Expand Down

0 comments on commit 7315e80

Please sign in to comment.