MetricsManager { public List<MetricsPacket> getMetrics(List<VespaService> services, Instant startTime) { return getMetricsAsBuilders(services, startTime).stream() .map(MetricsPacket.Builder::build) .collect(Collectors.toList()); } MetricsManager(VespaServices vespaServices,
                          VespaMetrics vespaMetrics,
                          ExternalMetrics externalMetrics,
                          ApplicationDimensions applicationDimensions,
                          NodeDimensions nodeDimensions); String getMetricNamesForServiceAndConsumer(String service, ConsumerId consumer); String getMetricsByConfigId(String configId); List<MetricsPacket> getMetrics(List<VespaService> services, Instant startTime); List<MetricsPacket.Builder> getMetricsAsBuilders(List<VespaService> services, Instant startTime); List<MetricsPacket> getHealthMetrics(List<VespaService> services); void setExtraMetrics(List<MetricsPacket.Builder> packets); Map<DimensionId, String> getExtraDimensions(); void purgeExtraMetrics(); String getAllVespaServices();  }
@Test public void service_that_is_down_has_a_separate_metrics_packet() { var downService = new DownService(HealthMetric.getDown("No response")); List<VespaService> testServices = Collections.singletonList(downService); MetricsManager metricsManager = TestUtil.createMetricsManager(new VespaServices(testServices), getMetricsConsumers(),getApplicationDimensions(), getNodeDimensions()); List<MetricsPacket> packets = metricsManager.getMetrics(testServices, Instant.EPOCH); assertThat(packets.size(), is(1)); assertTrue(packets.get(0).metrics().isEmpty()); assertThat(packets.get(0).dimensions().get(toDimensionId("instance")), is(DownService.NAME)); assertThat(packets.get(0).dimensions().get(toDimensionId("global")), is("value")); }
@Test public void each_service_gets_separate_metrics_packets() { List<MetricsPacket> packets = metricsManager.getMetrics(testServices, Instant.EPOCH); assertThat(packets.size(), is(2)); assertThat(packets.get(0).dimensions().get(toDimensionId("instance")), is("dummy0")); assertThat(packets.get(0).metrics().get(toMetricId("c.test")), is(1.0)); assertThat(packets.get(0).metrics().get(toMetricId("val")), is(1.05)); assertThat(packets.get(1).dimensions().get(toDimensionId("instance")), is("dummy1")); assertThat(packets.get(1).metrics().get(toMetricId("c.test")), is(6.0)); assertThat(packets.get(1).metrics().get(toMetricId("val")), is(2.35)); }
@Test public void global_dimensions_are_added_but_do_not_override_metric_dimensions() { List<MetricsPacket> packets = metricsManager.getMetrics(testServices, Instant.EPOCH); assertEquals(2, packets.size()); assertGlobalDimensions(packets.get(0).dimensions()); assertGlobalDimensions(packets.get(1).dimensions()); }
@Test public void system_metrics_are_added() { VespaService service0 = testServices.get(0); Metrics oldSystemMetrics = service0.getSystemMetrics(); service0.getSystemMetrics().add(new Metric("cpu", 1)); List<MetricsPacket> packets = metricsManager.getMetrics(testServices, Instant.EPOCH); assertEquals(3, packets.size()); MetricsPacket systemPacket = packets.get(0); assertThat(systemPacket.metrics().get(toMetricId("cpu")), is(1.0)); assertThat(systemPacket.dimensions().get(toDimensionId("metrictype")), is("system")); service0.setSystemMetrics(oldSystemMetrics); }