Skip to content

Commit

Permalink
Merge pull request #1012 from salesforce/srh-graphite-udp-bug-fix
Browse files Browse the repository at this point in the history
@W-16736060 - support IP changes during runtime for graphite UDP repo…
  • Loading branch information
sholavanalli authored Oct 4, 2024
2 parents e8bbc25 + 2b3e815 commit 08b84be
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 10 deletions.
4 changes: 4 additions & 0 deletions cc-metrics-reporter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,7 @@ publishing {
mavenLocal()
}
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.codahale.metrics.MetricRegistry;
import com.google.common.base.Charsets;
import com.google.common.base.MoreObjects;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -19,7 +20,7 @@

public class GraphiteUDPTransport extends AbstractGraphiteTransport
{
private final InetSocketAddress address;
private InetSocketAddress address;

private DatagramSocket socket;

Expand All @@ -29,13 +30,18 @@ public class GraphiteUDPTransport extends AbstractGraphiteTransport

private int metricCounter;

private Logger LOG = LogManager.getLogger( getClass() );
private final String host;

private final int port;

private final Logger LOG = LogManager.getLogger( getClass() );

GraphiteUDPTransport( String host, int port, int batchSize, MetricRegistry metricRegistry )
{
super(metricRegistry);
this.batchSize = batchSize;
this.address = new InetSocketAddress( host, port );
this.host = host;
this.port = port;
}

/**
Expand All @@ -47,6 +53,8 @@ public class GraphiteUDPTransport extends AbstractGraphiteTransport
@Override
public GraphiteTransport open() throws IOException
{
// Support IP address changes ar runtime
this.address = new InetSocketAddress( host, port );
this.socket = new DatagramSocket( );
return this;
}
Expand Down Expand Up @@ -101,7 +109,7 @@ private void sendPacket()
metricsCount().mark( metricCounter );
}
}
catch ( IOException e )
catch ( Exception e )
{
failureCount().mark();
LOG.error( "Failed sending UDP datagram to '{}'", address );
Expand All @@ -114,11 +122,36 @@ private void sendPacket()
}

@Override
public void close() throws IOException
public void close()
{
// send whatever is remaining of the last batch
sendPacket();
socket.close();
this.socket = null;
try
{
// send whatever is remaining of the last batch
sendPacket();
}
finally
{
try
{
socket.close();
}
finally
{
this.socket = null;
}
}
}

@Override
public String toString()
{
if ( address != null )
{
return MoreObjects.toStringHelper( this ).add( "address", address ).toString();
}
else
{
return MoreObjects.toStringHelper( this ).add( "host", host ).add( "port", port ).toString();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* Copyright (c) 2018, salesforce.com, inc.
* All rights reserved.
* SPDX-License-Identifier: BSD-3-Clause
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
*/
package com.sfcc.um.metrics_reporter.transport;

import com.codahale.metrics.MetricRegistry;
import org.junit.jupiter.api.Test;

import java.io.IOException;

import static com.codahale.metrics.MetricRegistry.name;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class GraphiteUDPTransportTest {

@Test
public void testSendFailure() throws IOException {
MetricRegistry metricRegistry = new MetricRegistry();
GraphiteUDPTransport transport = new GraphiteUDPTransport("xyz.salesforce.com", 8080, 1, metricRegistry);
transport.open();
transport.send("key", "1", System.currentTimeMillis());
transport.close();
assertEquals(1L, metricRegistry.meter(name("graphite", "transport", "error-counts")).getCount());
}

@Test
public void testSendSuccess() throws IOException {
MetricRegistry metricRegistry = new MetricRegistry();
GraphiteUDPTransport transport = new GraphiteUDPTransport("salesforce.com", 80, 1, metricRegistry);
transport.open();
transport.send("key", "1", System.currentTimeMillis());
transport.close();
assertEquals(0L, metricRegistry.meter(name("graphite", "transport", "error-counts")).getCount());
assertEquals(1L, metricRegistry.meter(name("graphite", "transport", "metric-counts")).getCount());
}
}

0 comments on commit 08b84be

Please sign in to comment.