1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.metricsproxy.metric.model.json;
import ai.vespa.metricsproxy.http.yamas.YamasResponse;
import ai.vespa.metricsproxy.metric.model.MetricsPacket;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import static ai.vespa.metricsproxy.metric.model.ConsumerId.toConsumerId;
import static ai.vespa.metricsproxy.metric.model.MetricId.toMetricId;
import static ai.vespa.metricsproxy.metric.model.ServiceId.toServiceId;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Tests for YamasJsonModel and YamasArrayJsonModel
*
* @author smorgrav
* @author gjoranv
*/
public class YamasJsonModelTest {
private static final String EXPECTED_JSON = "{\"metrics\":[{\"timestamp\":1400047900,\"application\":\"vespa.searchnode\",\"metrics\":{\"cpu\":55.5555555555555,\"memory_virt\":22222222222,\"memory_rss\":5555555555},\"dimensions\":{\"applicationName\":\"app\",\"tenantName\":\"tenant\",\"metrictype\":\"system\",\"instance\":\"searchnode\",\"applicationInstance\":\"default\",\"clustername\":\"cluster\"},\"routing\":{\"yamas\":{\"namespaces\":[\"Vespa\"]}}}]}";
@Test
public void array_definition_creates_correct_json() throws IOException {
YamasJsonModel jsonModel = getYamasJsonModel("yamas-array.json");
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
YamasResponse response = new YamasResponse(200, List.of(YamasJsonUtil.toMetricsPacketBuilder(jsonModel).build()), false);
response.render(outputStream);
assertEquals(EXPECTED_JSON, outputStream.toString());
}
}
@Test
public void deserialize_serialize_roundtrip() throws IOException {
YamasJsonModel jsonModel = getYamasJsonModel("yamas-array.json");
// Do some sanity checking
assertEquals("vespa.searchnode", jsonModel.application);
assertTrue(jsonModel.routing.get("yamas").namespaces.contains("Vespa"));
assertEquals(5.555555555E9, jsonModel.metrics.get("memory_rss"), 0.1d); //Not using custom double renderer
// Serialize and verify
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
YamasResponse response = new YamasResponse(200, List.of(YamasJsonUtil.toMetricsPacketBuilder(jsonModel).build()), false);
response.render(outputStream);
assertEquals(EXPECTED_JSON, outputStream.toString());
}
}
@Test
public void deserialize_serialize_roundtrip_with_metrics_packet() throws IOException {
YamasJsonModel jsonModel = getYamasJsonModel("yamas-array.json");
MetricsPacket metricsPacket = YamasJsonUtil.toMetricsPacketBuilder(jsonModel).build();
// Do some sanity checking
assertEquals(toServiceId("vespa.searchnode"), metricsPacket.service);
assertTrue(metricsPacket.consumers().contains(toConsumerId("Vespa")));
assertEquals(5.555555555E9, metricsPacket.metrics().get(toMetricId("memory_rss")).doubleValue(), 0.1d); //Not using custom double rendrer
// Serialize and verify
String string = YamasJsonUtil.toJson(List.of(metricsPacket), false);
assertEquals(EXPECTED_JSON, string);
}
@Test
public void missing_routing_object_makes_it_null() throws IOException {
// Read file that was taken from production (real -life example that is)
String filename = getClass().getClassLoader().getResource("yamas-array-no-routing.json").getFile();
BufferedReader reader = Files.newBufferedReader(Paths.get(filename));
ObjectMapper mapper = new ObjectMapper();
YamasJsonModel jsonModel = mapper.readValue(reader, YamasJsonModel.class);
// Do some sanity checking
assertNull(jsonModel.routing);
}
@Test
public void creates_correct_jsonl() throws IOException {
YamasJsonModel jsonModel = getYamasJsonModel("yamas-array.json");
MetricsPacket packet = YamasJsonUtil.toMetricsPacketBuilder(jsonModel).build();
// Add packet twice to verify object delimiter
List<MetricsPacket> metricPackets = List.of(packet, packet);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
YamasResponse response = new YamasResponse(200, metricPackets, true);
response.render(outputStream);
assertEquals("""
{"timestamp":1400047900,"application":"vespa.searchnode","metrics":{"cpu":55.5555555555555,"memory_virt":22222222222,"memory_rss":5555555555},"dimensions":{"applicationName":"app","tenantName":"tenant","metrictype":"system","instance":"searchnode","applicationInstance":"default","clustername":"cluster"},"routing":{"yamas":{"namespaces":["Vespa"]}}}
{"timestamp":1400047900,"application":"vespa.searchnode","metrics":{"cpu":55.5555555555555,"memory_virt":22222222222,"memory_rss":5555555555},"dimensions":{"applicationName":"app","tenantName":"tenant","metrictype":"system","instance":"searchnode","applicationInstance":"default","clustername":"cluster"},"routing":{"yamas":{"namespaces":["Vespa"]}}}""",
outputStream.toString());
}
}
private YamasJsonModel getYamasJsonModel(String testFile) throws IOException {
String filename = getClass().getClassLoader().getResource(testFile).getFile();
BufferedReader reader = Files.newBufferedReader(Paths.get(filename));
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(reader, YamasJsonModel.class);
}
}
|