createOTelAttributes static method
Creates OpenTelemetry-compatible attributes for a feature flag evaluation
These attributes follow the OpenTelemetry semantic conventions for feature flags. They can be used with any OpenTelemetry-compatible telemetry system.
Implementation
static OTelAttributes createOTelAttributes({
required String flagKey,
required dynamic value,
String? providerName,
String? variant,
String reason = OTelFeatureFlagConstants.REASON_DEFAULT,
Map<String, dynamic>? evaluationContext,
}) {
final attributes = <OTelAttribute>[];
// Add common attributes
attributes.add(OTelAttribute(OTelFeatureFlagConstants.FLAG_KEY, flagKey));
attributes.add(
OTelAttribute(OTelFeatureFlagConstants.FLAG_EVALUATED, true),
);
if (providerName != null && providerName.isNotEmpty) {
attributes.add(
OTelAttribute(
OTelFeatureFlagConstants.FLAG_PROVIDER_NAME,
providerName,
),
);
}
if (variant != null && variant.isNotEmpty) {
attributes.add(
OTelAttribute(OTelFeatureFlagConstants.FLAG_VARIANT, variant),
);
}
attributes.add(OTelAttribute(OTelFeatureFlagConstants.REASON, reason));
// Add value and determine type
if (value != null) {
if (value is bool) {
attributes.add(
OTelAttribute(
OTelFeatureFlagConstants.FLAG_VALUE_TYPE,
OTelFeatureFlagConstants.TYPE_BOOLEAN,
),
);
attributes.add(
OTelAttribute(OTelFeatureFlagConstants.FLAG_VALUE_BOOLEAN, value),
);
} else if (value is String) {
attributes.add(
OTelAttribute(
OTelFeatureFlagConstants.FLAG_VALUE_TYPE,
OTelFeatureFlagConstants.TYPE_STRING,
),
);
attributes.add(
OTelAttribute(OTelFeatureFlagConstants.FLAG_VALUE_STRING, value),
);
} else if (value is int) {
attributes.add(
OTelAttribute(
OTelFeatureFlagConstants.FLAG_VALUE_TYPE,
OTelFeatureFlagConstants.TYPE_INT,
),
);
attributes.add(
OTelAttribute(OTelFeatureFlagConstants.FLAG_VALUE_INT, value),
);
} else if (value is double) {
attributes.add(
OTelAttribute(
OTelFeatureFlagConstants.FLAG_VALUE_TYPE,
OTelFeatureFlagConstants.TYPE_DOUBLE,
),
);
attributes.add(
OTelAttribute(OTelFeatureFlagConstants.FLAG_VALUE_FLOAT, value),
);
} else {
attributes.add(
OTelAttribute(
OTelFeatureFlagConstants.FLAG_VALUE_TYPE,
OTelFeatureFlagConstants.TYPE_OBJECT,
),
);
// For non-primitive types, we convert to a string representation
attributes.add(
OTelAttribute(
OTelFeatureFlagConstants.FLAG_VALUE_STRING,
value.toString(),
),
);
}
}
return OTelAttributes(attributes);
}