|
| 1 | +package dev.openfeature.contrib.hooks.otel; |
| 2 | + |
| 3 | +import dev.openfeature.sdk.EvaluationContext; |
| 4 | +import dev.openfeature.sdk.FlagEvaluationDetails; |
| 5 | +import dev.openfeature.sdk.Hook; |
| 6 | +import dev.openfeature.sdk.HookContext; |
| 7 | +import dev.openfeature.sdk.ImmutableMetadata; |
| 8 | +import io.opentelemetry.api.OpenTelemetry; |
| 9 | +import io.opentelemetry.api.common.Attributes; |
| 10 | +import io.opentelemetry.api.common.AttributesBuilder; |
| 11 | +import io.opentelemetry.api.metrics.LongCounter; |
| 12 | +import io.opentelemetry.api.metrics.LongUpDownCounter; |
| 13 | +import io.opentelemetry.api.metrics.Meter; |
| 14 | +import lombok.extern.slf4j.Slf4j; |
| 15 | + |
| 16 | +import java.util.Collections; |
| 17 | +import java.util.List; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Optional; |
| 20 | + |
| 21 | +import static dev.openfeature.contrib.hooks.otel.OTelCommons.ERROR_KEY; |
| 22 | +import static dev.openfeature.contrib.hooks.otel.OTelCommons.REASON_KEY; |
| 23 | +import static dev.openfeature.contrib.hooks.otel.OTelCommons.flagKeyAttributeKey; |
| 24 | +import static dev.openfeature.contrib.hooks.otel.OTelCommons.providerNameAttributeKey; |
| 25 | +import static dev.openfeature.contrib.hooks.otel.OTelCommons.variantAttributeKey; |
| 26 | + |
| 27 | +/** |
| 28 | + * OpenTelemetry metric hook records metrics at different {@link Hook} stages. |
| 29 | + */ |
| 30 | +@Slf4j |
| 31 | +@SuppressWarnings("PMD.TooManyStaticImports") |
| 32 | +public class MetricsHook implements Hook { |
| 33 | + |
| 34 | + private static final String METER_NAME = "java.openfeature.dev"; |
| 35 | + private static final String EVALUATION_ACTIVE_COUNT = "feature_flag.evaluation_active_count"; |
| 36 | + private static final String EVALUATION_REQUESTS_TOTAL = "feature_flag.evaluation_requests_total"; |
| 37 | + private static final String FLAG_EVALUATION_SUCCESS_TOTAL = "feature_flag.evaluation_success_total"; |
| 38 | + private static final String FLAG_EVALUATION_ERROR_TOTAL = "feature_flag.evaluation_error_total"; |
| 39 | + |
| 40 | + private final LongUpDownCounter activeFlagEvaluationsCounter; |
| 41 | + private final LongCounter evaluationRequestCounter; |
| 42 | + private final LongCounter evaluationSuccessCounter; |
| 43 | + private final LongCounter evaluationErrorCounter; |
| 44 | + private final List<DimensionDescription> dimensionDescriptions; |
| 45 | + |
| 46 | + /** |
| 47 | + * Construct a metric hook by providing an {@link OpenTelemetry} instance. |
| 48 | + */ |
| 49 | + public MetricsHook(final OpenTelemetry openTelemetry) { |
| 50 | + this(openTelemetry, Collections.emptyList()); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Construct a metric hook with {@link OpenTelemetry} instance and a list of {@link DimensionDescription}. |
| 55 | + * Provided dimensions are attempted to be extracted from ImmutableMetadata attached to |
| 56 | + * {@link FlagEvaluationDetails}. |
| 57 | + */ |
| 58 | + public MetricsHook(final OpenTelemetry openTelemetry, final List<DimensionDescription> dimensions) { |
| 59 | + final Meter meter = openTelemetry.getMeter(METER_NAME); |
| 60 | + |
| 61 | + activeFlagEvaluationsCounter = |
| 62 | + meter.upDownCounterBuilder(EVALUATION_ACTIVE_COUNT).setDescription("active flag evaluations counter") |
| 63 | + .build(); |
| 64 | + |
| 65 | + evaluationRequestCounter = meter.counterBuilder(EVALUATION_REQUESTS_TOTAL) |
| 66 | + .setDescription("feature flag evaluation request counter").build(); |
| 67 | + |
| 68 | + evaluationSuccessCounter = meter.counterBuilder(FLAG_EVALUATION_SUCCESS_TOTAL) |
| 69 | + .setDescription("feature flag evaluation success counter").build(); |
| 70 | + |
| 71 | + evaluationErrorCounter = meter.counterBuilder(FLAG_EVALUATION_ERROR_TOTAL) |
| 72 | + .setDescription("feature flag evaluation error counter").build(); |
| 73 | + |
| 74 | + dimensionDescriptions = Collections.unmodifiableList(dimensions); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | + @Override |
| 79 | + public Optional<EvaluationContext> before(HookContext ctx, Map hints) { |
| 80 | + activeFlagEvaluationsCounter.add(+1, Attributes.of(flagKeyAttributeKey, ctx.getFlagKey())); |
| 81 | + |
| 82 | + evaluationRequestCounter.add(+1, Attributes.of(flagKeyAttributeKey, ctx.getFlagKey(), providerNameAttributeKey, |
| 83 | + ctx.getProviderMetadata().getName())); |
| 84 | + return Optional.empty(); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + public void after(HookContext ctx, FlagEvaluationDetails details, Map hints) { |
| 89 | + final AttributesBuilder attributesBuilder = Attributes.builder(); |
| 90 | + |
| 91 | + attributesBuilder.put(flagKeyAttributeKey, ctx.getFlagKey()); |
| 92 | + attributesBuilder.put(providerNameAttributeKey, ctx.getProviderMetadata().getName()); |
| 93 | + |
| 94 | + if (details.getReason() != null) { |
| 95 | + attributesBuilder.put(REASON_KEY, details.getReason()); |
| 96 | + } |
| 97 | + |
| 98 | + if (details.getVariant() != null) { |
| 99 | + attributesBuilder.put(variantAttributeKey, details.getVariant()); |
| 100 | + } else { |
| 101 | + attributesBuilder.put(variantAttributeKey, String.valueOf(details.getValue())); |
| 102 | + } |
| 103 | + |
| 104 | + if (!dimensionDescriptions.isEmpty()) { |
| 105 | + attributesBuilder.putAll(attributesFromFlagMetadata(details.getFlagMetadata(), dimensionDescriptions)); |
| 106 | + } |
| 107 | + |
| 108 | + evaluationSuccessCounter.add(+1, attributesBuilder.build()); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void error(HookContext ctx, Exception error, Map hints) { |
| 113 | + final AttributesBuilder attributesBuilder = Attributes.builder(); |
| 114 | + |
| 115 | + attributesBuilder.put(flagKeyAttributeKey, ctx.getFlagKey()); |
| 116 | + attributesBuilder.put(providerNameAttributeKey, ctx.getProviderMetadata().getName()); |
| 117 | + |
| 118 | + if (error.getMessage() != null) { |
| 119 | + attributesBuilder.put(ERROR_KEY, error.getMessage()); |
| 120 | + } |
| 121 | + |
| 122 | + evaluationErrorCounter.add(+1, attributesBuilder.build()); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void finallyAfter(HookContext ctx, Map hints) { |
| 127 | + activeFlagEvaluationsCounter.add(-1, Attributes.of(flagKeyAttributeKey, ctx.getFlagKey())); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * A helper to derive attributes from {@link DimensionDescription}. |
| 132 | + */ |
| 133 | + private static Attributes attributesFromFlagMetadata(final ImmutableMetadata flagMetadata, |
| 134 | + final List<DimensionDescription> dimensionDescriptions) { |
| 135 | + final AttributesBuilder builder = Attributes.builder(); |
| 136 | + |
| 137 | + for (DimensionDescription dimension : dimensionDescriptions) { |
| 138 | + final Object value = flagMetadata.getValue(dimension.getKey(), dimension.getType()); |
| 139 | + |
| 140 | + if (value == null) { |
| 141 | + log.debug("No value mapping found for key " + dimension.getKey() + " of type " |
| 142 | + + dimension.getType().getSimpleName()); |
| 143 | + continue; |
| 144 | + } |
| 145 | + |
| 146 | + if (dimension.getType().equals(String.class)) { |
| 147 | + builder.put(dimension.getKey(), (String) value); |
| 148 | + } else if (dimension.getType().equals(Integer.class)) { |
| 149 | + builder.put(dimension.getKey(), (Integer) value); |
| 150 | + } |
| 151 | + if (dimension.getType().equals(Long.class)) { |
| 152 | + builder.put(dimension.getKey(), (Long) value); |
| 153 | + } |
| 154 | + if (dimension.getType().equals(Float.class)) { |
| 155 | + builder.put(dimension.getKey(), (Float) value); |
| 156 | + } |
| 157 | + if (dimension.getType().equals(Double.class)) { |
| 158 | + builder.put(dimension.getKey(), (Double) value); |
| 159 | + } |
| 160 | + if (dimension.getType().equals(Boolean.class)) { |
| 161 | + builder.put(dimension.getKey(), (Boolean) value); |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return builder.build(); |
| 166 | + } |
| 167 | +} |
0 commit comments