Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Additional prometheus labels from envvars (#61)
* Initial implementation of additional prometheus labels from envvars * Unit tests.. * Catching up with refactored config field in master * Style * Type erasure ¯\_(ツ)_/¯ * Tests to prove fix in 58e093d
- Loading branch information
Andras Szerdahelyi
authored and
Erèbe - Romain Gerard
committed
Oct 30, 2019
1 parent
9e20c58
commit 26de7ce
Showing
9 changed files
with
247 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/test/java/com/criteo/nosql/cassandra/exporter/ConfigTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,26 @@ | |||
package com.criteo.nosql.cassandra.exporter; | |||
|
|||
import org.junit.Test; | |||
|
|||
import java.util.Optional; | |||
import java.util.regex.Pattern; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
|
|||
import static org.junit.Assert.*; | |||
|
|||
public class ConfigTest { | |||
Logger logger = LoggerFactory.getLogger(ConfigTest.class); | |||
|
|||
@Test | |||
public void test_additional_envvars_parsed() { | |||
|
|||
Optional<Config> configWithAdditionalEnvvars = Config.fromFile("src/test/resources/config_tests/config_with_additional_envvars_regexp.yml"); | |||
assertTrue(configWithAdditionalEnvvars.isPresent()); | |||
assertTrue(configWithAdditionalEnvvars.get().getAdditionalLabelsFromEnvvars().isPresent()); | |||
|
|||
assertEquals(Pattern.compile("^ADDL\\_(.*)$").pattern(), configWithAdditionalEnvvars.get().getAdditionalLabelsFromEnvvars().get().pattern()); | |||
|
|||
} | |||
|
|||
} |
27 changes: 27 additions & 0 deletions
27
src/test/java/com/criteo/nosql/cassandra/exporter/JmxScraperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,27 @@ | |||
package com.criteo.nosql.cassandra.exporter; | |||
|
|||
import org.junit.Test; | |||
|
|||
import static org.hamcrest.CoreMatchers.instanceOf; | |||
import static org.junit.Assert.assertArrayEquals; | |||
import static org.junit.Assert.assertThat; | |||
|
|||
public class JmxScraperTest { | |||
|
|||
@Test | |||
public void test_concat_concatenates_array_contents() { | |||
String[] arrayA = new String[]{"foo","bar","1"}; | |||
String[] arrayB = new String[]{"baz","2"}; | |||
|
|||
assertArrayEquals(new String[]{"foo","bar","1", "baz", "2"}, JmxScraper.concat(arrayA, arrayB)); | |||
} | |||
|
|||
@Test | |||
public void test_concat_results_in_generic_array_of_expected_type_parameter() { | |||
String[] arrayA = new String[]{"foo","bar","1"}; | |||
String[] arrayB = new String[]{"baz","2"}; | |||
|
|||
assertThat(JmxScraper.concat(arrayA, arrayB), instanceOf(String[].class)); | |||
} | |||
|
|||
} |
59 changes: 59 additions & 0 deletions
59
src/test/java/com/criteo/nosql/cassandra/exporter/MainTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,59 @@ | |||
package com.criteo.nosql.cassandra.exporter; | |||
|
|||
import org.junit.Test; | |||
|
|||
import java.util.Collections; | |||
import java.util.HashMap; | |||
import java.util.Map; | |||
import java.util.Optional; | |||
import java.util.regex.Pattern; | |||
|
|||
import static org.junit.Assert.assertEquals; | |||
|
|||
public class MainTest { | |||
|
|||
private static class AdditionalEnvvars { | |||
|
|||
private final static Map<String, String> envWithMatchingVars = new HashMap<String, String>() {{ | |||
put("foo", "bar"); | |||
put("ADDL_relevant-envvar-1", "relevant-value-1"); | |||
put("ADDL_relevant-envvar-2", "relevant-value-2"); | |||
}}; | |||
|
|||
private final static Map<String, String> envWithNoMatchingVars = new HashMap<String, String>() {{ | |||
put("foo", "bar"); | |||
put("IRRELEVANT-ENVVAR-1", "irrelevant-value-1"); | |||
put("IRRELEVANT-ENVVAR-2", "irrelevant-value-2"); | |||
}}; | |||
|
|||
private final static Pattern findRelevantEnvvarsGroup = Pattern.compile("^ADDL\\_(.*)$"); | |||
private final static Pattern findRelevantEnvvarsNoGroup = Pattern.compile("^ADDL\\_.*$"); | |||
} | |||
|
|||
|
|||
@Test | |||
public void test_findAdditionalLabelsInEnvironment_with_group_pattern_present_and_matching_envvars() { | |||
|
|||
assertEquals(new HashMap<String, String>() {{ | |||
put("relevant-envvar-1", "relevant-value-1"); | |||
put("relevant-envvar-2", "relevant-value-2"); | |||
}}, Main.findAdditionalLabelsInEnvironment(AdditionalEnvvars.envWithMatchingVars, Optional.of(AdditionalEnvvars.findRelevantEnvvarsGroup))); | |||
|
|||
} | |||
|
|||
@Test | |||
public void test_findAdditionalLabelsInEnvironment_with_pattern_present_and_no_matching_envvars() { | |||
|
|||
assertEquals(new HashMap<String, String>() {{ | |||
put("ADDL_relevant-envvar-1", "relevant-value-1"); | |||
put("ADDL_relevant-envvar-2", "relevant-value-2"); | |||
}}, Main.findAdditionalLabelsInEnvironment(AdditionalEnvvars.envWithMatchingVars, Optional.of(AdditionalEnvvars.findRelevantEnvvarsNoGroup))); | |||
} | |||
|
|||
@Test | |||
public void test_findAdditionalLabelsInEnvironment_with_no_pattern_present() { | |||
|
|||
assertEquals(Collections.emptyMap(), Main.findAdditionalLabelsInEnvironment(AdditionalEnvvars.envWithMatchingVars, Optional.empty())); | |||
} | |||
|
|||
} |
66 changes: 66 additions & 0 deletions
66
src/test/resources/config_tests/config_with_additional_envvars_regexp.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,66 @@ | |||
host: localhost:7199 | |||
ssl: False | |||
user: | |||
password: | |||
listenAddress: 0.0.0.0 | |||
listenPort: 8080 | |||
# Regular expression to match environment variables that will be added | |||
# as labels to all data points. The name of the label will be either | |||
# $1 from the regex below, or the entire environment variable name if no match groups are defined | |||
# | |||
# Example: | |||
# additionalLabelsFromEnvvars: "^ADDL\_(.*)$" | |||
additionalLabelsFromEnvvars: "^ADDL\\_(.*)$" | |||
blacklist: | |||
# To profile the duration of jmx call you can start the program with the following options | |||
# > java -Dorg.slf4j.simpleLogger.defaultLogLevel=trace -jar cassandra_exporter.jar config.yml --oneshot | |||
# | |||
# To get intuition of what is done by cassandra when something is called you can look in cassandra | |||
# https://github.com/apache/cassandra/tree/trunk/src/java/org/apache/cassandra/metrics | |||
# Please avoid to scrape frequently those calls that are iterating over all sstables | |||
|
|||
# Unaccessible metrics (not enough privilege) | |||
- java:lang:memorypool:.*usagethreshold.* | |||
|
|||
# Leaf attributes not interesting for us but that are presents in many path | |||
- .*:999thpercentile | |||
- .*:95thpercentile | |||
- .*:fifteenminuterate | |||
- .*:fiveminuterate | |||
- .*:durationunit | |||
- .*:rateunit | |||
- .*:stddev | |||
- .*:meanrate | |||
- .*:mean | |||
- .*:min | |||
|
|||
# Path present in many metrics but uninterresting | |||
- .*:viewlockacquiretime:.* | |||
- .*:viewreadtime:.* | |||
- .*:cas[a-z]+latency:.* | |||
- .*:colupdatetimedeltahistogram:.* | |||
|
|||
# Mostly for RPC, do not scrap them | |||
- org:apache:cassandra:db:.* | |||
|
|||
# columnfamily is an alias for Table metrics | |||
# https://github.com/apache/cassandra/blob/8b3a60b9a7dbefeecc06bace617279612ec7092d/src/java/org/apache/cassandra/metrics/TableMetrics.java#L162 | |||
- org:apache:cassandra:metrics:columnfamily:.* | |||
|
|||
# Should we export metrics for system keyspaces/tables ? | |||
- org:apache:cassandra:metrics:[^:]+:system[^:]*:.* | |||
|
|||
# Don't scrap us | |||
- com:criteo:nosql:cassandra:exporter:.* | |||
|
|||
maxScrapeFrequencyInSec: | |||
50: | |||
- .* | |||
|
|||
# Refresh those metrics only every hour as it is costly for cassandra to retrieve them | |||
3600: | |||
- .*:snapshotssize:.* | |||
- .*:estimated.* | |||
- .*:totaldiskspaceused:.* | |||
|
|||
|