/// <summary> /// Creates and begins a new measurement. /// </summary> /// <param name="entity">The entity.</param> /// <param name="metric">The metric.</param> public MetricMeasurement(IReactiveResource entity, EntityMetric metric) { _entity = entity; _metric = metric; entity.BeginMetric(metric); }
/// <summary> /// Get all the metrics for a given entity. /// </summary> /// <param name="entity">The entity.</param> /// <returns>The set of metrics that have been collected for that entity.</returns> public static IReadOnlyDictionary <EntityMetric, TimeSpan> GetMetrics(this IReactiveResource entity) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } return(Impl.Instance.GetMetrics(entity)); }
private long[] GetEntityMetrics(IReactiveResource entity) { return(_metrics.GetValue(entity, _ => { var res = new long[s_metricCount]; Array.Copy(s_emptyMetrics, 0, res, 0, s_metricCount); return res; })); }
/// <summary> /// Creates and starts a new measurement. /// </summary> /// <param name="entity">The entity.</param> /// <param name="metric">The metric.</param> /// <returns>A measurement object that should be disposed to end the measurement.</returns> internal static MetricMeasurement Measure(this IReactiveResource entity, EntityMetric metric) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } return(new MetricMeasurement(entity, metric)); }
/// <summary> /// Stops a timer for a given entity and metric, and store the elapsed time. /// </summary> /// <param name="entity">The entity.</param> /// <param name="metric">The metric.</param> internal static void EndMetric(this IReactiveResource entity, EntityMetric metric) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } if (ShouldTrack) { Impl.Instance.EndMetric(entity, metric); } }
private bool OnEntitySaveFailed(Uri uri, IReactiveResource entity, ReactiveEntityKind kind, Exception error) { var entitySaveFailed = Parent.EntitySaveFailed; if (entitySaveFailed != null) { var e = new ReactiveEntitySaveFailedEventArgs(uri, entity, kind, error); entitySaveFailed.InvokeSafe(Parent, e); return(e.Handled); } return(false); }
/// <summary> /// Set a time span for a given entity and metric. /// </summary> /// <param name="entity">The entity.</param> /// <param name="metric">The metric.</param> /// <param name="elapsed">The time span.</param> internal static void SetMetric(this IReactiveResource entity, EntityMetric metric, TimeSpan elapsed) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } if (elapsed < TimeSpan.Zero) { throw new ArgumentOutOfRangeException(nameof(elapsed)); } if (ShouldTrack) { Impl.Instance.SetMetric(entity, metric, elapsed); } }
private void HandleError(ReactiveEntityKind kind, string key, IReactiveResource entity, Exception ex, Action <Exception> unhandled) { if (ex is EngineUnloadedException) { return; } var uri = new Uri(key); var error = new EntitySaveFailedException(uri, kind, ex); if (!_engine.OnEntitySaveFailed(uri, entity, kind, error)) { unhandled(error); } }
private void HandleError(ReactiveEntityKind kind, string key, IReactiveResource entity, Exception ex, Action <Exception> unhandled) { if (ex is EngineUnloadedException) { return; } var uri = new Uri(key); var error = new EntityLoadFailedException(uri, kind, ex); // TODO: support mitigation for any kind of artifact if (!_engine.OnEntityLoadFailed(uri, entity, kind, error, out _)) { unhandled(error); } }
// PERF: Expose the metrics as a struct that contains a reference to the long[] and provides // dictionary-like APIs to reduce allocations. public IReadOnlyDictionary <EntityMetric, TimeSpan> GetMetrics(IReactiveResource entity) { var entityMetrics = GetEntityMetrics(entity); var results = EnumDictionary.Create <EntityMetric, TimeSpan>(); for (var i = 0; i < s_metricCount; ++i) { var metric = entityMetrics[i]; if (metric >= 0) { results[(EntityMetric)i] = new TimeSpan(metric); } } // CONSIDER: Consider whether we need to create a wrapper. return(new ReadOnlyDictionary <EntityMetric, TimeSpan>(results)); }
public void EndMetric(IReactiveResource entity, EntityMetric metric) { var entityMetrics = GetEntityMetrics(entity); var idx = (int)metric; var startingTicks = entityMetrics[idx]; if (startingTicks == EmptyMetric) { // End before start return; } if (startingTicks >= 0) { // The metric was already set. We can't do anything else. return; } entityMetrics[idx] = startingTicks + s_timer.Elapsed.Ticks; }
private bool OnEntityLoadFailed(Uri uri, IReactiveResource entity, ReactiveEntityKind kind, Exception error, out ReactiveEntityRecoveryFailureMitigation mitigation) { mitigation = ReactiveEntityRecoveryFailureMitigation.Ignore; if (error is MitigationBailOutException) { return(true); } var entityLoadFailed = Parent.EntityLoadFailed; if (entityLoadFailed != null) { var e = new ReactiveEntityLoadFailedEventArgs(uri, entity, kind, error); entityLoadFailed.InvokeSafe(Parent, e); mitigation = e.Mitigation; return(e.Handled); } return(false); }
/// <summary> /// Creates a new instance of <see cref="SubscriptionFactoryEventArgs"/> class for the specified entity. /// </summary> /// <param name="entity">The entity representing the subscription factory.</param> public SubscriptionFactoryEventArgs(IReactiveResource entity) : base(entity.Uri, entity, ReactiveEntityKind.SubscriptionFactory) { }
public void SetMetric(IReactiveResource entity, EntityMetric metric, TimeSpan elapsed) { GetEntityMetrics(entity)[(int)metric] = elapsed.Ticks; }
public void BeginMetric(IReactiveResource entity, EntityMetric metric) { var entityMetrics = GetEntityMetrics(entity); Interlocked.Exchange(ref entityMetrics[(int)metric], -1 * s_timer.Elapsed.Ticks); }
/// <summary> /// Creates a new instance of the <see cref="ReactiveEntityLoadFailedEventArgs"/> class for the specified entity. /// </summary> /// <param name="uri">URI of the reactive entity.</param> /// <param name="entity">Reactive entity.</param> /// <param name="entityType">Kind of the reactive entity.</param> /// <param name="error">The error that occured during load.</param> public ReactiveEntityLoadFailedEventArgs(Uri uri, IReactiveResource entity, ReactiveEntityKind entityType, Exception error) : base(uri, entity, entityType) { Error = error; }
private static void End(IReactiveResource entity, EntityMetric metric) { EntityMetricsTracker.Impl.Instance.EndMetric(entity, metric); }
/// <summary> /// Creates a new instance of the <see cref="ReactiveEntityEventArgs"/> class for the specified entity. /// </summary> /// <param name="uri">URI of the reactive entity.</param> /// <param name="entity">Reactive entity.</param> /// <param name="entityType">Kind of the reactive entity.</param> protected ReactiveEntityEventArgs(Uri uri, IReactiveResource entity, ReactiveEntityKind entityType) { Uri = uri; Entity = entity; EntityType = entityType; }
private static IReadOnlyDictionary <EntityMetric, TimeSpan> Get(IReactiveResource entity) { return(EntityMetricsTracker.Impl.Instance.GetMetrics(entity)); }
private static void Set(IReactiveResource entity, EntityMetric metric, TimeSpan elapsed) { EntityMetricsTracker.Impl.Instance.SetMetric(entity, metric, elapsed); }
/// <summary> /// Creates a new instance of <see cref="ObserverEventArgs"/> class for the specified entity. /// </summary> /// <param name="entity">The entity representing the observer.</param> public ObserverEventArgs(IReactiveResource entity) : base(entity.Uri, entity, ReactiveEntityKind.Observer) { }
/// <summary> /// Creates a new instance of <see cref="StreamFactoryEventArgs"/> class for the specified entity. /// </summary> /// <param name="entity">The entity representing the stream factory.</param> public StreamFactoryEventArgs(IReactiveResource entity) : base(entity.Uri, entity, ReactiveEntityKind.StreamFactory) { }