|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.gobblin.temporal.workflows.metrics; |
| 19 | + |
| 20 | +import java.time.Duration; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.HashMap; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.stream.Collectors; |
| 25 | + |
| 26 | +import org.apache.commons.lang.StringUtils; |
| 27 | +import org.jetbrains.annotations.NotNull; |
| 28 | + |
| 29 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 30 | +import com.google.common.annotations.VisibleForTesting; |
| 31 | +import com.typesafe.config.Config; |
| 32 | +import com.uber.m3.tally.StatsReporter; |
| 33 | + |
| 34 | +import io.micrometer.core.instrument.Clock; |
| 35 | +import io.micrometer.core.instrument.MeterRegistry; |
| 36 | +import io.micrometer.registry.otlp.OtlpConfig; |
| 37 | +import io.micrometer.registry.otlp.OtlpMeterRegistry; |
| 38 | +import io.temporal.common.reporter.MicrometerClientStatsReporter; |
| 39 | +import lombok.extern.slf4j.Slf4j; |
| 40 | + |
| 41 | +import org.apache.gobblin.temporal.GobblinTemporalConfigurationKeys; |
| 42 | +import org.apache.gobblin.util.ConfigUtils; |
| 43 | + |
| 44 | + |
| 45 | +@Slf4j |
| 46 | +public class TemporalMetricsHelper { |
| 47 | + |
| 48 | + /** |
| 49 | + * Retrieves a map of dimension names and their corresponding values from the provided config. |
| 50 | + * The dimensions are defined as a comma-separated string in the config, and the method |
| 51 | + * fetches the corresponding values for each dimension. |
| 52 | + * A missing dimension in config will have empty string as value. |
| 53 | + * |
| 54 | + * @param config Config object |
| 55 | + * @return a map where the key is the dimension name and the value is the corresponding value from the config |
| 56 | + */ |
| 57 | + public static Map<String, String> getDimensions(Config config) { |
| 58 | + String dimensionsString = ConfigUtils.getString(config, GobblinTemporalConfigurationKeys.TEMPORAL_METRICS_OTLP_DIMENSIONS_KEY, ""); |
| 59 | + |
| 60 | + // Split the string by "," and create a map by fetching values from config |
| 61 | + return Arrays.stream(dimensionsString.split(",")) |
| 62 | + .map(String::trim) |
| 63 | + .filter(StringUtils::isNotBlank) |
| 64 | + .collect(Collectors.toMap(key -> key, key -> ConfigUtils.getString(config, key, ""), (l, r)-> r)); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Creates and returns a {@link StatsReporter} instance configured with an {@link OtlpMeterRegistry}. |
| 69 | + * This reporter can be used to report metrics via the OpenTelemetry Protocol (OTLP) to a metrics backend. |
| 70 | + * |
| 71 | + * @param config Config object |
| 72 | + * @return a {@link StatsReporter} instance, configured with an OTLP meter registry and ready to report metrics. |
| 73 | + */ |
| 74 | + public static StatsReporter getStatsReporter(Config config) { |
| 75 | + OtlpConfig otlpConfig = getOtlpConfig(config); |
| 76 | + MeterRegistry meterRegistry = new OtlpMeterRegistry(otlpConfig, Clock.SYSTEM); |
| 77 | + return new MicrometerClientStatsReporter(meterRegistry); |
| 78 | + } |
| 79 | + |
| 80 | + @VisibleForTesting |
| 81 | + static OtlpConfig getOtlpConfig(Config config) { |
| 82 | + return new OtlpConfig() { |
| 83 | + @Override |
| 84 | + public String get(@NotNull String key) { |
| 85 | + return ConfigUtils.getString(config, key, null); |
| 86 | + } |
| 87 | + |
| 88 | + @NotNull |
| 89 | + @Override |
| 90 | + public String prefix() { |
| 91 | + return GobblinTemporalConfigurationKeys.TEMPORAL_METRICS_OTLP_PREFIX_WITHOUT_DOT; |
| 92 | + } |
| 93 | + |
| 94 | + @NotNull |
| 95 | + @Override |
| 96 | + public Map<String, String> headers() { |
| 97 | + String headers = get(GobblinTemporalConfigurationKeys.TEMPORAL_METRICS_OTLP_HEADERS_KEY); |
| 98 | + return parseHeaders(headers); |
| 99 | + } |
| 100 | + |
| 101 | + @NotNull |
| 102 | + @Override |
| 103 | + public Duration step() { |
| 104 | + int reportInterval = ConfigUtils.getInt(config, GobblinTemporalConfigurationKeys.TEMPORAL_METRICS_REPORT_INTERVAL_SECS, |
| 105 | + GobblinTemporalConfigurationKeys.DEFAULT_TEMPORAL_METRICS_REPORT_INTERVAL_SECS); |
| 106 | + return Duration.ofSeconds(reportInterval); |
| 107 | + } |
| 108 | + }; |
| 109 | + } |
| 110 | + |
| 111 | + private static Map<String, String> parseHeaders(String headersString) { |
| 112 | + try { |
| 113 | + ObjectMapper mapper = new ObjectMapper(); |
| 114 | + return mapper.readValue(headersString, HashMap.class); |
| 115 | + } catch (Exception e) { |
| 116 | + String errMsg = "Failed to parse headers: " + headersString; |
| 117 | + log.error(errMsg, e); |
| 118 | + throw new RuntimeException(errMsg); |
| 119 | + } |
| 120 | + } |
| 121 | +} |
0 commit comments