Add API for metrics based on Micrometer (#1576)

This commit is contained in:
Sebastian Sdorra
2021-03-10 10:07:29 +01:00
committed by GitHub
parent aa15227f0a
commit 7656c2dc14
16 changed files with 902 additions and 1 deletions

View File

@@ -0,0 +1,226 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.api.v2.resources;
import com.google.common.collect.ImmutableSet;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.metrics.MonitoringSystem;
import sonia.scm.metrics.ScrapeTarget;
import javax.inject.Provider;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
class MetricsIndexEnricherTest {
@Mock
private HalEnricherContext context;
@Mock
private HalAppender appender;
@Mock
private Subject subject;
private Provider<ResourceLinks> resourceLinks;
@BeforeEach
void setUpResourceLinks() {
ScmPathInfoStore scmPathInfoStore = new ScmPathInfoStore();
scmPathInfoStore.set(() -> URI.create("/"));
resourceLinks = () -> new ResourceLinks(scmPathInfoStore);
}
@BeforeEach
void setUpSubject() {
ThreadContext.bind(subject);
}
@AfterEach
void tearDownSubject() {
ThreadContext.unbindSubject();
}
@Nested
class WithPermission {
@BeforeEach
void prepareSubject() {
when(subject.isPermitted("metrics:read")).thenReturn(true);
}
@Test
void shouldNotEnrichWithoutMonitoringSystems() {
MetricsIndexEnricher enricher = new MetricsIndexEnricher(
resourceLinks,
Collections.emptySet()
);
enricher.enrich(context, appender);
verify(appender, never()).linkArrayBuilder("metrics");
}
@Test
void shouldNotEnrichWithMonitoringSystemsWithScrapeTarget() {
MetricsIndexEnricher enricher = new MetricsIndexEnricher(
resourceLinks,
Collections.singleton(new NoScrapeMonitoringSystem())
);
enricher.enrich(context, appender);
verify(appender, never()).linkArrayBuilder("metrics");
}
@Test
void shouldEnrichIndex() {
CapturingLinkArrayBuilder linkBuilder = new CapturingLinkArrayBuilder();
when(appender.linkArrayBuilder("metrics")).thenReturn(linkBuilder);
MetricsIndexEnricher enricher = new MetricsIndexEnricher(
resourceLinks,
ImmutableSet.of(
new NoScrapeMonitoringSystem(),
new ScrapeMonitoringSystem("one"),
new ScrapeMonitoringSystem("two"))
);
enricher.enrich(context, appender);
assertThat(linkBuilder.buildWasCalled).isTrue();
assertThat(linkBuilder.links)
.containsEntry("one", "/v2/metrics/one")
.containsEntry("two", "/v2/metrics/two")
.hasSize(2);
}
}
@Nested
class WithoutPermission {
@BeforeEach
void prepareSubject() {
when(subject.isPermitted("metrics:read")).thenReturn(false);
}
@Test
void shouldNotEnrichWithoutPermission() {
MetricsIndexEnricher enricher = new MetricsIndexEnricher(
resourceLinks,
ImmutableSet.of(
new ScrapeMonitoringSystem("one")
)
);
enricher.enrich(context, appender);
verify(appender, never()).linkArrayBuilder("metrics");
}
}
private static class NoScrapeMonitoringSystem implements MonitoringSystem {
@Override
public String getName() {
return "noscrap";
}
@Override
public MeterRegistry getRegistry() {
return new SimpleMeterRegistry();
}
}
private static class ScrapeMonitoringSystem implements MonitoringSystem {
private final String type;
private ScrapeMonitoringSystem(String type) {
this.type = type;
}
@Override
public String getName() {
return type;
}
@Override
public MeterRegistry getRegistry() {
return new SimpleMeterRegistry();
}
@Override
public Optional<ScrapeTarget> getScrapeTarget() {
return Optional.of(new NoopScrapeTarget());
}
}
private static class NoopScrapeTarget implements ScrapeTarget {
@Override
public String getContentType() {
return null;
}
@Override
public void write(OutputStream outputStream) throws IOException {
}
}
private static class CapturingLinkArrayBuilder implements HalAppender.LinkArrayBuilder {
private final Map<String, String> links = new HashMap<>();
private boolean buildWasCalled = false;
@Override
public HalAppender.LinkArrayBuilder append(String name, String href) {
links.put(name, href);
return this;
}
@Override
public void build() {
buildWasCalled = true;
}
}
}

View File

@@ -0,0 +1,162 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.api.v2.resources;
import com.google.common.collect.ImmutableSet;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ThreadContext;
import org.jboss.resteasy.mock.MockHttpRequest;
import org.jboss.resteasy.mock.MockHttpResponse;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import sonia.scm.metrics.MonitoringSystem;
import sonia.scm.metrics.ScrapeTarget;
import sonia.scm.web.RestDispatcher;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doThrow;
@ExtendWith(MockitoExtension.class)
class MetricsResourceTest {
private RestDispatcher dispatcher;
@Mock
private Subject subject;
@BeforeEach
void setUpDispatcher() {
dispatcher = new RestDispatcher();
dispatcher.addSingletonResource(new MetricsResource(ImmutableSet.of(
new NoScrapeMonitoringSystem(),
new ScrapeMonitoringSystem()
)));
}
@BeforeEach
void setUpSubject() {
ThreadContext.bind(subject);
}
@AfterEach
void tearDownSubject() {
ThreadContext.unbindSubject();
}
@Test
void shouldReturn404ForUnknownMonitoringSystem() throws URISyntaxException {
MockHttpRequest request = MockHttpRequest.get("/v2/metrics/unknown");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_FOUND);
}
@Test
void shouldReturn404ForMonitoringSystemWithoutScrapeTarget() throws URISyntaxException {
MockHttpRequest request = MockHttpRequest.get("/v2/metrics/noscrape");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_NOT_FOUND);
}
@Test
void shouldReturn403WithoutPermission() throws URISyntaxException {
doThrow(new AuthorizationException("not permitted")).when(subject).checkPermission("metrics:read");
MockHttpRequest request = MockHttpRequest.get("/v2/metrics/scrape");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_FORBIDDEN);
}
@Test
void shouldReturnMetrics() throws URISyntaxException, UnsupportedEncodingException {
MockHttpRequest request = MockHttpRequest.get("/v2/metrics/scrape");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_OK);
assertThat(response.getOutputHeaders().getFirst("Content-Type").toString()).hasToString("text/plain;charset=UTF-8");
assertThat(response.getContentAsString()).isEqualTo("hello");
}
private static class NoScrapeMonitoringSystem implements MonitoringSystem {
@Override
public String getName() {
return "noscrape";
}
@Override
public MeterRegistry getRegistry() {
return new SimpleMeterRegistry();
}
}
private static class ScrapeMonitoringSystem implements MonitoringSystem {
@Override
public String getName() {
return "scrape";
}
@Override
public MeterRegistry getRegistry() {
return new SimpleMeterRegistry();
}
@Override
public Optional<ScrapeTarget> getScrapeTarget() {
return Optional.of(new HelloScrapeTarget());
}
}
private static class HelloScrapeTarget implements ScrapeTarget {
@Override
public String getContentType() {
return "text/plain";
}
@Override
public void write(OutputStream outputStream) throws IOException {
outputStream.write("hello".getBytes(StandardCharsets.UTF_8));
}
}
}

View File

@@ -0,0 +1,96 @@
/*
* MIT License
*
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package sonia.scm.metrics;
import com.google.common.collect.ImmutableSet;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.junit.jupiter.api.Test;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
class MeterRegistryProviderTest {
@Test
void shouldReturnNoopRegistry() {
MeterRegistryProvider provider = new MeterRegistryProvider(Collections.emptySet());
MeterRegistry registry = provider.get();
registry.counter("sample").increment();
assertThat(registry.get("sample").counter().count()).isEqualTo(0.0);
}
@Test
void shouldNotRegisterCommonMetricsForDefaultRegistry() {
MeterRegistryProvider provider = new MeterRegistryProvider(Collections.emptySet());
assertThat(provider.get().getMeters()).withFailMessage("no common metrics should be registered").isEmpty();
}
@Test
void shouldStoreMetrics() {
MeterRegistryProvider provider = new MeterRegistryProvider(Collections.singleton(new SimpleMonitoringSystem()));
MeterRegistry registry = provider.get();
registry.counter("sample").increment();
assertThat(registry.get("sample").counter().count()).isEqualTo(1.0);
}
@Test
void shouldRegisterCommonMetrics() {
MeterRegistryProvider provider = new MeterRegistryProvider(Collections.singleton(new SimpleMonitoringSystem()));
MeterRegistry registry = provider.get();
assertThat(registry.getMeters()).isNotEmpty();
}
@Test
void shouldCreateCompositeMeterRegistry() {
MeterRegistryProvider provider = new MeterRegistryProvider(
ImmutableSet.of(new SimpleMonitoringSystem(), new SimpleMonitoringSystem())
);
assertThat(provider.get()).isInstanceOf(CompositeMeterRegistry.class);
}
@Test
void shouldReturnSingleRegistryUnwrapped() {
MeterRegistryProvider provider = new MeterRegistryProvider(Collections.singleton(new SimpleMonitoringSystem()));
assertThat(provider.get()).isInstanceOf(SimpleMeterRegistry.class);
}
private static class SimpleMonitoringSystem implements MonitoringSystem {
@Override
public String getName() {
return "simple";
}
@Override
public MeterRegistry getRegistry() {
return new SimpleMeterRegistry();
}
}
}