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

[Feature Request] Skip sending metrics if endpoint is not available #171

Closed
yousifh opened this issue Apr 6, 2019 · 2 comments
Closed
Milestone

Comments

@yousifh
Copy link
Contributor

yousifh commented Apr 6, 2019

I'm using this library in an environment where everything in running in Docker containers with non-DNS based service discovery.

To use this library, I have a custom implementation of IEndPointSource that hooks up to the service discovery framework and will always return an up to date Endpoint with the right IP address of Statsd server. So SocketTransport instance will always know how to talk to Statsd.

However there are cases where the Statsd server isn't running or is unavailable for an extended period of time. Now since metrics aren't critical to running an app, it should be ok to skip sending metrics until Statsd is back up and operational. However the problem is with UDP sockets on Linux, if the endpoint isn't running it will return a connection refused error (this behaviour is Linux only based on this issue https://github.com/dotnet/corefx/issues/31206)

I was thinking if it's possible to allow IEndPointSource to return null from GetEndpoint to indicate the endpoint isn't running or not available. Then the Send method in SocketTransport can handle this condition by returning immediately instead of attempting to use a Socket to send metrics.

So this code block

public void Send(in ArraySegment<byte> metric)
{
    if (metric.Array == null || metric.Count == 0)
    {
        return;
    }

    var pool = GetPool(_endpointSource.GetEndpoint());
    var socket = pool.PopOrCreate();

Would change to

public void Send(in ArraySegment<byte> metric)
{
    if (metric.Array == null || metric.Count == 0)
    {
        return;
    }

    var endpoint = _endpointSource.GetEndpoint();
    if (endpoint == null)
    {
        return;
    }

    var pool = GetPool(endpoint);
    var socket = pool.PopOrCreate();

The current behaviour doesn't cause any exceptions to the app since by default errors will be ignored, however the socket refused error usually adds a bit of delay for each failed send call and with the approach above this can be avoided.

@yousifh yousifh changed the title [Feature Request] Skip sending metrics if endpoint not available [Feature Request] Skip sending metrics if endpoint is not available Apr 6, 2019
@AnthonySteele
Copy link
Contributor

This seems possible and non-breaking.

@martincostello
Copy link
Member

This has been published to NuGet as version 4.0.1.

Thanks again for your contribution @yousifh!

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

3 participants