opentracing / opentracing-csharp

OpenTracing API for C# (.NET). 🛑 This library is DEPRECATED! https://github.com/opentracing/specification/issues/163
http://opentracing.io
Apache License 2.0
516 stars 73 forks source link

Metrics ? #103

Closed jrouaix closed 6 years ago

jrouaix commented 6 years ago

Hello.

Starting to use https://github.com/opentracing/opentracing-csharp & https://github.com/jaegertracing/jaeger-client-csharp

I plugged a MetricsFactory just to realize it was tracing tracer internal metrics.

Do you have some plan to implement an easy way to collect user metrics from the OpenTracing stack ? Some decorators around ITracer/ISpan/IScope ? Some composition ? Some middleware of a kind ? Should it be the Tracer implementation responsability ?

Below the ducktape code I used :

using App.Metrics;
using Jaeger.Metrics;
using System.Collections.Generic;
using System.Linq;
using AM = App.Metrics;

namespace XXXXXXX
{
    class AppMetricsMetricsFactory : IMetricsFactory
    {
        private readonly IMetricsRoot _metrics;

        public AppMetricsMetricsFactory(IMetricsRoot metrics)
        {
            _metrics = metrics;
        }

        private static MetricTags GetTags(IDictionary<string, string> tags) => new MetricTags(tags.Keys.ToArray(), tags.Values.ToArray());

        public ICounter CreateCounter(string name, Dictionary<string, string> tags)
        {
            var t = GetTags(tags);
            var meter = new AM.Meter.MeterOptions { Name = name, Tags = t };
            var counter = new AM.Counter.CounterOptions { Name = name, Tags = t };

            return new CounterAdapter(_metrics, meter, counter);
        }

        public IGauge CreateGauge(string name, Dictionary<string, string> tags)
        {
            var t = GetTags(tags);
            var gauge = new AM.Gauge.GaugeOptions { Name = name, Tags = t };

            return new GaugeAdapter(_metrics, gauge);
        }

        static readonly Unit TICKS = Unit.Custom("Ticks");

        public ITimer CreateTimer(string name, Dictionary<string, string> tags)
        {
            var t = GetTags(tags);
            var histogram = new AM.Histogram.HistogramOptions { Name = name, Tags = t, MeasurementUnit = TICKS };

            return new TimerAdapter(_metrics, histogram);
        }

        class CounterAdapter : ICounter
        {
            private readonly IMetricsRoot _metrics;
            private readonly AM.Meter.MeterOptions _meter;
            private readonly AM.Counter.CounterOptions _counter;

            public CounterAdapter(IMetricsRoot metrics, AM.Meter.MeterOptions meter, AM.Counter.CounterOptions counter)
            {
                _metrics = metrics;
                _meter = meter;
                _counter = counter;
            }

            public void Inc(long delta)
            {
                _metrics.Measure.Meter.Mark(_meter, delta);
                _metrics.Measure.Counter.Increment(_counter, delta);
            }
        }

        class GaugeAdapter : IGauge
        {
            private readonly IMetricsRoot _metrics;
            private readonly AM.Gauge.GaugeOptions _gauge;

            public GaugeAdapter(IMetricsRoot metrics, AM.Gauge.GaugeOptions gauge)
            {
                _metrics = metrics;
                _gauge = gauge;
            }

            public void Update(long amount)
            {
                _metrics.Measure.Gauge.SetValue(_gauge, amount);
            }
        }

        class TimerAdapter : ITimer
        {
            private readonly IMetricsRoot _metrics;
            private readonly AM.Histogram.HistogramOptions _histogram;

            public TimerAdapter(IMetricsRoot metrics, AM.Histogram.HistogramOptions histogram)
            {
                _metrics = metrics;
                _histogram = histogram;
            }

            public void DurationTicks(long ticks)
            {
                _metrics.Measure.Histogram.Update(_histogram, ticks);
            }
        }
    }
}
cwe1ss commented 6 years ago

@jrouaix sorry for the very late reply!

Metrics are currently not part of the opentracing specification, so we wouldn't add it to this repository without a discussion/approval in the opentracing/specification-repository. Please have a look at that repository and comment on existing issues/create a new issue if you think that this is something that should be added to the specification.

We do have the opentracing-contrib org for language specific additions though. Some C# decorators are being added as a contrib project right now - see https://github.com/opentracing/opentracing-csharp/pull/104 and https://github.com/opentracing-contrib/csharp-decorators.

cwe1ss commented 6 years ago

Oh, lol - that PR is from you. Should have read all PRs / issues before commenting! Sorry! closing this...