@Test public void test_WHEN_MetricNameSuffixRequested_THEN_ShouldReturnCorrectSample() { final Map<String, String> labels = new HashMap<String, String>(); labels.put("service", "${0}"); labels.put("status", "s_${1}"); final MapperConfig mapperConfig = new MapperConfig( "app.okhttpclient.client.HttpClient.*.*", "app.okhttpclient.client.HttpClient.${0}", labels ); final List<MapperConfig> mapperConfigs = Arrays.asList( new MapperConfig("client-nope.*.*.*"), mapperConfig, new MapperConfig("app.okhttpclient.client.HttpClient.*.total") ); final CustomMappingSampleBuilder converter = new CustomMappingSampleBuilder(mapperConfigs); final Collector.MetricFamilySamples.Sample expectedResult = new Collector.MetricFamilySamples.Sample( "app_okhttpclient_client_HttpClient_greatService_suffix", Arrays.asList("service", "status"), Arrays.asList("greatService", "s_400"), 1d); final Collector.MetricFamilySamples.Sample result = converter.createSample( "app.okhttpclient.client.HttpClient.greatService.400", "_suffix", Collections.<String>emptyList(), Collections.<String>emptyList(), 1d); assertEquals(expectedResult, result); }
@Override public Collector.MetricFamilySamples.Sample createSample(final String dropwizardName, final String nameSuffix, final List<String> additionalLabelNames, final List<String> additionalLabelValues, final double value) { if (dropwizardName == null) { throw new IllegalArgumentException("Dropwizard metric name cannot be null"); } CompiledMapperConfig matchingConfig = null; for (CompiledMapperConfig config : this.compiledMapperConfigs) { if (config.pattern.matches(dropwizardName)) { matchingConfig = config; break; } } if (matchingConfig != null) { final Map<String, String> params = matchingConfig.pattern.extractParameters(dropwizardName); final NameAndLabels nameAndLabels = getNameAndLabels(matchingConfig.mapperConfig, params); nameAndLabels.labelNames.addAll(additionalLabelNames); nameAndLabels.labelValues.addAll(additionalLabelValues); return defaultMetricSampleBuilder.createSample( nameAndLabels.name, nameSuffix, nameAndLabels.labelNames, nameAndLabels.labelValues, value ); } return defaultMetricSampleBuilder.createSample( dropwizardName, nameSuffix, additionalLabelNames, additionalLabelValues, value ); }
CustomMappingSampleBuilder implements SampleBuilder { @Override public Collector.MetricFamilySamples.Sample createSample(final String dropwizardName, final String nameSuffix, final List<String> additionalLabelNames, final List<String> additionalLabelValues, final double value) { if (dropwizardName == null) { throw new IllegalArgumentException("Dropwizard metric name cannot be null"); } CompiledMapperConfig matchingConfig = null; for (CompiledMapperConfig config : this.compiledMapperConfigs) { if (config.pattern.matches(dropwizardName)) { matchingConfig = config; break; } } if (matchingConfig != null) { final Map<String, String> params = matchingConfig.pattern.extractParameters(dropwizardName); final NameAndLabels nameAndLabels = getNameAndLabels(matchingConfig.mapperConfig, params); nameAndLabels.labelNames.addAll(additionalLabelNames); nameAndLabels.labelValues.addAll(additionalLabelValues); return defaultMetricSampleBuilder.createSample( nameAndLabels.name, nameSuffix, nameAndLabels.labelNames, nameAndLabels.labelValues, value ); } return defaultMetricSampleBuilder.createSample( dropwizardName, nameSuffix, additionalLabelNames, additionalLabelValues, value ); } }
CustomMappingSampleBuilder implements SampleBuilder { @Override public Collector.MetricFamilySamples.Sample createSample(final String dropwizardName, final String nameSuffix, final List<String> additionalLabelNames, final List<String> additionalLabelValues, final double value) { if (dropwizardName == null) { throw new IllegalArgumentException("Dropwizard metric name cannot be null"); } CompiledMapperConfig matchingConfig = null; for (CompiledMapperConfig config : this.compiledMapperConfigs) { if (config.pattern.matches(dropwizardName)) { matchingConfig = config; break; } } if (matchingConfig != null) { final Map<String, String> params = matchingConfig.pattern.extractParameters(dropwizardName); final NameAndLabels nameAndLabels = getNameAndLabels(matchingConfig.mapperConfig, params); nameAndLabels.labelNames.addAll(additionalLabelNames); nameAndLabels.labelValues.addAll(additionalLabelValues); return defaultMetricSampleBuilder.createSample( nameAndLabels.name, nameSuffix, nameAndLabels.labelNames, nameAndLabels.labelValues, value ); } return defaultMetricSampleBuilder.createSample( dropwizardName, nameSuffix, additionalLabelNames, additionalLabelValues, value ); } CustomMappingSampleBuilder(final List<MapperConfig> mapperConfigs); }
CustomMappingSampleBuilder implements SampleBuilder { @Override public Collector.MetricFamilySamples.Sample createSample(final String dropwizardName, final String nameSuffix, final List<String> additionalLabelNames, final List<String> additionalLabelValues, final double value) { if (dropwizardName == null) { throw new IllegalArgumentException("Dropwizard metric name cannot be null"); } CompiledMapperConfig matchingConfig = null; for (CompiledMapperConfig config : this.compiledMapperConfigs) { if (config.pattern.matches(dropwizardName)) { matchingConfig = config; break; } } if (matchingConfig != null) { final Map<String, String> params = matchingConfig.pattern.extractParameters(dropwizardName); final NameAndLabels nameAndLabels = getNameAndLabels(matchingConfig.mapperConfig, params); nameAndLabels.labelNames.addAll(additionalLabelNames); nameAndLabels.labelValues.addAll(additionalLabelValues); return defaultMetricSampleBuilder.createSample( nameAndLabels.name, nameSuffix, nameAndLabels.labelNames, nameAndLabels.labelValues, value ); } return defaultMetricSampleBuilder.createSample( dropwizardName, nameSuffix, additionalLabelNames, additionalLabelValues, value ); } CustomMappingSampleBuilder(final List<MapperConfig> mapperConfigs); @Override Collector.MetricFamilySamples.Sample createSample(final String dropwizardName, final String nameSuffix, final List<String> additionalLabelNames, final List<String> additionalLabelValues, final double value); }
CustomMappingSampleBuilder implements SampleBuilder { @Override public Collector.MetricFamilySamples.Sample createSample(final String dropwizardName, final String nameSuffix, final List<String> additionalLabelNames, final List<String> additionalLabelValues, final double value) { if (dropwizardName == null) { throw new IllegalArgumentException("Dropwizard metric name cannot be null"); } CompiledMapperConfig matchingConfig = null; for (CompiledMapperConfig config : this.compiledMapperConfigs) { if (config.pattern.matches(dropwizardName)) { matchingConfig = config; break; } } if (matchingConfig != null) { final Map<String, String> params = matchingConfig.pattern.extractParameters(dropwizardName); final NameAndLabels nameAndLabels = getNameAndLabels(matchingConfig.mapperConfig, params); nameAndLabels.labelNames.addAll(additionalLabelNames); nameAndLabels.labelValues.addAll(additionalLabelValues); return defaultMetricSampleBuilder.createSample( nameAndLabels.name, nameSuffix, nameAndLabels.labelNames, nameAndLabels.labelValues, value ); } return defaultMetricSampleBuilder.createSample( dropwizardName, nameSuffix, additionalLabelNames, additionalLabelValues, value ); } CustomMappingSampleBuilder(final List<MapperConfig> mapperConfigs); @Override Collector.MetricFamilySamples.Sample createSample(final String dropwizardName, final String nameSuffix, final List<String> additionalLabelNames, final List<String> additionalLabelValues, final double value);  }