|
| 1 | +/* |
| 2 | + * Copyright 2020-2025 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.graphql.observation; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Locale; |
| 21 | + |
| 22 | +import io.micrometer.common.KeyValue; |
| 23 | +import io.micrometer.common.KeyValues; |
| 24 | + |
| 25 | +import org.springframework.graphql.observation.GraphQlObservationDocumentation.DataLoaderHighCardinalityKeyNames; |
| 26 | +import org.springframework.graphql.observation.GraphQlObservationDocumentation.DataLoaderLowCardinalityKeyNames; |
| 27 | + |
| 28 | +/** |
| 29 | + * Default implementation for a {@link DataLoaderObservationConvention} |
| 30 | + * extracting information from a {@link DataLoaderObservationContext}. |
| 31 | + * |
| 32 | + * @author Brian Clozel |
| 33 | + * @since 1.4.0 |
| 34 | + */ |
| 35 | +public class DefaultDataLoaderObservationConvention implements DataLoaderObservationConvention { |
| 36 | + |
| 37 | + private static final String DEFAULT_NAME = "graphql.dataloader"; |
| 38 | + |
| 39 | + private static final KeyValue ERROR_TYPE_NONE = KeyValue.of(DataLoaderLowCardinalityKeyNames.ERROR_TYPE, "NONE"); |
| 40 | + |
| 41 | + private static final KeyValue LOADER_TYPE_UNKNOWN = KeyValue.of(DataLoaderLowCardinalityKeyNames.LOADER_TYPE, "unknown"); |
| 42 | + |
| 43 | + private static final KeyValue OUTCOME_SUCCESS = KeyValue.of(DataLoaderLowCardinalityKeyNames.OUTCOME, "SUCCESS"); |
| 44 | + |
| 45 | + private static final KeyValue OUTCOME_ERROR = KeyValue.of(DataLoaderLowCardinalityKeyNames.OUTCOME, "ERROR"); |
| 46 | + |
| 47 | + @Override |
| 48 | + public String getName() { |
| 49 | + return DEFAULT_NAME; |
| 50 | + } |
| 51 | + |
| 52 | + @Override |
| 53 | + public String getContextualName(DataLoaderObservationContext context) { |
| 54 | + List<?> result = context.getResult(); |
| 55 | + if (result.isEmpty()) { |
| 56 | + return "graphql dataloader"; |
| 57 | + } |
| 58 | + else { |
| 59 | + return "graphql dataloader " + result.get(0).getClass().getSimpleName().toLowerCase(Locale.ROOT); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public KeyValues getLowCardinalityKeyValues(DataLoaderObservationContext context) { |
| 65 | + return KeyValues.of(errorType(context), loaderType(context), outcome(context)); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public KeyValues getHighCardinalityKeyValues(DataLoaderObservationContext context) { |
| 70 | + return KeyValues.of(loaderSize(context)); |
| 71 | + } |
| 72 | + |
| 73 | + protected KeyValue errorType(DataLoaderObservationContext context) { |
| 74 | + if (context.getError() != null) { |
| 75 | + return KeyValue.of(DataLoaderLowCardinalityKeyNames.ERROR_TYPE, context.getError().getClass().getSimpleName()); |
| 76 | + } |
| 77 | + return ERROR_TYPE_NONE; |
| 78 | + } |
| 79 | + |
| 80 | + protected KeyValue loaderType(DataLoaderObservationContext context) { |
| 81 | + if (context.getResult().isEmpty()) { |
| 82 | + return LOADER_TYPE_UNKNOWN; |
| 83 | + } |
| 84 | + return KeyValue.of(DataLoaderLowCardinalityKeyNames.LOADER_TYPE, context.getResult().get(0).getClass().getSimpleName()); |
| 85 | + } |
| 86 | + |
| 87 | + protected KeyValue outcome(DataLoaderObservationContext context) { |
| 88 | + if (context.getError() != null) { |
| 89 | + return OUTCOME_ERROR; |
| 90 | + } |
| 91 | + return OUTCOME_SUCCESS; |
| 92 | + } |
| 93 | + |
| 94 | + protected KeyValue loaderSize(DataLoaderObservationContext context) { |
| 95 | + return KeyValue.of(DataLoaderHighCardinalityKeyNames.LOADER_SIZE, String.valueOf(context.getResult().size())); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments