|
| 1 | +/* |
| 2 | + * Copyright 2020-2021 the original author or authors. |
| 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 | + |
| 17 | +package org.springframework.boot.actuate.metrics.graphql; |
| 18 | + |
| 19 | +import java.util.concurrent.CompletionStage; |
| 20 | +import java.util.concurrent.atomic.AtomicLong; |
| 21 | + |
| 22 | +import graphql.ExecutionResult; |
| 23 | +import graphql.execution.instrumentation.InstrumentationContext; |
| 24 | +import graphql.execution.instrumentation.InstrumentationState; |
| 25 | +import graphql.execution.instrumentation.SimpleInstrumentation; |
| 26 | +import graphql.execution.instrumentation.SimpleInstrumentationContext; |
| 27 | +import graphql.execution.instrumentation.parameters.InstrumentationExecutionParameters; |
| 28 | +import graphql.execution.instrumentation.parameters.InstrumentationFieldFetchParameters; |
| 29 | +import graphql.schema.DataFetcher; |
| 30 | +import io.micrometer.core.instrument.DistributionSummary; |
| 31 | +import io.micrometer.core.instrument.MeterRegistry; |
| 32 | +import io.micrometer.core.instrument.Tag; |
| 33 | +import io.micrometer.core.instrument.Timer; |
| 34 | + |
| 35 | +import org.springframework.boot.actuate.metrics.AutoTimer; |
| 36 | +import org.springframework.lang.Nullable; |
| 37 | + |
| 38 | +public class GraphQlMetricsInstrumentation extends SimpleInstrumentation { |
| 39 | + |
| 40 | + private final MeterRegistry registry; |
| 41 | + |
| 42 | + private final GraphQlTagsProvider tagsProvider; |
| 43 | + |
| 44 | + private final AutoTimer autoTimer; |
| 45 | + |
| 46 | + private final DistributionSummary dataFetchingSummary; |
| 47 | + |
| 48 | + public GraphQlMetricsInstrumentation(MeterRegistry registry, GraphQlTagsProvider tagsProvider, |
| 49 | + AutoTimer autoTimer) { |
| 50 | + this.registry = registry; |
| 51 | + this.tagsProvider = tagsProvider; |
| 52 | + this.autoTimer = autoTimer; |
| 53 | + this.dataFetchingSummary = DistributionSummary.builder("graphql.request.datafetch.count").baseUnit("calls") |
| 54 | + .description("Count of DataFetcher calls per request.").register(this.registry); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public InstrumentationState createState() { |
| 59 | + return new RequestMetricsInstrumentationState(this.autoTimer, this.registry); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public InstrumentationContext<ExecutionResult> beginExecution(InstrumentationExecutionParameters parameters) { |
| 64 | + if (this.autoTimer.isEnabled()) { |
| 65 | + RequestMetricsInstrumentationState state = parameters.getInstrumentationState(); |
| 66 | + state.startTimer(); |
| 67 | + return new SimpleInstrumentationContext<ExecutionResult>() { |
| 68 | + @Override |
| 69 | + public void onCompleted(ExecutionResult result, Throwable exc) { |
| 70 | + Iterable<Tag> tags = GraphQlMetricsInstrumentation.this.tagsProvider.getExecutionTags(parameters, |
| 71 | + result, exc); |
| 72 | + state.tags(tags).stopTimer(); |
| 73 | + if (!result.getErrors().isEmpty()) { |
| 74 | + result.getErrors() |
| 75 | + .forEach((error) -> GraphQlMetricsInstrumentation.this.registry.counter("graphql.error", |
| 76 | + GraphQlMetricsInstrumentation.this.tagsProvider.getErrorTags(parameters, error)) |
| 77 | + .increment()); |
| 78 | + } |
| 79 | + GraphQlMetricsInstrumentation.this.dataFetchingSummary.record(state.getDataFetchingCount()); |
| 80 | + } |
| 81 | + }; |
| 82 | + } |
| 83 | + return super.beginExecution(parameters); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public DataFetcher<?> instrumentDataFetcher(DataFetcher<?> dataFetcher, |
| 88 | + InstrumentationFieldFetchParameters parameters) { |
| 89 | + if (this.autoTimer.isEnabled() && !parameters.isTrivialDataFetcher()) { |
| 90 | + return (environment) -> { |
| 91 | + Timer.Sample sample = Timer.start(this.registry); |
| 92 | + try { |
| 93 | + Object value = dataFetcher.get(environment); |
| 94 | + if (value instanceof CompletionStage<?>) { |
| 95 | + CompletionStage<?> completion = (CompletionStage<?>) value; |
| 96 | + return completion.whenComplete( |
| 97 | + (result, error) -> recordDataFetcherMetric(sample, dataFetcher, parameters, error)); |
| 98 | + } |
| 99 | + else { |
| 100 | + recordDataFetcherMetric(sample, dataFetcher, parameters, null); |
| 101 | + return value; |
| 102 | + } |
| 103 | + } |
| 104 | + catch (Throwable throwable) { |
| 105 | + recordDataFetcherMetric(sample, dataFetcher, parameters, throwable); |
| 106 | + throw throwable; |
| 107 | + } |
| 108 | + }; |
| 109 | + } |
| 110 | + return super.instrumentDataFetcher(dataFetcher, parameters); |
| 111 | + } |
| 112 | + |
| 113 | + private void recordDataFetcherMetric(Timer.Sample sample, DataFetcher<?> dataFetcher, |
| 114 | + InstrumentationFieldFetchParameters parameters, @Nullable Throwable throwable) { |
| 115 | + Timer.Builder timer = this.autoTimer.builder("graphql.datafetcher"); |
| 116 | + timer.tags(this.tagsProvider.getDataFetchingTags(dataFetcher, parameters, throwable)); |
| 117 | + sample.stop(timer.register(this.registry)); |
| 118 | + RequestMetricsInstrumentationState state = parameters.getInstrumentationState(); |
| 119 | + state.incrementDataFetchingCount(); |
| 120 | + } |
| 121 | + |
| 122 | + static class RequestMetricsInstrumentationState implements InstrumentationState { |
| 123 | + |
| 124 | + private final MeterRegistry registry; |
| 125 | + |
| 126 | + private final Timer.Builder timer; |
| 127 | + |
| 128 | + private Timer.Sample sample; |
| 129 | + |
| 130 | + private AtomicLong dataFetchingCount = new AtomicLong(0L); |
| 131 | + |
| 132 | + RequestMetricsInstrumentationState(AutoTimer autoTimer, MeterRegistry registry) { |
| 133 | + this.timer = autoTimer.builder("graphql.request"); |
| 134 | + this.registry = registry; |
| 135 | + } |
| 136 | + |
| 137 | + RequestMetricsInstrumentationState tags(Iterable<Tag> tags) { |
| 138 | + this.timer.tags(tags); |
| 139 | + return this; |
| 140 | + } |
| 141 | + |
| 142 | + void startTimer() { |
| 143 | + this.sample = Timer.start(this.registry); |
| 144 | + } |
| 145 | + |
| 146 | + void stopTimer() { |
| 147 | + this.sample.stop(this.timer.register(this.registry)); |
| 148 | + } |
| 149 | + |
| 150 | + void incrementDataFetchingCount() { |
| 151 | + this.dataFetchingCount.incrementAndGet(); |
| 152 | + } |
| 153 | + |
| 154 | + long getDataFetchingCount() { |
| 155 | + return this.dataFetchingCount.get(); |
| 156 | + } |
| 157 | + |
| 158 | + } |
| 159 | + |
| 160 | +} |
0 commit comments