Skip to content

Commit

Permalink
Merge pull request #1009 from salesforce/pmoineau.W-16656937.graphite…
Browse files Browse the repository at this point in the history
…-prefix

W-16656937: add graphite.prefix.includeHostname property
  • Loading branch information
pdmoineau authored Sep 12, 2024
2 parents 4125492 + 277d8bd commit 40bec26
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ NameUtils nameUtils()

@Value( "${graphite.prefix:}" ) private String graphitePrefix;

@Value( "${graphite.prefix.includeHostname:false}" ) private boolean graphitePrefixIncludeHostname;

@Value( "${dw.podId:-1}" ) private int podId;

@Value( "${dw.groupId:}" ) private String groupId;
Expand All @@ -82,7 +84,8 @@ NameUtils nameUtils()
{
GraphiteReporter graphiteReporter =
Util.getGraphiteReporter( metricRegistry, graphiteHost, graphitePort, graphiteTransport,
Util.getGraphiteMetricPrefix( graphitePrefix, podId, groupId, svcVersion ) );
Util.getGraphiteMetricPrefix( graphitePrefix, graphitePrefixIncludeHostname,
podId, groupId, svcVersion ) );
if ( graphiteReporter != null )
{
graphiteReporter.start( 60, TimeUnit.SECONDS );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ public class Util

private static GraphiteReporter graphiteReporter = null;

public static String getGraphiteMetricPrefix( String graphiteMetricsPrefix, int podId, String groupId,
String version )
public static String getGraphiteMetricPrefix( String graphiteMetricsPrefix, boolean graphitePrefixIncludeHostname,
int podId, String groupId, String version )
throws UnknownHostException
{
String fqdn = InetAddress.getLocalHost().getHostName();
Expand All @@ -40,6 +40,10 @@ else if ( StringUtils.isEmpty( graphiteMetricsPrefix ) )
{
graphiteMetricsPrefix = "um.dev.carbonj." + hostname;
}
else if ( graphitePrefixIncludeHostname )
{
graphiteMetricsPrefix = graphiteMetricsPrefix + "." + hostname;
}
return graphiteMetricsPrefix;
}

Expand Down
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.demandware.core.metric;

import java.net.InetAddress;
import java.net.UnknownHostException;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class TestUtil
{
private static String falconPrefix = "cc-umon-client.falcon-fi";
private static String umDevPrefix = "um.dev.carbonj.";

@Test
public void testGetGraphiteMetricPrefix() throws UnknownHostException
{
String fqdn = InetAddress.getLocalHost().getHostName();
String hostname = fqdn.substring(0, fqdn.indexOf( "." ) == -1 ? fqdn.length() : fqdn.indexOf( "." ) );

// Verify provided prefix with hostname
assertEquals(falconPrefix + "." + hostname, Util.getGraphiteMetricPrefix(falconPrefix, true, -1, "00", "v1"));

// Verify provided prefix without hostname
assertEquals(falconPrefix, Util.getGraphiteMetricPrefix(falconPrefix, false, -1, "00", "v1"));

// Verify prefix if podId is provided
assertEquals("pod100.00.carbonj." + hostname + ".v1", Util.getGraphiteMetricPrefix(null, false, 100, "00", "v1"));

// Verify prefix if podId and prefix aren't provided
assertEquals(umDevPrefix + hostname, Util.getGraphiteMetricPrefix(null, false, -1, "00", "v1"));
}
}

0 comments on commit 40bec26

Please sign in to comment.