Ejemplo n.º 1
0
        public static async Task <T?> Measure <T>(
            Func <Task <T> > action,
            IHistogramMetric metric,
            Func <T, int> getCount,
            ICountMetric?errorCount = null,
            string[]?labels         = null
            ) where T : class
        {
            var stopwatch = Stopwatch.StartNew();

            T?result = null;

            try {
                result = await action().ConfigureAwait(false);
            }
            catch (Exception) {
                errorCount?.Inc(labels);

                throw;
            }
            finally {
                stopwatch.Stop();
                var count = result != null?getCount(result) : 0;

                metric.Observe(stopwatch, labels, count);
            }

            return(result);
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="action">Actual operation to measure</param>
        /// <param name="metric">Histogram metrics, which will observe the measurement</param>
        /// <param name="getCount">A function, which is able to get the custom count from the function return</param>
        /// <param name="errorCount">Optional: counter metric to count errors</param>
        /// <param name="labels">Labels for the histogram and errors counter metrics</param>
        /// <typeparam name="T">The observed function result</typeparam>
        /// <returns></returns>
        public static async Task <T?> Measure <T>(
            Func <Task <T> > action,
            IHistogramMetric metric,
            Func <T, int> getCount,
            ICountMetric?errorCount = null,
            string[]?labels         = null
            ) where T : class
        {
            var stopwatch = Stopwatch.StartNew();

            T?result = null;

            try {
                var task = action();
                result = task.IsCompleted ? task.Result : await action();
            }
            catch (Exception) {
                errorCount?.Inc(labels: labels.ValueOrEmpty());

                throw;
            }
            finally {
                stopwatch.Stop();
                metric.Observe(stopwatch, labels, result != null ? getCount(result) : 0);
            }

            return(result);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Helpful function to measure the execution time of a given asynchronous function,
        /// and returns the observed function result.
        /// </summary>
        /// <param name="action">Actual operation to measure</param>
        /// <param name="metric">Histogram metrics, which will observe the measurement</param>
        /// <param name="errorCount">Optional: counter metric to count errors</param>
        /// <param name="labels">Labels for the histogram and errors counter metrics</param>
        /// <param name="count">Optional: count, one by default</param>
        /// <returns></returns>
        public static async Task <T> Measure <T>(
            Func <ValueTask <T> > action,
            IHistogramMetric metric,
            ICountMetric?errorCount = null,
            string[]?labels         = null,
            int count = 1
            )
        {
            var stopwatch = Stopwatch.StartNew();

            T result;

            try {
                var task = action();
                result = task.IsCompleted ? task.Result : await action();
            }
            catch (Exception) {
                errorCount?.Inc(labels: labels.ValueOrEmpty());

                throw;
            }
            finally {
                stopwatch.Stop();
                metric.Observe(stopwatch, labels, count);
            }

            return(result);
        }
        public static async Task Observe(this IHistogramMetric histogram, Func <Task> factory, params string[] labels)
        {
            var   sw = Stopwatch.StartNew();
            var   t  = factory();
            await t;

            sw.Stop();
            histogram.Observe(sw, labels);
        }
Ejemplo n.º 5
0
        public ServerGroup(string name, ServerVisibility visibility, DarkRiftThreadHelper threadHelper, Logger logger, MetricsCollector metricsCollector)
        {
            this.Name       = name;
            this.Visibility = visibility;

            this.threadHelper = threadHelper;
            this.logger       = logger;

            serversConnectedGauge            = metricsCollector.Gauge("remote_servers_connected", "The number of servers connected to the server in this group.", "group").WithTags(name);
            serverJoinedEventTimeHistogram   = metricsCollector.Histogram("remote_server_joined_event_time", "The time taken to execute the ServerJoined event.", "group").WithTags(name);
            serverLeftEventTimeHistogram     = metricsCollector.Histogram("remote_server_left_event_time", "The time taken to execute the ServerLeft event.", "group").WithTags(name);
            serverJoinedEventFailuresCounter = metricsCollector.Counter("remote_server_joined_event_failures", "The number of failures executing the ServerJoined event.", "group").WithTags(name);
            serverLeftEventFailuresCounter   = metricsCollector.Counter("remote_server_left_event_failures", "The number of failures executing the ServerLeft event.", "group").WithTags(name);
        }
        /// <summary>
        ///     Creates a new remote server.
        /// </summary>
        /// <param name="id">The ID of the server.</param>
        /// <param name="host">The host connected to.</param>
        /// <param name="port">The port connected to.</param>
        /// <param name="group">The group the server belongs to.</param>
        /// <param name="threadHelper">The thread helper to use.</param>
        /// <param name="logger">The logger to use.</param>
        /// <param name="metricsCollector">The metrics collector to use.</param>
        internal DownstreamRemoteServer(ushort id, string host, ushort port, DownstreamServerGroup group, DarkRiftThreadHelper threadHelper, Logger logger, MetricsCollector metricsCollector)
        {
            this.ID           = id;
            this.Host         = host;
            this.Port         = port;
            this.serverGroup  = group;
            this.threadHelper = threadHelper;
            this.logger       = logger;

            messagesSentCounter                    = metricsCollector.Counter("messages_sent", "The number of messages sent to remote servers.");
            messagesReceivedCounter                = metricsCollector.Counter("messages_received", "The number of messages received from remote servers.");
            messageReceivedEventTimeHistogram      = metricsCollector.Histogram("message_received_event_time", "The time taken to execute the MessageReceived event.");
            messageReceivedEventFailuresCounter    = metricsCollector.Counter("message_received_event_failures", "The number of failures executing the MessageReceived event.");
            serverConnectedEventTimeHistogram      = metricsCollector.Histogram("remote_server_connected_event_time", "The time taken to execute the ServerConnected event.", "group").WithTags(group.Name);
            serverDisconnectedEventTimeHistogram   = metricsCollector.Histogram("remote_server_disconnected_event_time", "The time taken to execute the ServerDisconnected event.", "group").WithTags(group.Name);
            serverConnectedEventFailuresCounter    = metricsCollector.Counter("remote_server_connected_event_failures", "The number of failures executing the ServerConnected event.", "group").WithTags(group.Name);
            serverDisconnectedEventFailuresCounter = metricsCollector.Counter("remote_server_disconnected_event_failures", "The number of failures executing the ServerDisconnected event.", "group").WithTags(group.Name);
        }
Ejemplo n.º 7
0
        /// <summary>
        ///     Creates a new client manager.
        /// </summary>
        /// <param name="settings">The settings for this client manager.</param>
        /// <param name="networkListenerManager">The server's network listener manager.</param>
        /// <param name="threadHelper">The thread helper the client manager will use.</param>
        /// <param name="logger">The logger this client manager will use.</param>
        /// <param name="clientLogger">The logger clients will use.</param>
        internal ClientManager(ServerSpawnData.ServerSettings settings, NetworkListenerManager networkListenerManager, DarkRiftThreadHelper threadHelper, Logger logger, Logger clientLogger)
#endif
        {
            this.MaxStrikes             = settings.MaxStrikes;
            this.networkListenerManager = networkListenerManager;
            this.threadHelper           = threadHelper;
            this.logger       = logger;
            this.clientLogger = clientLogger;
#if PRO
            this.clientMetricsCollector = clientMetricsCollector;

            clientsConnectedGauge                  = metricsCollector.Gauge("clients_connected", "The number of clients connected to the server.");
            clientConnectedEventTimeHistogram      = metricsCollector.Histogram("client_connected_event_time", "The time taken to execute the ClientConnected event.");
            clientDisconnectedEventTimeHistogram   = metricsCollector.Histogram("client_disconnected_event_time", "The time taken to execute the ClientDisconnected event.");
            clientConnectedEventFailuresCounter    = metricsCollector.Counter("client_connected_event_failures", "The number of failures executing the ClientConnected event.");
            clientDisconnectedEventFailuresCounter = metricsCollector.Counter("client_disconnected_event_failures", "The number of failures executing the ClientDisconnected event.");
#endif
        }
        public readonly IHistogramMetric ThreadPoolLatencyHistogram; //done

        public ActorMetrics(ProtoMetrics metrics)
        {
            ThreadPoolLatencyHistogram = metrics.CreateHistogram("protoactor_threadpool_latency_duration_seconds", "", "id", "address");
            DeadletterCount            = metrics.CreateCount("protoactor_deadletter_count", "", "id", "address", "messagetype");
            ActorSpawnCount            = metrics.CreateCount("protoactor_actor_spawn_count", "", "id", "address", "actortype");
            ActorStoppedCount          = metrics.CreateCount("protoactor_actor_stopped_count", "", "id", "address", "actortype");
            ActorRestartedCount        = metrics.CreateCount("protoactor_actor_restarted_count", "", "id", "address", "actortype");
            ActorFailureCount          = metrics.CreateCount("protoactor_actor_failure_count", "", "id", "address", "actortype");

            ActorMailboxLength = metrics.CreateGauge("protoactor_actor_mailbox_length", "", "id", "address", "actortype");

            ActorMessageReceiveHistogram = metrics.CreateHistogram("protoactor_actor_messagereceive_duration_seconds", "", "id", "address",
                                                                   "actortype", "messagetype"
                                                                   );
            FuturesStartedCount   = metrics.CreateCount("protoactor_future_started_count", "", "id", "address");
            FuturesTimedOutCount  = metrics.CreateCount("protoactor_future_timedout_count", "", "id", "address");
            FuturesCompletedCount = metrics.CreateCount("protoactor_future_completed_count", "", "id", "address");
        }
        public ClusterMetrics(ProtoMetrics metrics)
        {
            ClusterActorGauge = metrics.CreateGauge("protocluster_virtualactors", "", "id", "address", "clusterkind");

            ClusterActorSpawnHistogram =
                metrics.CreateHistogram("protocluster_virtualactor_spawn_duration_seconds", "", "id", "address", "clusterkind");

            ClusterRequestHistogram = metrics.CreateHistogram("protocluster_virtualactor_requestasync_duration_seconds", "", "id", "address",
                                                              "clusterkind", "messagetype", "pidsource"
                                                              );

            ClusterRequestRetryCount = metrics.CreateCount("protocluster_virtualactor_requestasync_retry_count", "", "id", "address", "clusterkind",
                                                           "messagetype"
                                                           );

            ClusterTopologyEventGauge = metrics.CreateGauge("protocluster_topology_events", "", "id", "address", "membershiphashcode");

            ClusterResolvePidHistogram =
                metrics.CreateHistogram("protocluster_resolve_pid_duration_seconds", "", "id", "address", "clusterkind");
        }
Ejemplo n.º 10
0
        /// <summary>
        /// Helpful function to measure the execution time of a given asynchronous function
        /// </summary>
        /// <param name="action">Actual operation to measure</param>
        /// <param name="metric">Histogram metrics, which will observe the measurement</param>
        /// <param name="errorCount">Optional: counter metric to count errors</param>
        /// <param name="labels">Labels for the histogram and errors counter metrics</param>
        /// <param name="count">Optional: count, one by default</param>
        /// <returns></returns>
        public static async Task Measure(
            Func <Task> action,
            IHistogramMetric metric,
            ICountMetric?errorCount = null,
            string[]?labels         = null,
            int count = 1
            )
        {
            var stopwatch = Stopwatch.StartNew();

            try {
                await action();
            }
            catch (Exception) {
                errorCount?.Inc(labels: labels.ValueOrEmpty());

                throw;
            }
            finally {
                stopwatch.Stop();
                metric.Observe(stopwatch, labels, count);
            }
        }
Ejemplo n.º 11
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="TimerMetric" /> class.
 /// </summary>
 /// <param name="histogram">The histogram implementation to use.</param>
 /// <param name="clock">The clock to use to measure processing duration.</param>
 public TimerMetric(IHistogramMetric histogram, IClock clock)
     : this(histogram, new MeterMetric(clock), clock)
 {
 }
Ejemplo n.º 12
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="DefaultTimerMetric" /> class.
 /// </summary>
 /// <param name="histogram">The histogram implementation to use.</param>
 /// <param name="meter">The meter implementation to use to genreate the rate of events over time.</param>
 /// <param name="clock">The clock to use to measure processing duration.</param>
 public DefaultTimerMetric(IHistogramMetric histogram, IMeterMetric meter, IClock clock)
 {
     _clock     = clock;
     _meter     = meter;
     _histogram = histogram;
 }
Ejemplo n.º 13
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="DefaultTimerMetric" /> class.
 /// </summary>
 /// <param name="reservoir">The reservoir implementation to use for sampling values to generate the histogram.</param>
 /// <param name="clock">The clock to use to measure processing duration.</param>
 /// <param name="meterTickScheduler">The scheduler used to tick the associated meter.</param>
 internal DefaultTimerMetric(IReservoir reservoir, IClock clock, IMeterTickerScheduler meterTickScheduler)
 {
     _clock     = clock;
     _histogram = new DefaultHistogramMetric(reservoir);
     _meter     = new DefaultMeterMetric(clock, meterTickScheduler);
 }
Ejemplo n.º 14
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="DefaultTimerMetric" /> class.
 /// </summary>
 /// <param name="reservoir">The reservoir implementation to use for sampling values to generate the histogram.</param>
 /// <param name="clock">The clock to use to measure processing duration.</param>
 public DefaultTimerMetric(IReservoir reservoir, IClock clock)
 {
     _clock     = clock;
     _histogram = new DefaultHistogramMetric(reservoir);
     _meter     = new DefaultMeterMetric(clock);
 }
Ejemplo n.º 15
0
 /// <summary>
 ///     Initializes a new instance of the <see cref="DefaultTimerMetric" /> class.
 /// </summary>
 /// <param name="histogram">The histogram implementation to use.</param>
 /// <param name="clock">The clock to use to measure processing duration.</param>
 public DefaultTimerMetric(IHistogramMetric histogram, IClock clock)
 {
     _clock     = clock;
     _histogram = histogram;
     _meter     = new DefaultMeterMetric(clock);
 }
 /// <summary>
 ///     Initializes a new instance of the <see cref="DefaultTimerMetric" /> class.
 /// </summary>
 /// <param name="histogram">The histogram implementation to use.</param>
 /// <param name="clock">The clock to use to measure processing duration.</param>
 public DefaultTimerMetric(IHistogramMetric histogram, IClock clock)
     : this(histogram, new DefaultMeterMetric(clock), clock)
 {
 }
Ejemplo n.º 17
0
 /// <inheritdoc />
 public ITimerMetric Build(IHistogramMetric histogram, IMeterMetric meter, IClock clock)
 {
     return(new DefaultTimerMetric(histogram, meter, clock));
 }
 public static ITimerMetric BuildTimer(this IAdvancedMetrics context, TimerOptions options, IHistogramMetric histogram)
 {
     return(new TimerMetric(histogram, context.Clock));
 }