|
| 1 | +import 'dart:convert'; |
| 2 | + |
| 3 | +import 'package:launchdarkly_flutter_client_sdk/launchdarkly_flutter_client_sdk.dart'; |
| 4 | +import 'package:launchdarkly_flutter_observability/src/otel/stringable_attribute.dart'; |
| 5 | + |
| 6 | +const _featureFlagEventName = 'feature_flag'; |
| 7 | +const _featureFlagKeyAttr = '$_featureFlagEventName.key'; |
| 8 | + |
| 9 | +// Feature flag set. |
| 10 | +const _featureFlagSetAttr = '$_featureFlagEventName.set'; |
| 11 | +const _featureFlagSetIdAttr = '$_featureFlagSetAttr.id'; |
| 12 | + |
| 13 | +// Feature flag provider. |
| 14 | +const _featureFlagProviderAttr = '$_featureFlagEventName.provider'; |
| 15 | +const _featureFlagProviderNameAttr = '$_featureFlagProviderAttr.name'; |
| 16 | + |
| 17 | +// Feature flag context. |
| 18 | +const _featureFlagContextAttr = '$_featureFlagEventName.context'; |
| 19 | +const _featureFlagContextIdAttr = '$_featureFlagContextAttr.id'; |
| 20 | + |
| 21 | +// Feature flag result. |
| 22 | +const _featureFlagResultAttr = '$_featureFlagEventName.result'; |
| 23 | +const _featureFlagResultValueAttr = '$_featureFlagResultAttr.value'; |
| 24 | +const _featureFlagResultVariationIndex = |
| 25 | + '$_featureFlagResultAttr.variationIndex'; |
| 26 | +const _featureFlagResultReasonAttr = '$_featureFlagResultAttr.reason'; |
| 27 | +const _featureFlagResultReasonInExperimentAttr = |
| 28 | + '$_featureFlagResultReasonAttr.inExperiment'; |
| 29 | + |
| 30 | +final providerNameAttribute = StringableAttribute.fromString( |
| 31 | + _featureFlagProviderNameAttr, |
| 32 | + 'LaunchDarkly', |
| 33 | +); |
| 34 | + |
| 35 | +String? _toJsonValue(LDValue value) { |
| 36 | + try { |
| 37 | + return jsonEncode(value.toDynamic()); |
| 38 | + } catch (_) { |
| 39 | + // All LDValues must be JSON presentable, but this is for safety. |
| 40 | + return null; |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +List<StringableAttribute> _getValueAttributes(LDEvaluationDetail<LDValue> detail) { |
| 45 | + final attributes = <StringableAttribute>[]; |
| 46 | + final jsonValue = _toJsonValue(detail.value); |
| 47 | + if (jsonValue != null) { |
| 48 | + attributes.add( |
| 49 | + StringableAttribute.fromString(_featureFlagResultValueAttr, jsonValue), |
| 50 | + ); |
| 51 | + } |
| 52 | + if (detail.variationIndex != null) { |
| 53 | + attributes.add( |
| 54 | + StringableAttribute.fromInt( |
| 55 | + _featureFlagResultVariationIndex, |
| 56 | + detail.variationIndex!, |
| 57 | + ), |
| 58 | + ); |
| 59 | + } |
| 60 | + if (detail.reason != null && detail.reason!.inExperiment) { |
| 61 | + attributes.add( |
| 62 | + StringableAttribute.fromBoolean( |
| 63 | + _featureFlagResultReasonInExperimentAttr, |
| 64 | + detail.reason!.inExperiment, |
| 65 | + ), |
| 66 | + ); |
| 67 | + } |
| 68 | + return attributes; |
| 69 | +} |
| 70 | + |
| 71 | +/// Class representing the feature flagging semantic convention with |
| 72 | +/// LaunchDarkly additions. |
| 73 | +class FeatureFlagConvention { |
| 74 | + /// Get feature_flag event attributes. |
| 75 | + static List<StringableAttribute> getEventAttributes({ |
| 76 | + required String key, |
| 77 | + required LDEvaluationDetail<LDValue> detail, |
| 78 | + LDContext? context, |
| 79 | + String? environmentId, |
| 80 | + }) { |
| 81 | + List<StringableAttribute> attributes = [ |
| 82 | + providerNameAttribute, |
| 83 | + StringableAttribute.fromString(_featureFlagKeyAttr, key), |
| 84 | + ..._getValueAttributes(detail), |
| 85 | + ]; |
| 86 | + |
| 87 | + if (context != null && context.valid) { |
| 88 | + attributes.add( |
| 89 | + StringableAttribute.fromString(_featureFlagContextIdAttr, context.canonicalKey), |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + if (environmentId != null) { |
| 94 | + attributes.add( |
| 95 | + StringableAttribute.fromString(_featureFlagSetIdAttr, environmentId!), |
| 96 | + ); |
| 97 | + } |
| 98 | + return attributes; |
| 99 | + } |
| 100 | + |
| 101 | + /// The name to use for the event. |
| 102 | + static const eventName = _featureFlagEventName; |
| 103 | +} |
0 commit comments