public MetricPusher(string endpoint, string job, string instance = null, long intervalMilliseconds = 1000, IEnumerable <Tuple <string, string> > additionalLabels = null, CollectorRegistry registry = null) : base(registry)
        {
            if (string.IsNullOrEmpty(endpoint))
            {
                throw new ArgumentNullException("endpoint");
            }
            if (string.IsNullOrEmpty(job))
            {
                throw new ArgumentNullException("job");
            }
            if (intervalMilliseconds <= 0)
            {
                throw new ArgumentException("Interval must be greater than zero", "intervalMilliseconds");
            }

            StringBuilder sb = new StringBuilder(string.Format("{0}/job/{1}", endpoint.TrimEnd('/'), job));

            if (!string.IsNullOrEmpty(instance))
            {
                sb.AppendFormat("/instance/{0}", instance);
            }

            if (additionalLabels != null)
            {
                foreach (var pair in additionalLabels)
                {
                    if (pair == null || string.IsNullOrEmpty(pair.Item1) || string.IsNullOrEmpty(pair.Item2))
                    {
                        throw new NotSupportedException($"Invalid {nameof(MetricPusher)} additional label: ({pair?.Item1}):({pair?.Item2})");
                    }

                    sb.AppendFormat("/{0}/{1}", pair.Item1, pair.Item2);
                }
            }

            if (!Uri.TryCreate(sb.ToString(), UriKind.Absolute, out _targetUrl))
            {
                throw new ArgumentException("Endpoint must be a valid url", "endpoint");
            }

            _pushInterval = TimeSpan.FromMilliseconds(intervalMilliseconds);
        }
Beispiel #2
0
 protected MetricHandler(CollectorRegistry registry = null)
 {
     _registry = registry ?? Metrics.DefaultRegistry;
 }
 public Handler(CollectorRegistry registry)
 {
     _registry = registry;
 }
Beispiel #4
0
        public MetricServer(string hostname, int port, string url = "metrics/", CollectorRegistry registry = null, bool useHttps = false) : base(registry)
        {
            var s = useHttps ? "s" : "";

            _httpListener.Prefixes.Add($"http{s}://{hostname}:{port}/{url}");
        }
Beispiel #5
0
 internal MetricFactory(CollectorRegistry registry)
 {
     _registry = registry ?? throw new ArgumentNullException(nameof(registry));
 }
Beispiel #6
0
 public MetricServer(int port, string url = "metrics/", CollectorRegistry registry = null, bool useHttps = false) : this("+", port, url, registry, useHttps)
 {
 }
Beispiel #7
0
        /// <summary>
        /// Registers the .NET metrics in the specified registry.
        /// </summary>
        public static void Register(CollectorRegistry registry)
        {
            var instance = new DotNetStats(registry);

            registry.AddBeforeCollectCallback(instance.UpdateMetrics);
        }
Beispiel #8
0
        public KestrelMetricServer(string hostname, int port, string url = "/metrics", CollectorRegistry registry = null, X509Certificate2 certificate = null) : base(registry)
        {
            _hostname = hostname;
            _port     = port;
            _url      = url;

            _certificate = certificate;
        }
Beispiel #9
0
 public KestrelMetricServer(int port, string url = "/metrics", CollectorRegistry registry = null, X509Certificate2 certificate = null) : this("+", port, url, registry, certificate)
 {
 }
Beispiel #10
0
        /// <summary>
        /// Starts a Prometheus metrics exporter. The default URL is /metrics, which is a Prometheus convention.
        /// Use static methods on the <see cref="Metrics"/> class to create your metrics.
        /// </summary>
        public static IApplicationBuilder UseMetricServer(this IApplicationBuilder builder, string url = "/metrics", CollectorRegistry registry = null)
        {
            // If there is a URL to map, map it and re-enter without the URL.
            if (url != null)
            {
                return(builder.Map(url, builder2 => builder2.UseMetricServer(null, registry)));
            }

            return(builder.UseMiddleware <MetricServerMiddleware>(new MetricServerMiddleware.Settings
            {
                Registry = registry
            }));
        }