|
| 1 | +/* |
| 2 | + * Copyright (c) 2000, 2025, Oracle and/or its affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Universal Permissive License v 1.0 as shown at |
| 5 | + * https://oss.oracle.com/licenses/upl. |
| 6 | + */ |
| 7 | +package metrics; |
| 8 | + |
| 9 | +import com.oracle.bedrock.runtime.LocalPlatform; |
| 10 | +import com.oracle.bedrock.runtime.coherence.CoherenceClusterMember; |
| 11 | +import com.oracle.bedrock.runtime.coherence.options.LocalHost; |
| 12 | +import com.oracle.bedrock.runtime.coherence.options.Logging; |
| 13 | +import com.oracle.bedrock.runtime.java.options.IPv4Preferred; |
| 14 | +import com.oracle.bedrock.runtime.java.options.SystemProperty; |
| 15 | +import com.oracle.bedrock.testsupport.deferred.Eventually; |
| 16 | +import com.oracle.bedrock.util.Capture; |
| 17 | +import com.tangosol.coherence.metrics.internal.MetricsResource; |
| 18 | +import com.tangosol.internal.net.metrics.MetricsHttpHelper; |
| 19 | +import java.net.HttpURLConnection; |
| 20 | +import java.net.URI; |
| 21 | + |
| 22 | +import java.io.BufferedReader; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.io.InputStreamReader; |
| 25 | +import java.io.PrintWriter; |
| 26 | + |
| 27 | +import java.util.ArrayList; |
| 28 | +import java.util.Collection; |
| 29 | + |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.junit.runners.Parameterized; |
| 33 | + |
| 34 | +import static org.hamcrest.CoreMatchers.is; |
| 35 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 36 | + |
| 37 | +@RunWith(Parameterized.class) |
| 38 | +public class MetricsFormatTests |
| 39 | + extends AbstractMetricsFunctionalTest |
| 40 | + { |
| 41 | + // ----- Parameterized Setup -------------------------------------------- |
| 42 | + |
| 43 | + /** |
| 44 | + * Create the test parameters. |
| 45 | + * |
| 46 | + * @return the test parameters |
| 47 | + */ |
| 48 | + @Parameterized.Parameters(name = "Format={0}") |
| 49 | + public static Collection<Object[]> getTestParameters() |
| 50 | + { |
| 51 | + ArrayList<Object[]> parameters = new ArrayList<>(); |
| 52 | + |
| 53 | + // Run Coherence with no metrics format configured - i.e. use whatever is the current default |
| 54 | + parameters.add(new Object[]{"None Specified", "foo", false}); |
| 55 | + // Run Coherence with the legacy metrics format configured |
| 56 | + parameters.add(new Object[]{"Legacy", MetricsResource.PROP_USE_LEGACY_NAMES, true}); |
| 57 | + // Run Coherence with the MP metrics format configured |
| 58 | + parameters.add(new Object[]{"MP", MetricsResource.PROP_USE_MP_NAMES, true}); |
| 59 | + // Run Coherence with the dot delimited metrics format configured |
| 60 | + parameters.add(new Object[]{"Dot Delimited", "foo", false}); |
| 61 | + |
| 62 | + return parameters; |
| 63 | + } |
| 64 | + |
| 65 | + // ----- constructors --------------------------------------------------- |
| 66 | + /** |
| 67 | + * Create the test instance. |
| 68 | + * |
| 69 | + * @param sFormat the descriptive name of the test |
| 70 | + * @param sProperty the system property to set to configure the metric name format |
| 71 | + * @param fValue the value to set the system property to |
| 72 | + * @param format the expected metrics format |
| 73 | + */ |
| 74 | + public MetricsFormatTests(String sFormat, String sProperty, boolean fValue) |
| 75 | + { |
| 76 | + super("client-cache-config-metrics.xml"); |
| 77 | + f_sFormat = sFormat; |
| 78 | + f_sProperty = sProperty; |
| 79 | + f_fPropertyValue = fValue; |
| 80 | + } |
| 81 | + |
| 82 | + // ----- test ----------------------------------------------------------- |
| 83 | + /** |
| 84 | + * Just extracts metrics into the file so their format can be verified by an external tool. |
| 85 | + * |
| 86 | + * @throws Exception |
| 87 | + */ |
| 88 | + @Test |
| 89 | + public void shouldHaveCorrectFormat() throws Exception |
| 90 | + { |
| 91 | + LocalPlatform platform = LocalPlatform.get(); |
| 92 | + Capture<Integer> port = new Capture<>(platform.getAvailablePorts()); |
| 93 | + |
| 94 | + String additionalMetricsProperty = "foo"; |
| 95 | + if (!f_sFormat.equals("Legacy")) |
| 96 | + { |
| 97 | + additionalMetricsProperty = MetricsResource.PROP_USE_LEGACY_NAMES; |
| 98 | + } |
| 99 | + // start Coherence with metrics configured with the test format |
| 100 | + try (CoherenceClusterMember member = platform.launch(CoherenceClusterMember.class, |
| 101 | + SystemProperty.of(f_sProperty, f_fPropertyValue), |
| 102 | + SystemProperty.of(additionalMetricsProperty, false), |
| 103 | + SystemProperty.of(MetricsHttpHelper.PROP_METRICS_ENABLED, true), |
| 104 | + SystemProperty.of("coherence.metrics.http.port", port), |
| 105 | + SystemProperty.of("test.persistence.enabled", false), |
| 106 | + SystemProperty.of("coherence.management.extendedmbeanname", true), |
| 107 | + SystemProperty.of(Logging.PROPERTY_LEVEL, "9"), |
| 108 | + IPv4Preferred.yes(), |
| 109 | + LocalHost.only())) |
| 110 | + { |
| 111 | + Eventually.assertDeferred(() -> member.isServiceRunning(MetricsHttpHelper.getServiceName()), is(true)); |
| 112 | + |
| 113 | + String sURL = "http://127.0.0.1:" + port.get() + "/metrics"; |
| 114 | + |
| 115 | + HttpURLConnection con = (HttpURLConnection) URI.create(sURL).toURL().openConnection(); |
| 116 | + con.setRequestProperty("Accept", "text/plain"); |
| 117 | + con.setRequestMethod("GET"); |
| 118 | + |
| 119 | + int responseCode = con.getResponseCode(); |
| 120 | + assertThat(responseCode, is(200)); |
| 121 | + |
| 122 | + try (PrintWriter writer = new PrintWriter("target/" + f_sFormat + ".metrics.txt"); |
| 123 | + InputStream in = con.getInputStream()) |
| 124 | + { |
| 125 | + BufferedReader isr = new BufferedReader(new InputStreamReader(in)); |
| 126 | + String line; |
| 127 | + int counter = 0; |
| 128 | + while ((line = isr.readLine()) != null ) |
| 129 | + { |
| 130 | + writer.print(line + "\n"); |
| 131 | + // limit example size otherwise verification takes too much time to complete |
| 132 | + if (counter++ == 110) |
| 133 | + { |
| 134 | + break; |
| 135 | + } |
| 136 | + } |
| 137 | + writer.print("# EOF"); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // ----- data members ---------------------------------------------------- |
| 143 | + |
| 144 | + /** |
| 145 | + * The text description of the format to set. |
| 146 | + */ |
| 147 | + private final String f_sFormat; |
| 148 | + |
| 149 | + /** |
| 150 | + * The system property to set to configure the name format. |
| 151 | + */ |
| 152 | + private final String f_sProperty; |
| 153 | + |
| 154 | + /** |
| 155 | + * The value of the system property to set to configure the name format. |
| 156 | + */ |
| 157 | + private final boolean f_fPropertyValue; |
| 158 | + } |
0 commit comments