Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide Integration with System.Diagnostics.Metrics #341

Open
slang25 opened this issue Jun 25, 2021 · 1 comment
Open

Provide Integration with System.Diagnostics.Metrics #341

slang25 opened this issue Jun 25, 2021 · 1 comment

Comments

@slang25
Copy link
Member

slang25 commented Jun 25, 2021

Microsoft are introducing a new set of metrics APIs, which could potentially be used as an alternative abstraction to use our library with (the same way M.E.L.A is to SeriLog), but probably more interestingly, we'd be able to send lots of Microsoft internal metrics (from ASP.NET Core for example) to StatsD to be able to graph on, which would be super cool.

I'm not completely sure what is required, here is the listener part of the design we'd probably want to integrate with:
https://github.com/dotnet/designs/blob/main/accepted/2021/System.Diagnostics/Metrics-Design.md#listening-example

We might have an extension method on MeterListener (InstrumentListener in the design above, I think it's changed) which registers the various SetMeasurementEventCallback<T> callbacks for each supported primitive type.

I'm thinking a light library named JustEat.Diagnostics.Metrics.StatsD.

@martincostello
Copy link
Member

I had a very quick look at this and ended up with this Console app using 6.0 RC1 to handle counters using the design doc as a guide.

using System.Diagnostics.Metrics;
using JustEat.StatsD;

var publisher = new StatsDPublisher(new()
{
    Host = "localhost",
    Port = 8125,
    Prefix = "myservice",
});

var listener = new MeterListener();
listener.InstrumentPublished = (instrument, meterListener) =>
{
    if (instrument.Name == "Requests" && instrument.Meter.Name == "io.opentelemetry.contrib.mongodb")
    {
        meterListener.EnableMeasurementEvents(instrument, null);
    }
};

listener.SetMeasurementEventCallback<int>((instrument, measurement, tags, state) =>
{
    Dictionary<string, string?>? stringTags = null;

    if (!tags.IsEmpty)
    {
        stringTags = new Dictionary<string, string?>();

        foreach (var tag in tags)
        {
            stringTags[tag.Key] = tag.Value?.ToString();
        }
    }

    publisher.Increment(measurement, instrument.Meter.Name + "." + instrument.Name, stringTags);
});

listener.Start();

using var meter = new Meter("io.opentelemetry.contrib.mongodb", "v1.0");
var counter = meter.CreateCounter<int>("Requests");
counter.Add(1);
counter.Add(1, KeyValuePair.Create<string, object?>("request", "read"));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants