|
| 1 | +/* |
| 2 | + * Copyright 2025 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.stackdriver; |
| 17 | + |
| 18 | +import io.micrometer.common.util.internal.logging.InternalLogLevel; |
| 19 | +import io.micrometer.common.util.internal.logging.MockLogger; |
| 20 | +import io.micrometer.common.util.internal.logging.MockLoggerFactory; |
| 21 | +import io.micrometer.core.instrument.Clock; |
| 22 | +import io.micrometer.core.instrument.DistributionSummary; |
| 23 | +import io.micrometer.core.instrument.Timer; |
| 24 | +import org.junit.jupiter.api.Tag; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | + |
| 27 | +import java.time.Duration; |
| 28 | + |
| 29 | +import static org.assertj.core.api.Assertions.assertThat; |
| 30 | + |
| 31 | +/** |
| 32 | + * When running this integration test locally, authentication can be done by configuring |
| 33 | + * the env var GOOGLE_APPLICATION_CREDENTIALS with the location of the service key JSON |
| 34 | + * file. |
| 35 | + */ |
| 36 | +@Tag("gcp-it") |
| 37 | +class StackdriverIntegrationTest { |
| 38 | + |
| 39 | + static final MockLoggerFactory LOGGER_FACTORY = new MockLoggerFactory(); |
| 40 | + |
| 41 | + MockLogger mockLogger = LOGGER_FACTORY.getLogger(StackdriverMeterRegistry.class); |
| 42 | + |
| 43 | + static final StackdriverConfig CONFIG = new StackdriverConfig() { |
| 44 | + @Override |
| 45 | + public String projectId() { |
| 46 | + return "micrometer-gcp"; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public String get(String key) { |
| 51 | + return null; |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + StackdriverMeterRegistry registry = LOGGER_FACTORY |
| 56 | + .injectLogger(() -> new StackdriverMeterRegistry(CONFIG, Clock.SYSTEM)); |
| 57 | + |
| 58 | + StackdriverMeterRegistry.Batch batch = registry.new Batch(); |
| 59 | + |
| 60 | + void publishAndVerify() { |
| 61 | + registry.close(); |
| 62 | + assertThat(mockLogger.getLogEvents() |
| 63 | + .stream() |
| 64 | + .filter(log -> log.getLevel() == InternalLogLevel.WARN || log.getLevel() == InternalLogLevel.ERROR)) |
| 65 | + .as("There should be no WARN or ERROR logs if metrics publish to Stackdriver successfully") |
| 66 | + .isEmpty(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + void timerWithSlos() { |
| 71 | + Timer slos = Timer.builder("micrometer.stackdriver.it.timer.slo") |
| 72 | + .maximumExpectedValue(Duration.ofMillis(2)) |
| 73 | + .serviceLevelObjectives(Duration.ofMillis(1), Duration.ofMillis(2)) |
| 74 | + .register(registry); |
| 75 | + slos.record(Duration.ofMillis(1)); |
| 76 | + slos.record(Duration.ofMillis(2)); |
| 77 | + slos.record(Duration.ofMillis(3)); |
| 78 | + |
| 79 | + System.out.println(slos.getId().toString() + batch.distribution(slos.takeSnapshot(), true)); |
| 80 | + publishAndVerify(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + void timerWithPercentileHistogram() { |
| 85 | + Timer percentileHistogram = Timer.builder("micrometer.stackdriver.it.timer.percentilehistogram") |
| 86 | + .publishPercentileHistogram() |
| 87 | + .register(registry); |
| 88 | + |
| 89 | + percentileHistogram.record(Duration.ofMillis(1)); |
| 90 | + percentileHistogram.record(Duration.ofMillis(2)); |
| 91 | + percentileHistogram.record(Duration.ofMillis(3)); |
| 92 | + |
| 93 | + System.out.println( |
| 94 | + percentileHistogram.getId().toString() + batch.distribution(percentileHistogram.takeSnapshot(), true)); |
| 95 | + publishAndVerify(); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void timerWithClientPercentiles() { |
| 100 | + Timer clientPercentiles = Timer.builder("micrometer.stackdriver.it.timer.clientpercentiles") |
| 101 | + .publishPercentiles(0.5, 0.99) |
| 102 | + .register(registry); |
| 103 | + clientPercentiles.record(Duration.ofMillis(1)); |
| 104 | + clientPercentiles.record(Duration.ofMillis(2)); |
| 105 | + clientPercentiles.record(Duration.ofMillis(3)); |
| 106 | + |
| 107 | + System.out |
| 108 | + .println(clientPercentiles.getId().toString() + batch.distribution(clientPercentiles.takeSnapshot(), true)); |
| 109 | + publishAndVerify(); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + void timerWithClientPercentilesAndSlo() { |
| 114 | + Timer clientPercentilesAndSlos = Timer.builder("micrometer.stackdriver.it.timer.clientpercentilesandslo") |
| 115 | + .serviceLevelObjectives(Duration.ofMillis(2)) |
| 116 | + .publishPercentiles(0.5, 0.99) |
| 117 | + .register(registry); |
| 118 | + clientPercentilesAndSlos.record(Duration.ofMillis(1)); |
| 119 | + clientPercentilesAndSlos.record(Duration.ofMillis(2)); |
| 120 | + clientPercentilesAndSlos.record(Duration.ofMillis(3)); |
| 121 | + |
| 122 | + System.out.println(clientPercentilesAndSlos.getId().toString() |
| 123 | + + batch.distribution(clientPercentilesAndSlos.takeSnapshot(), true)); |
| 124 | + publishAndVerify(); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + void summary() { |
| 129 | + DistributionSummary summary = DistributionSummary.builder("micrometer.stackdriver.it.summary") |
| 130 | + .register(registry); |
| 131 | + summary.record(10d); |
| 132 | + summary.record(20d); |
| 133 | + summary.record(30d); |
| 134 | + |
| 135 | + System.out.println(summary.getId().toString() + batch.distribution(summary.takeSnapshot(), false)); |
| 136 | + publishAndVerify(); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void summaryWithOneSlo() { |
| 141 | + DistributionSummary oneSlo = DistributionSummary.builder("micrometer.stackdriver.it.summaryoneslo") |
| 142 | + .serviceLevelObjectives(20) |
| 143 | + .register(registry); |
| 144 | + oneSlo.record(10d); |
| 145 | + oneSlo.record(20d); |
| 146 | + oneSlo.record(30d); |
| 147 | + |
| 148 | + System.out.println(oneSlo.getId().toString() + batch.distribution(oneSlo.takeSnapshot(), false)); |
| 149 | + publishAndVerify(); |
| 150 | + } |
| 151 | + |
| 152 | +} |
0 commit comments