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

Fixes Issue 29 - Support metric probes with no labels #30

Merged
merged 3 commits into from Nov 7, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/configuration.md
Expand Up @@ -102,6 +102,10 @@ probes:
AND trunc(TO_DATE(s.last_load_time, 'YYYY-MM-DD/HH24:MI:SS')) >= trunc(SYSDATE - 1)
ORDER BY elapsed_time DESC)
WHERE ROWNUM <= 5;
- name: noLabels
type: exists
query: SELECT 1 FROM DUAL
metricName: no_labels
```

## passwords.yml
Expand Down
Expand Up @@ -19,6 +19,10 @@

import io.github.tjheslin1.patterdale.ValueType;

import java.util.stream.Collectors;

import static java.lang.String.*;

/**
* The in-memory representation of probes list in 'patterdale.yml', passed in on app start-up.
*/
Expand Down Expand Up @@ -47,13 +51,17 @@ public Probe dbLabelled(DatabaseDefinition databaseDefinition) {
}

private String getMetricLabels(DatabaseDefinition databaseDefinition) {
final StringBuilder builder = new StringBuilder("database=\"").append(databaseDefinition.name).append("\",");
final StringBuilder builder = new StringBuilder("database=\"").append(databaseDefinition.name).append("\"");

if(databaseDefinition.metricLabels != null) {
databaseDefinition.metricLabels.forEach((label, value) -> builder.append(label).append("=\"").append(value).append("\","));
builder.append(',').append(databaseDefinition.metricLabels.entrySet()
.stream().map(e -> format("%s=\"%s\"", e.getKey(), e.getValue()))
.collect(Collectors.joining(",")));
}

builder.append(this.metricLabels);
if (metricLabels != null && !metricLabels.isEmpty()) {
builder.append(",").append(this.metricLabels);
}

return builder.toString();
}
Expand Down
Expand Up @@ -79,7 +79,7 @@ private PatterdaleConfig expectedConfig() {
expectedConfig.connectionPool = connectionPoolProperties;

expectedConfig.probes = new Probe[]{
PROBE_1, PROBE_2
PROBE_1, PROBE_2, PROBE_3
};

return expectedConfig;
Expand All @@ -94,6 +94,7 @@ private PatterdaleConfig expectedConfig() {
private static final String LIST = "list";
private static final String METRIC_NAME = "database_up";
private static final String METRIC_NAME_2 = "slowest_queries";
private static final String METRIC_NAME_3 = "no_labels";
private static final String METRIC_LABELS = "query=\"SELECT 1 FROM DUAL\"";
private static final String METRIC_LABELS_2 = "sqlText=\"%s\",sqlId=\"%s\",username=\"%s\",childNumber=\"%s\",diskReads=\"%s\",executions=\"%s\",firstLoadTime=\"%s\",lastLoadTime=\"%s\"";
private static final String QUERY_SQL_1 = "SELECT 1 FROM DUAL";
Expand All @@ -113,6 +114,7 @@ private PatterdaleConfig expectedConfig() {
"WHERE s.parsing_user_id = d.user_id\n" +
"AND trunc(TO_DATE(s.last_load_time, 'YYYY-MM-DD/HH24:MI:SS')) >= trunc(SYSDATE - 1)\n" +
"ORDER BY elapsed_time DESC)\n" +
"WHERE ROWNUM <= 5;";
"WHERE ROWNUM <= 5;\n";
private static final Probe PROBE_2 = probe("slowestQueries", QUERY_SQL_2, LIST, METRIC_NAME_2, METRIC_LABELS_2);
private static final Probe PROBE_3 = probe("noLabels", QUERY_SQL_1, EXISTS, METRIC_NAME_3, null);
}
Expand Up @@ -29,4 +29,14 @@ public void dbLabelledPassesMetricLabelsFromDatabaseDefinitionToProbe() {

assertThat(got.metricLabels).isEqualTo("database=\"db\",label1=\"value1\",label2=\"value2\",probeLabel=\"probeValue\"");
}
}

@Test
public void dbLabelledWithNoMetricLabels() {
final Probe probe = Probe.probe("name", "SQL; ", "", "", null);
final Map<String, String> labels = new HashMap<>();
labels.put("label1", "value1");
tommagowan marked this conversation as resolved.
Show resolved Hide resolved

final Probe got = probe.dbLabelled(databaseDefinition("db", "user", "url", emptyList(), labels));
assertThat(got.metricLabels).isEqualTo("database=\"db\",label1=\"value1\"");
}
}
6 changes: 5 additions & 1 deletion src/test/resources/patterdale.yml
Expand Up @@ -48,4 +48,8 @@ probes:
WHERE s.parsing_user_id = d.user_id
AND trunc(TO_DATE(s.last_load_time, 'YYYY-MM-DD/HH24:MI:SS')) >= trunc(SYSDATE - 1)
ORDER BY elapsed_time DESC)
WHERE ROWNUM <= 5;
WHERE ROWNUM <= 5;
- name: noLabels
type: exists
query: SELECT 1 FROM DUAL
metricName: no_labels