|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.spark.k8s.operator.metrics; |
| 21 | + |
| 22 | +import java.util.Map; |
| 23 | +import java.util.concurrent.ConcurrentHashMap; |
| 24 | + |
| 25 | +import com.codahale.metrics.Counter; |
| 26 | +import com.codahale.metrics.Gauge; |
| 27 | +import com.codahale.metrics.Histogram; |
| 28 | +import com.codahale.metrics.MetricRegistry; |
| 29 | +import com.codahale.metrics.Timer; |
| 30 | +import lombok.RequiredArgsConstructor; |
| 31 | + |
| 32 | +@RequiredArgsConstructor |
| 33 | +public class BaseOperatorSource { |
| 34 | + protected final MetricRegistry metricRegistry; |
| 35 | + protected final Map<String, Histogram> histograms = new ConcurrentHashMap<>(); |
| 36 | + protected final Map<String, Counter> counters = new ConcurrentHashMap<>(); |
| 37 | + protected final Map<String, Gauge<?>> gauges = new ConcurrentHashMap<>(); |
| 38 | + protected final Map<String, Timer> timers = new ConcurrentHashMap<>(); |
| 39 | + |
| 40 | + protected Histogram getHistogram(String metricNamePrefix, String... names) { |
| 41 | + Histogram histogram; |
| 42 | + String metricName = MetricRegistry.name(metricNamePrefix, names).toLowerCase(); |
| 43 | + if (histograms.containsKey(metricName)) { |
| 44 | + histogram = histograms.get(metricName); |
| 45 | + } else { |
| 46 | + histogram = metricRegistry.histogram(metricName); |
| 47 | + histograms.put(metricName, histogram); |
| 48 | + } |
| 49 | + return histogram; |
| 50 | + } |
| 51 | + |
| 52 | + protected Counter getCounter(String metricNamePrefix, String... names) { |
| 53 | + Counter counter; |
| 54 | + String metricName = MetricRegistry.name(metricNamePrefix, names).toLowerCase(); |
| 55 | + if (counters.containsKey(metricName)) { |
| 56 | + counter = counters.get(metricName); |
| 57 | + } else { |
| 58 | + counter = metricRegistry.counter(metricName); |
| 59 | + counters.put(metricName, counter); |
| 60 | + } |
| 61 | + return counter; |
| 62 | + } |
| 63 | + |
| 64 | + protected Gauge<?> getGauge(Gauge<?> defaultGauge, String metricNamePrefix, String... names) { |
| 65 | + Gauge<?> gauge; |
| 66 | + String metricName = MetricRegistry.name(metricNamePrefix, names).toLowerCase(); |
| 67 | + if (gauges.containsKey(metricName)) { |
| 68 | + gauge = gauges.get(metricName); |
| 69 | + } else { |
| 70 | + gauge = defaultGauge; |
| 71 | + gauges.put(metricName, defaultGauge); |
| 72 | + } |
| 73 | + return gauge; |
| 74 | + } |
| 75 | + |
| 76 | + protected Timer getTimer(String metricNamePrefix, String... names) { |
| 77 | + Timer timer; |
| 78 | + String metricName = MetricRegistry.name(metricNamePrefix, names).toLowerCase(); |
| 79 | + if (timers.containsKey(metricName)) { |
| 80 | + timer = timers.get(metricName); |
| 81 | + } else { |
| 82 | + timer = metricRegistry.timer(metricName); |
| 83 | + timers.put(metricName, timer); |
| 84 | + } |
| 85 | + return timer; |
| 86 | + } |
| 87 | + |
| 88 | + protected String getMetricNamePrefix(Class<?> klass) { |
| 89 | + return klass.getSimpleName(); |
| 90 | + } |
| 91 | +} |
0 commit comments