Skip to content

Commit

Permalink
atlas: aggr function with empty tags (#1097)
Browse files Browse the repository at this point in the history
Uses a placeholder name of `unknown` if the query for an
aggregate function has no exact tags. This matches the
behavior of the backend.
  • Loading branch information
brharrington authored Dec 2, 2023
1 parent 3fdaece commit 278395b
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ final class All implements DataExpr {

/** Base type for simple aggregate functions. */
interface AggregateFunction extends DataExpr {

/** Return the exact matches from the query clause. */
Map<String, String> queryTags();

@Override default Map<String, String> resultTags(Map<String, String> tags) {
Map<String, String> ts = queryTags();
return ts.isEmpty()
? Collections.singletonMap("name", "unknown")
: ts;
}
}

/**
Expand All @@ -202,7 +212,7 @@ final class Sum implements AggregateFunction {
return true;
}

@Override public Map<String, String> resultTags(Map<String, String> tags) {
@Override public Map<String, String> queryTags() {
return queryTags;
}

Expand Down Expand Up @@ -269,7 +279,7 @@ final class Min implements AggregateFunction {
return false;
}

@Override public Map<String, String> resultTags(Map<String, String> tags) {
@Override public Map<String, String> queryTags() {
return queryTags;
}

Expand Down Expand Up @@ -336,7 +346,7 @@ final class Max implements AggregateFunction {
return false;
}

@Override public Map<String, String> resultTags(Map<String, String> tags) {
@Override public Map<String, String> queryTags() {
return queryTags;
}

Expand Down Expand Up @@ -407,7 +417,7 @@ final class Count implements AggregateFunction {
return true;
}

@Override public Map<String, String> resultTags(Map<String, String> tags) {
@Override public Map<String, String> queryTags() {
return queryTags;
}

Expand Down Expand Up @@ -496,7 +506,7 @@ private Map<String, String> keyTags(Map<String, String> tags) {
if (resultTags == null) {
return null;
} else {
resultTags.putAll(af.resultTags(tags));
resultTags.putAll(af.queryTags());
return resultTags;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,20 @@ public void mismatchedClosingParen() {
() -> Parser.parseDataExpr("key,(,key,),),:in,:sum"));
}

@Test
public void orClauseTags() {
DataExpr expr = parse("name,foo,:eq,name,bar,:eq,:or,:sum");
Map<String, String> tags = expr.resultTags(Collections.singletonMap("name", "foo"));
Assertions.assertEquals(Collections.singletonMap("name", "unknown"), tags);
}

@Test
public void orClauseTagsGroupBy() {
DataExpr expr = parse("name,foo,:eq,name,bar,:eq,:or,:sum,(,name,),:by");
Map<String, String> tags = expr.resultTags(Collections.singletonMap("name", "foo"));
Assertions.assertEquals(Collections.singletonMap("name", "foo"), tags);
}

@Test
public void allEqualsContract() {
EqualsVerifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ public void sumAndMaxForGroup() {
EvalPayload payload = evaluator.eval(0L, data("foo", 1.0, 2.0, 3.0));

List<EvalPayload.Metric> metrics = new ArrayList<>();
metrics.add(new EvalPayload.Metric("max", Collections.emptyMap(), 3.0));
metrics.add(new EvalPayload.Metric("sum", Collections.emptyMap(), 6.0));
metrics.add(new EvalPayload.Metric("max", tags("name", "unknown"), 3.0));
metrics.add(new EvalPayload.Metric("sum", tags("name", "unknown"), 6.0));
EvalPayload expected = new EvalPayload(0L, metrics);
Assertions.assertEquals(expected, sort(payload));
}
Expand All @@ -133,7 +133,7 @@ public void updateSub() {
evaluator.sync(sumSub);
EvalPayload payload = evaluator.eval(0L, data("foo", 1.0, 2.0, 3.0));
List<EvalPayload.Metric> metrics = new ArrayList<>();
metrics.add(new EvalPayload.Metric("sum", Collections.emptyMap(), 6.0));
metrics.add(new EvalPayload.Metric("sum", tags("name", "unknown"), 6.0));
EvalPayload expected = new EvalPayload(0L, metrics);
Assertions.assertEquals(expected, payload);

Expand All @@ -143,7 +143,7 @@ public void updateSub() {
evaluator.sync(maxSub);
payload = evaluator.eval(0L, data("foo", 1.0, 2.0, 3.0));
metrics = new ArrayList<>();
metrics.add(new EvalPayload.Metric("sum", Collections.emptyMap(), 3.0));
metrics.add(new EvalPayload.Metric("sum", tags("name", "unknown"), 3.0));
expected = new EvalPayload(0L, metrics);
Assertions.assertEquals(expected, payload);
}
Expand Down Expand Up @@ -212,7 +212,7 @@ public void delayAggrCounterSum() {

Assertions.assertEquals(1, payload.getMetrics().size());
Assertions.assertEquals(
new EvalPayload.Metric("sum", tags(), 6.0),
new EvalPayload.Metric("sum", tags("name", "unknown"), 6.0),
payload.getMetrics().get(0)
);
}
Expand All @@ -229,7 +229,7 @@ public void delayAggrGaugeSum() {
Assertions.assertEquals(3, payload.getMetrics().size());
for (EvalPayload.Metric m : payload.getMetrics()) {
Map<String, String> tags = m.getTags();
Assertions.assertEquals(1, tags.size());
Assertions.assertEquals(2, tags.size());
Assertions.assertTrue(tags.containsKey("atlas.aggr"));
}
}
Expand All @@ -247,7 +247,7 @@ public void delayAggrGaugeCount() {
Assertions.assertEquals(3, payload.getMetrics().size());
for (EvalPayload.Metric m : payload.getMetrics()) {
Map<String, String> tags = m.getTags();
Assertions.assertEquals(1, tags.size());
Assertions.assertEquals(2, tags.size());
Assertions.assertTrue(tags.containsKey("atlas.aggr"));
Assertions.assertEquals(1.0, m.getValue());
}
Expand Down Expand Up @@ -294,7 +294,7 @@ public void delayAggrGaugeMax() {

Assertions.assertEquals(1, payload.getMetrics().size());
Assertions.assertEquals(
new EvalPayload.Metric("max", tags(), 3.0),
new EvalPayload.Metric("max", tags("name", "unknown"), 3.0),
payload.getMetrics().get(0)
);
}
Expand Down

0 comments on commit 278395b

Please sign in to comment.