|
| 1 | +/* |
| 2 | + * Copyright 2023 VMware, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.micrometer.osgi.test; |
| 17 | + |
| 18 | +import io.micrometer.core.instrument.MeterRegistry; |
| 19 | +import io.micrometer.core.instrument.Metrics; |
| 20 | +import io.micrometer.core.instrument.MockClock; |
| 21 | +import io.micrometer.core.instrument.composite.CompositeMeterRegistry; |
| 22 | +import io.micrometer.jmx.JmxConfig; |
| 23 | +import io.micrometer.jmx.JmxMeterRegistry; |
| 24 | +import io.micrometer.prometheus.PrometheusConfig; |
| 25 | +import io.micrometer.prometheus.PrometheusMeterRegistry; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 28 | +import org.osgi.framework.Bundle; |
| 29 | +import org.osgi.framework.BundleContext; |
| 30 | +import org.osgi.framework.FrameworkUtil; |
| 31 | +import org.osgi.test.junit5.context.BundleContextExtension; |
| 32 | +import org.osgi.test.junit5.service.ServiceExtension; |
| 33 | + |
| 34 | +import java.time.Duration; |
| 35 | + |
| 36 | +import static org.assertj.core.api.Assertions.assertThat; |
| 37 | + |
| 38 | +@ExtendWith({ ServiceExtension.class, BundleContextExtension.class }) |
| 39 | +class OsgiTest { |
| 40 | + |
| 41 | + private final BundleContext context = FrameworkUtil.getBundle(this.getClass()).getBundleContext(); |
| 42 | + |
| 43 | + private final MockClock clock = new MockClock(); |
| 44 | + |
| 45 | + @Test |
| 46 | + void testCoreResolves() { |
| 47 | + Bundle bundle = context.getBundle(); |
| 48 | + assertThat(bundle).isNotNull(); |
| 49 | + |
| 50 | + CompositeMeterRegistry registry = Metrics.globalRegistry; |
| 51 | + assertThat(registry).isNotNull(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void testPrometheusMeterRegistryResolves() { |
| 56 | + PrometheusMeterRegistry registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT); |
| 57 | + testMetrics(registry); |
| 58 | + assertThat(registry.scrape()).contains("micrometer_test_counter").contains(" micrometer_test_timer"); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void testJmxMeterRegistryResolves() { |
| 63 | + JmxMeterRegistry registry = new JmxMeterRegistry(JmxConfig.DEFAULT, clock); |
| 64 | + testMetrics(registry); |
| 65 | + } |
| 66 | + |
| 67 | + private void testMetrics(MeterRegistry registry) { |
| 68 | + registry.counter("micrometer.test.counter").increment(); |
| 69 | + registry.timer("micrometer.test.timer").record(Duration.ofMillis(123)); |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments