PositionBasedTransformer implements Transformer { @Override public Map<String, String> getTags(String metricsName) { String[] parts = metricsName.split(SEPARATOR); Map<String, String> tags = new HashMap<String, String>(); for (Map.Entry<String, Category> entry : mappings.entrySet()) { final Pattern pattern = entry.getValue().getMapping(); final int position = entry.getValue().position; if ((parts.length > position) && pattern.matcher(metricsName).matches()) { tags.put(entry.getKey(), parts[position]); } } return tags; } PositionBasedTransformer(Map<String, Category> mappings); @Override Map<String, String> getTags(String metricsName);  }
@Test public void shouldExtractMultipleTags() { Map<String, PositionBasedTransformer.Category> mappings = new HashMap<String, PositionBasedTransformer.Category>(); mappings.put("className", new PositionBasedTransformer.Category( 5, "com\\.izettle\\.metrics\\.influxdb\\.tags\\..*")); mappings.put("function", new PositionBasedTransformer.Category( 6, "com\\.izettle\\.metrics\\.influxdb\\.tags\\..*")); PositionBasedTransformer transformer = new PositionBasedTransformer(mappings); assertThat(transformer.getTags("com.izettle.metrics.influxdb.tags.PositionBasedTransformer.count")) .containsEntry("className", "PositionBasedTransformer") .containsEntry("function", "count"); assertThat(transformer.getTags("com.izettle.metrics.influxdb.tags.NoopTransformer.count")) .containsEntry("className", "NoopTransformer") .containsEntry("function", "count"); }
@Test public void shouldIgnoreIncorrectPosition() { Map<String, PositionBasedTransformer.Category> mappings = new HashMap<String, PositionBasedTransformer.Category>(); mappings.put("incorrectTag", new PositionBasedTransformer.Category(42, ".*")); PositionBasedTransformer transformer = new PositionBasedTransformer(mappings); assertThat(transformer.getTags("com.izettle.metrics.influxdb.tags.PositionBasedTransformer.count")).isEmpty(); }