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

W-16656937: add graphite.prefix.includeHostname property #1009

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to add unit test for this ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a unit test

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"));
}
}
Loading