Exemple #1
0
 public IActionResult Get2()
 {
     _histogram.Observe(1); // No Crash
     _histogram.WithLabels("test1", "value1").Observe(1);
     _histogram.WithLabels("test2", "value2").Observe(2);
     return(Ok());
 }
    // Use metrics
    private void MagicFunc(InputMessage input)
    {
        if (input.FancyText == "")
        {
            _counter?.Inc();
        }

        _simpleSummary?.Observe(input.FancyNumber);

        var success = false;

        /*
         * With AutoObserveStopwatch, the metrics are still collected,
         * even if an exception is thrown within the using scope.
         * For histograms, AutoObserve can be used as a more generic approach.
         *
         * When using the Rider IDE, you might see a warning here, regarding possible
         * modifications of the variable `success` in the outer scope. Because this is
         * exactly what we want to achieve here, you can simply ignore this warning.
         * Applying the fix suggested by Rider would even break the functionality.
         */
        using (new AutoObserveStopwatch(() => _labeledSummary?.WithLabels(success.ToString())))
        {
            success = LongRunningDangerousCheck(input);
        }

        _serviceInDifferentNamespace.CountInDifferentNamespace();
    }
    private void WriteStatistics(string json)
    {
        var partitionConsumerLags = JsonSerializer
                                    .Deserialize <KafkaStatistics>(json)?
                                    .Topics?
                                    .Select(t => t.Value)
                                    .SelectMany(t => t.Partitions ?? new Dictionary <string, KafkaStatisticsPartition>())
                                    .Select(t => (Parition: t.Key.ToString(), t.Value.ConsumerLag));

        if (partitionConsumerLags is null)
        {
            return;
        }

        foreach (var(partition, consumerLag) in partitionConsumerLags)
        {
            var lag = consumerLag;
            if (lag == -1)
            {
                lag = 0;
            }

            _consumerLagSummary?.WithLabels(_options.Topic, partition)?.Observe(lag);
            _consumerLagGauge?.WithLabels(_options.Topic, partition)?.Set(lag);
        }
    }
        /// <inheritdoc />
        public Task PublishAsync(HealthReport report, CancellationToken cancellationToken)
        {
            foreach (var entry in report.Entries)
            {
                _status.WithLabels(entry.Key).Set((double)entry.Value.Status);
                _duration.WithLabels(entry.Key).Set(entry.Value.Duration.TotalSeconds);
            }

            return(Task.CompletedTask);
        }
            public Task <MotorCloudEvent <string> > ConvertMessageAsync(MotorCloudEvent <string> dataCloudEvent,
                                                                        CancellationToken token = default)
            {
                _logger.LogInformation("log your request");
                var tmpChar  = dataCloudEvent.TypedData.ToCharArray();
                var reversed = tmpChar.Reverse().ToArray();

                _summary.WithLabels("collect_your_metrics").Observe(1.0);
                return(Task.FromResult(dataCloudEvent.CreateNew(new string(reversed))));
            }
    public override async Task<ProcessedMessageStatus> HandleMessageAsync(MotorCloudEvent<TInput> dataCloudEvent,
        CancellationToken token = default)
    {
        var processedMessageStatus = ProcessedMessageStatus.CriticalFailure;
        using (new AutoIncCounter(() => _messageProcessingTotal.WithLabels(processedMessageStatus.ToString())))
        {
            processedMessageStatus = await base.HandleMessageAsync(dataCloudEvent, token);
        }

        return processedMessageStatus;
    }
        public override async Task <ProcessedMessageStatus> HandleMessageAsync(MotorCloudEvent <TInput> dataCloudEvent,
                                                                               CancellationToken token = default)
        {
            var processedMessageStatus = ProcessedMessageStatus.CriticalFailure;

            try
            {
                processedMessageStatus = await base.HandleMessageAsync(dataCloudEvent, token);
            }
            finally
            {
                _messageProcessingTotal.WithLabels(processedMessageStatus.ToString()).Inc();
            }

            return(processedMessageStatus);
        }
        protected override async Task <HttpResponseMessage> SendAsync(
            HttpRequestMessage request,
            CancellationToken token)
        {
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            var response = await base.SendAsync(request, token);

            var uri = request.RequestUri;

            if (uri == null)
            {
                return(response);
            }
            _requestTotal.WithLabels(uri.Host, response.StatusCode.ToString()).Inc();
            _requestLatency.WithLabels(uri.Host).Observe(stopwatch.ElapsedMilliseconds);
            return(response);
        }
        private void WriteMetrics(string statusCode, string method, string controller, string action, string path, double elapsedSeconds)
        {
            // Order is important

            var labelValues = new List <string>();

            if (_options.IncludeStatusCode)
            {
                labelValues.Add(statusCode);
            }

            if (_options.IncludeMethod)
            {
                labelValues.Add(method);
            }

#if HasRoutes
            if (_options.IncludeController)
            {
                labelValues.Add(controller);
            }

            if (_options.IncludeAction)
            {
                labelValues.Add(action);
            }
#endif

            if (_options.IncludePath)
            {
                labelValues.Add(path);
            }

            if (_options.CustomLabels != null)
            {
                foreach (var customLabel in _options.CustomLabels)
                {
                    labelValues.Add(customLabel.Value());
                }
            }

            _histogram.WithLabels(labelValues.ToArray()).Observe(elapsedSeconds);
        }
 public IGauge LabelledCreation()
 {
     return(_gauge.WithLabels("test label"));
 }
 public ICounter LabelledCreation()
 {
     return(_counter.WithLabels("test label1", "test label2"));
 }
Exemple #12
0
 public IActionResult Get()
 {
     _histogram.WithLabels("test1").Observe(1);
     _histogram.WithLabels("test2").Observe(2);
     return(Ok());
 }
 public IHistogram LabelledCreation()
 {
     return(_histogram.WithLabels("test label"));
 }
 public ISummary LabelledCreation()
 {
     return(_summary.WithLabels("test label"));
 }
Exemple #15
0
 public static TMetric Labels <TMetric>(this IMetricFamily <TMetric, ValueTuple <string> > metricFamily, string label)
     where TMetric : IMetric
 {
     return(metricFamily.WithLabels(ValueTuple.Create(label)));
 }
Exemple #16
0
 private void Report(int statusCode, string method, string path, TimeSpan value)
 {
     _histogram.WithLabels(statusCode.ToString(), method, string.IsNullOrWhiteSpace(path) ? "/" : path)
     .Observe(value.TotalSeconds);
 }