setGauge method
Set a gauge metric value
Implementation
Future<void> setGauge(
String key,
num value,
String fbl,
String operation,
String view,
String state, {
int? date,
}) async {
final hub = _hub;
if (!_isInitialized || hub == null) {
ObslyLogger.warn('MetricsController not initialized');
return;
}
// Check if metrics is enabled
final config = ConfigController.instance.config;
if (!(config?.enableMetrics ?? true)) {
ObslyLogger.debug('Metrics disabled, ignoring setGauge call');
return;
}
try {
final currentView = NavigationIntegrationV2.isNavigationAvailable
? NavigationIntegrationV2.getCurrentViewName()
: view;
// Update internal state
final metricKey = _createMetricKey(key, fbl, operation, currentView);
final currentState = MetricState(
key: key,
type: MetricType.gauge,
value: value,
lastUpdated: DateTime.now(),
);
_metricStates[metricKey] = currentState;
// Create dimension with user-provided values (empty values will use session fallbacks)
final dimensions = Dimension(
fbl: fbl.isNotEmpty ? fbl : '', // Pass empty if not provided, Hub will use session fallback
operation: operation.isNotEmpty ? operation : '', // Pass empty if not provided, Hub will use session fallback
view: currentView.isNotEmpty ? currentView : '', // Pass empty if not provided, Hub will use session fallback
state: state,
app: '', // Will be filled by Hub with app info
platform: '', // Will be filled by Hub with app info
version: '', // Will be filled by Hub with app info
);
final event = MetricEventBase(
key: key,
value: value,
dimensions: dimensions,
metricType: MetricType.gauge.value,
);
final reservation = hub.reserveEventMetadata();
hub.captureEvent(event, reservation);
ObslyLogger.debug('📊 Gauge set: $key = $value');
} catch (e) {
ObslyLogger.error('Error setting gauge: $e');
}
}