Giter Site home page Giter Site logo

datadogsharp's Introduction

DatadogSharp

Yet another C# Datadog client that supports DogStatsD and APM.

What is this

A Datadog client for C# which transport metrics to datadog agent. Datadog has official C# client but it is not performant. DatadogSharp is more performant and has zero-overhead api.

Additionaly supports Datadog APM API for Datadog trace agent with my fastest MessagePack for C# implements. This is only C# SDK which supports APM.

Installation

The library provides in NuGet for .NET 4.5, 4.6 and .NET Standard 2.0(.NET Core).

Install-Package DatadogSharp

How to use(DogStatsd)

At first, configure transport address on application startup.

using DatadogSharp.DogStatsd;

DatadogStats.ConfigureDefault(
    address: "127.0.0.1",
    port: 8125, // Optional, default is 8125
    metricNamePrefix: null, // Optinal, if exists prefix, append "prefix." on every metrics call
    defaultTags: null // Optional, append this tag with called tags
);

And instrument

// DatadogStats.Default is configured default invoker.

// Increment and Decrement is special case of `Counter`.
DatadogStats.Default.Increment("metricA");
DatadogStats.Default.Decrement("metricA", 3);

// Metrics is follows to http://docs.datadoghq.com/guides/dogstatsd/
DatadogStats.Default.Gauge();
DatadogStats.Default.Histogram();
DatadogStats.Default.Set();
DatadogStats.Default.Timer();

// sampleRate and tags are optional
DatadogStats.Default.Histogram("merticB", sampleRate:0.5);
DatadogStats.Default.Histogram("merticB", tags:new[] { "mygroup:foo", "foobar" });

// BeginTimer is scoped verison of Timer, you can measure executing time easily
using(DatadogStats.Default.BeginTimer())
{
}

// You can Event and ServiceCheck. message is automaticaly truncated max 4096.
DatadogStats.Default.Event("MyApp.Exception", "Stacktrace and messdages",  alertType:AlertType.Error);
DatadogStats.Default.ServiceCheck();

If you want to use another configuration(meric prefix, default tags), you can create DatadogStats instance and use it.

var anotherStats = new DatadogStats("127.0.0.1", 9999);

How to use(APM)

APM's entrypoint is TracingManager. Here is the simple sample of APM Client.

using (var trace = TracingManager.Default.BeginTracing("Request", "/Home/Index", "webservice", "web"))
{
    Thread.Sleep(TimeSpan.FromSeconds(1));

    using (trace.BeginSpan("QuerySql", trace.Resource, "sqlserver", "db"))
    {
        Thread.Sleep(TimeSpan.FromSeconds(2));
    }

    Thread.Sleep(TimeSpan.FromSeconds(2));
}

image

Windows binary of trace agent is in datadog-trace-agent's releases page.

BeginTracing returns TracingScope, it starts tracing as root, when calls TracingScope.Dispose(or use using scope) finish measurement and enqueue to send worker.

BeginSpan returns SpanScope, it starts child tracing, when calls SpanScope.Dispose(or use using scope) finish measurement and add span to root tracing.

TracingScope and SpanScope have WithMeta and WithError methods for append additional info.

using (var trace = TracingManager.Default.BeginTracing("Request", "/Home/Index", "webservice", "web"))
{
    try
    {
        // do anything...
        var userId = ....
        trace.WithMeta(new Dictionary<string, string>
        {
            {"userId", userId.ToString() }
        });
    }
    catch (Exception ex)
    {
        trace.WithError(); // mark error.
        trace.WithMeta(new Dictionary<string, string>
        {
            {"exception", ex.ToString() }
        });
    }
}
void Application_BeginRequest()
{
    var trace = TracingManager.Default.BeginTracing("Request", "/Home/Index", "webservice", "web");
    HttpContext.Current.Items["DatadogTracingScope"] = trace;
}

void Application_EndRequest()
{
    var scope = HttpContext.Current.Items["DatadogTracingScope"] as TracingScope;
    scope?.Dispose(); // complete scope.
}

void Application_Error()
{
    var scope = HttpContext.Current.Items["DatadogTracingScope"] as TracingScope;
    var ex = Server.GetLastError();
    scope.WithError();
    scope.WithMeta(x =>
    {
        x.Add("exception", ex.ToString());
    });
}

ASP.NET Core in IHttpContextAccessor

Real sample

In web, store TraceScope in HttpContext.

you can store in AsyncLocal.

Advanced(APM)

DatadogClient is wrapper of HttpClient to access APM endpoint.

Author Info

Yoshifumi Kawai(a.k.a. neuecc) is a software developer in Japan.
He is the Director/CTO at Grani, Inc.
Grani is a mobile game developer company in Japan and well known for using C#.
He is awarding Microsoft MVP for Visual C# since 2011.
He is known as the creator of UniRx(Reactive Extensions for Unity)

Blog: https://medium.com/@neuecc (English)
Blog: http://neue.cc/ (Japanese)
Twitter: https://twitter.com/neuecc (Japanese)

License

This library is under the MIT License.

datadogsharp's People

Contributors

kralizek avatar neuecc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

datadogsharp's Issues

TracingManager and ParentId

Hello i'm doing some test with APM client and i do not understand why in the root Span you force ParentId = null. On my test i'm working with a distributed monitoring, so i always receive a root information from an other platform / system but with ParentId = null i'm not able to link the first Span with the right parent. I'm using the BeginTracingto initialize the context.

I mean this code snippet in Dispose method in TracingManager class:

var span = new Span
            {
                TraceId = TraceId,
                SpanId = SpanId,
                Name = Name,
                Resource = Resource,
                Service = Service,
                Type = Type,
                Start = start,
                Duration = Span.ToNanoseconds(stopwatch.Elapsed),
                ParentId = null,
                Error = error,
                Meta = meta,
            };

Is it a right behaviour or a bug ?

At the moment i fixed with this code:

ParentId = this.parentId,

Add support for Dependency Injection

Both DatadogStats and TracingManager use a Singleton approach but it makes very difficult to test classes using this library requiring the usage of an (unnecessary) adapter class.

If it's welcome, I can work on a PR to solve the issue.

Support configuring the client from environment variables

All of the official Datadog dogstatsd clients can now be configured via standard environment variables. As the maintainer of the C# dogstatsd library, could you port the same functionality to this client? We would prefer that configuration be consistent across client libraries.

Sample changes from the C# implementation can be found here: DataDog/dogstatsd-csharp-client#78

Let me know if you have any questions about the code or the feature more generally.

Thanks,

Ahmed

Azure App Service Usage

Hi, we are using Azure App Service plan, and which does not have a Statsd server. Anyway to go around this, other than installing the DD agent on a VM somewhere?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.