luoyuxia commented on code in PR #2839: URL: https://github.com/apache/fluss/pull/2839#discussion_r3225979825
########## fluss-metrics/fluss-metrics-influxdb/src/main/java/org/apache/fluss/metrics/influxdb/InfluxdbReporter.java: ########## @@ -0,0 +1,162 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.fluss.metrics.influxdb; + +import org.apache.fluss.config.Configuration; +import org.apache.fluss.metrics.Metric; +import org.apache.fluss.metrics.groups.MetricGroup; +import org.apache.fluss.metrics.reporter.ScheduledMetricReporter; +import org.apache.fluss.utils.StringUtils; + +import com.influxdb.v3.client.InfluxDBClient; +import com.influxdb.v3.client.Point; +import com.influxdb.v3.client.config.ClientConfig; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.time.Duration; +import java.time.Instant; +import java.util.AbstractMap; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.regex.Pattern; + +/** {@link ScheduledMetricReporter} that exports {@link Metric Metrics} via InfluxDB. */ +public class InfluxdbReporter implements ScheduledMetricReporter { + + private static final Logger LOG = LoggerFactory.getLogger(InfluxdbReporter.class); + + private static final char SCOPE_SEPARATOR = '_'; + private static final String SCOPE_PREFIX = "fluss" + SCOPE_SEPARATOR; + private static final Pattern INVALID_CHAR_PATTERN = Pattern.compile("[^a-zA-Z0-9_:]"); + + final Map<Metric, String> metricNames; + final Map<Metric, List<Map.Entry<String, String>>> metricTags; + private final InfluxDBClient client; + private final Duration pushInterval; + private final InfluxdbPointProducer pointProducer; + + public InfluxdbReporter( + String hostUrl, String org, String bucket, String token, Duration pushInterval) { + ClientConfig.Builder clientConfigBuilder = + new ClientConfig.Builder().host(hostUrl).token(token.toCharArray()); + + if (!StringUtils.isNullOrWhitespaceOnly(org)) { + clientConfigBuilder.organization(org); + } + + ClientConfig clientConfig = clientConfigBuilder.database(bucket).build(); + + this.client = InfluxDBClient.getInstance(clientConfig); + this.pushInterval = pushInterval; + this.metricNames = new ConcurrentHashMap<>(); + this.metricTags = new ConcurrentHashMap<>(); + this.pointProducer = InfluxdbPointProducer.getInstance(); + + LOG.info("Started InfluxDB reporter connecting to {}", hostUrl); + } + + @Override + public void open(Configuration config) { + // do nothing + } + + @Override + public void close() { + if (client != null) { + try { + client.close(); + } catch (Exception e) { + LOG.warn("Failed to close InfluxDB client", e); + } + } + } + + @Override + public void report() { + List<Point> points = new ArrayList<>(); + Instant now = Instant.now(); + + for (Map.Entry<Metric, String> entry : metricNames.entrySet()) { + Metric metric = entry.getKey(); + String metricName = entry.getValue(); + List<Map.Entry<String, String>> tags = + metricTags.getOrDefault(metric, Collections.emptyList()); + + try { + Point point = pointProducer.createPoint(metric, metricName, tags, now); + points.add(point); + } catch (Exception e) { + LOG.warn("Failed to create point for metric {}", metricName, e); + } + } + + if (!points.isEmpty()) { + try { + client.writePoints(points); + } catch (Exception e) { + LOG.warn("Failed to write points to InfluxDB", e); + } + } + } + + @Override + public Duration scheduleInterval() { + return pushInterval; + } + + @Override + public void notifyOfAddedMetric(Metric metric, String metricName, MetricGroup group) { + String scopedMetricName = getScopedName(metricName, group); + List<Map.Entry<String, String>> tags = getTags(group); + + metricNames.put(metric, scopedMetricName); + metricTags.put(metric, tags); + } + + @Override + public void notifyOfRemovedMetric(Metric metric, String metricName, MetricGroup group) { + metricNames.remove(metric); + metricTags.remove(metric); + } + + private String getScopedName(String metricName, MetricGroup group) { + return SCOPE_PREFIX + + group.getLogicalScope(this::filterCharacters, SCOPE_SEPARATOR) + + SCOPE_SEPARATOR + + filterCharacters(metricName); + } + + private List<Map.Entry<String, String>> getTags(MetricGroup group) { + List<Map.Entry<String, String>> tags = new ArrayList<>(); + for (Map.Entry<String, String> entry : group.getAllVariables().entrySet()) { + tags.add( + new AbstractMap.SimpleEntry<>( + filterCharacters(entry.getKey()), filterCharacters(entry.getValue()))); Review Comment: the value is for tag/label value? After, https://github.com/apache/fluss/issues/3211 we won't filter tag value. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
