Beispiel #1
0
 internal MetricGroup(MetricsCollector collector, string name, bool withoutPrefix, string unit, string description, Func <T1, TMetric> metricFactory = null)
 {
     _collector     = collector;
     Name           = name;
     WithoutPrefix  = withoutPrefix;
     Unit           = unit;
     Description    = description;
     _metricFactory = metricFactory ?? GetDefaultFactory();
 }
 /// <inheritdoc/>
 public Task StopAsync(CancellationToken cancellationToken)
 {
     _collector?.Shutdown();
     _collector = null;
     return(Task.CompletedTask);
 }
Beispiel #3
0
        /// <summary>
        /// Dumps the state of a <see cref="MetricsCollector" />.
        /// </summary>
        public async static Task DumpAsync(this MetricsCollector collector, TextWriter textWriter, string[] excludedTags = null)
        {
            excludedTags ??= Array.Empty <string>();
            await textWriter.WriteLineAsync("DefaultTags:");

            var defaultTags = collector.DefaultTags;

            if (defaultTags.Count > 0)
            {
                foreach (var t in defaultTags)
                {
                    await textWriter.WriteAsync("  ");

                    await textWriter.WriteAsync(t.Key);

                    await textWriter.WriteAsync(" = ");

                    await textWriter.WriteAsync(t.Value);

                    await textWriter.WriteLineAsync(string.Empty);
                }
            }

            await textWriter.WriteLineAsync("----");

            await textWriter.WriteLineAsync("Endpoints:");

            var endpoints = collector.Endpoints;

            foreach (var endpoint in endpoints)
            {
                await textWriter.WriteAsync("  ");

                await textWriter.WriteAsync(endpoint.Name);

                await textWriter.WriteAsync(": ");

                switch (endpoint.Handler)
                {
                case BosunMetricHandler bosunHandler:
                    await textWriter.WriteLineAsync(bosunHandler.BaseUri?.AbsoluteUri ?? "null");

                    break;

                case SignalFxMetricHandler signalFxHandler:
                    await textWriter.WriteLineAsync(signalFxHandler.BaseUri?.AbsoluteUri ?? "null");

                    break;

                case LocalMetricHandler h:
                    await textWriter.WriteLineAsync("Local");

                    break;

                default:
                    await textWriter.WriteLineAsync("Unknown");

                    break;
                }
            }

            await textWriter.WriteLineAsync("----");

            await textWriter.WriteLineAsync("Counters:");

            var count        = 0;
            var localHandler = endpoints.Select(x => x.Handler).OfType <LocalMetricHandler>().FirstOrDefault();

            if (localHandler != null)
            {
                foreach (var reading in localHandler.GetReadings(reset: false).OrderBy(r => r.NameWithSuffix))
                {
                    await textWriter.WriteAsync("Name = ");

                    await textWriter.WriteAsync(reading.Name);

                    await textWriter.WriteAsync(reading.Suffix);

                    await textWriter.WriteAsync(", Value = ");

                    await textWriter.WriteAsync(reading.Value.ToString());

                    await textWriter.WriteAsync(", Type = ");

                    await textWriter.WriteAsync(reading.Type.ToString());

                    await textWriter.WriteAsync(", Tags = [");

                    var i = 0;
                    foreach (var tag in reading.Tags)
                    {
                        // Skip excluded tags
                        if (excludedTags.Contains(tag.Key) || collector.DefaultTags.ContainsKey(tag.Key))
                        {
                            i++;
                            continue;
                        }

                        await textWriter.WriteAsync(tag.Key);

                        await textWriter.WriteAsync(" = ");

                        await textWriter.WriteAsync(tag.Value);

                        if (i++ < reading.Tags.Count - 1)
                        {
                            await textWriter.WriteAsync(", ");
                        }
                    }

                    await textWriter.WriteLineAsync("]");

                    count++;
                }
            }

            if (count == 0)
            {
                await textWriter.WriteLineAsync("No readings found yet - this is normal immediately after startup, GIVE IT A MINUTE.");
            }
        }
 /// <inheritdoc/>
 public Task StartAsync(CancellationToken cancellationToken)
 {
     _collector = new MetricsCollector(_options.Value);
     return(Task.CompletedTask);
 }