You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@fluss.apache.org by "zhuyufeng0809 (via GitHub)" <gi...@apache.org> on 2026/03/10 13:53:35 UTC

[PR] [metric] Support influxdb reporter [fluss]

zhuyufeng0809 opened a new pull request, #2839:
URL: https://github.com/apache/fluss/pull/2839

   <!--
   *Thank you very much for contributing to Fluss - we are happy that you want to help us improve Fluss. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [GitHub issue](https://github.com/apache/fluss/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no issue.
   
     - Name the pull request in the format "[component] Title of the pull request", where *[component]* should be replaced by the name of the component being changed. Typically, this corresponds to the component label assigned to the issue (e.g., [kv], [log], [client], [flink]). Skip *[component]* if you are unsure about which is the best component.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
   
     - Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes.
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
   
   
   **(The sections below can be removed for hotfixes or typos)**
   -->
   
   ### Purpose
   
   <!-- Linking this pull request to the issue -->
   Linked issue: close #xxx
   
   <!-- What is the purpose of the change -->
   
   ### Brief change log
   
   <!-- Please describe the changes made in this pull request and explain how they address the issue -->
   
   ### Tests
   
   <!-- List UT and IT cases to verify this change -->
   
   ### API and Format
   
   <!-- Does this change affect API or storage format -->
   
   ### Documentation
   
   <!-- Does this change introduce a new feature -->
   


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [metric] Support influxdb reporter [fluss]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #2839:
URL: https://github.com/apache/fluss/pull/2839#discussion_r2930990098


##########
fluss-metrics/fluss-metrics-influxdb/src/main/java/org/apache/fluss/metrics/influxdb/InfluxdbReporter.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.MapUtils;
+
+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.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/** {@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 final Map<Metric, String> metricNames;
+    private 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 clientConfig =
+                new ClientConfig.Builder()
+                        .host(hostUrl)
+                        .token(token.toCharArray())
+                        .organization(org)
+                        .database(bucket)
+                        .build();
+
+        this.client = InfluxDBClient.getInstance(clientConfig);
+        this.pushInterval = pushInterval;
+        this.metricNames = MapUtils.newConcurrentHashMap();
+        this.metricTags = MapUtils.newConcurrentHashMap();
+        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.get(metric);
+
+            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);
+            }
+        }
+
+        client.writePoints(points);

Review Comment:
   `report()` calls `client.writePoints(points)` without any exception handling. If the Influx client throws (network issues, auth errors, etc.), this can bubble up into the metrics system and impact process stability. Wrap the write call in a try/catch and log at WARN (similar to other reporters) to keep metrics best-effort.
   



##########
fluss-metrics/fluss-metrics-influxdb/src/main/java/org/apache/fluss/metrics/influxdb/InfluxdbReporter.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.MapUtils;
+
+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.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/** {@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 final Map<Metric, String> metricNames;
+    private 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 clientConfig =
+                new ClientConfig.Builder()
+                        .host(hostUrl)
+                        .token(token.toCharArray())
+                        .organization(org)
+                        .database(bucket)
+                        .build();
+
+        this.client = InfluxDBClient.getInstance(clientConfig);
+        this.pushInterval = pushInterval;
+        this.metricNames = MapUtils.newConcurrentHashMap();
+        this.metricTags = MapUtils.newConcurrentHashMap();
+        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.get(metric);
+
+            try {
+                Point point = pointProducer.createPoint(metric, metricName, tags, now);
+                points.add(point);

Review Comment:
   `metricTags.get(metric)` may return null (e.g., concurrent remove between iterating `metricNames` and reading `metricTags`), but `InfluxdbPointProducer.createPoint()` unconditionally iterates over `tags`. This can cause an NPE during reporting. Consider iterating over a single combined structure (name+tags) or defaulting to an empty tag list when missing.



##########
fluss-metrics/fluss-metrics-influxdb/src/test/java/org/apache/fluss/metrics/influxdb/InfluxdbReporterTest.java:
##########
@@ -0,0 +1,228 @@
+/*
+ * 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.Counter;
+import org.apache.fluss.metrics.Gauge;
+import org.apache.fluss.metrics.SimpleCounter;
+import org.apache.fluss.metrics.groups.MetricGroup;
+import org.apache.fluss.metrics.util.TestHistogram;
+import org.apache.fluss.metrics.util.TestMeter;
+import org.apache.fluss.metrics.util.TestMetricGroup;
+
+import com.influxdb.v3.client.InfluxDBClient;
+import com.influxdb.v3.client.Point;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.mockito.ArgumentCaptor;
+
+import java.lang.reflect.Field;
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doNothing;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+
+/** Tests for the {@link InfluxdbReporter}. */
+class InfluxdbReporterTest {
+
+    private InfluxdbReporter reporter;
+    private MetricGroup metricGroup;
+    private InfluxDBClient mockClient;
+
+    @BeforeEach
+    void setUp() throws Exception {
+        mockClient = mock(InfluxDBClient.class);
+        doNothing().when(mockClient).writePoints(any());
+
+        reporter =
+                new InfluxdbReporter(
+                        "http://localhost:8086",
+                        "test-org",
+                        "test-bucket",
+                        "test-token",
+                        Duration.ofSeconds(10)) {
+                    @Override
+                    public void open(Configuration config) {
+                        // Override to inject mock client
+                        try {
+                            Field clientField = InfluxdbReporter.class.getDeclaredField("client");
+                            clientField.setAccessible(true);
+                            clientField.set(this, mockClient);
+                        } catch (Exception e) {
+                            throw new RuntimeException(e);
+                        }
+                    }
+                };

Review Comment:
   The test injects `mockClient` by reflectively setting the `InfluxdbReporter.client` field inside an overridden `open()`. In production code the field is `final`, so this is brittle and may fail on newer JVMs (and the constructor already creates a real client before `open()` runs). Prefer a test-only constructor/factory that accepts an `InfluxDBClient` (or a supplier) so tests can avoid reflection and avoid instantiating the real client.



##########
fluss-metrics/fluss-metrics-influxdb/src/main/java/org/apache/fluss/metrics/influxdb/InfluxdbReporter.java:
##########
@@ -0,0 +1,150 @@
+/*
+ * 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.MapUtils;
+
+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.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/** {@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 final Map<Metric, String> metricNames;
+    private 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 clientConfig =
+                new ClientConfig.Builder()
+                        .host(hostUrl)
+                        .token(token.toCharArray())
+                        .organization(org)
+                        .database(bucket)
+                        .build();
+
+        this.client = InfluxDBClient.getInstance(clientConfig);
+        this.pushInterval = pushInterval;
+        this.metricNames = MapUtils.newConcurrentHashMap();
+        this.metricTags = MapUtils.newConcurrentHashMap();
+        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.get(metric);
+
+            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);
+            }
+        }
+
+        client.writePoints(points);
+    }
+
+    @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 HashMap.SimpleEntry<>(
+                            filterCharacters(entry.getKey()), filterCharacters(entry.getValue())));
+        }

Review Comment:
   `getTags()` uses `new HashMap.SimpleEntry<>(...)`, but `HashMap` doesn't provide `SimpleEntry` (it's `AbstractMap.SimpleEntry`). This currently won't compile; switch to `AbstractMap.SimpleEntry` (or another `Map.Entry` implementation) for tag entries.



##########
fluss-metrics/fluss-metrics-influxdb/src/main/java/org/apache/fluss/metrics/influxdb/InfluxdbPointProducer.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.metrics.Counter;
+import org.apache.fluss.metrics.Gauge;
+import org.apache.fluss.metrics.Histogram;
+import org.apache.fluss.metrics.HistogramStatistics;
+import org.apache.fluss.metrics.Meter;
+import org.apache.fluss.metrics.Metric;
+
+import com.influxdb.v3.client.Point;
+
+import java.time.Instant;
+import java.util.List;
+import java.util.Map;
+
+/** Producer that creates InfluxDB {@link Point Points} from Fluss {@link Metric Metrics}. */
+public class InfluxdbPointProducer {
+
+    private static final InfluxdbPointProducer INSTANCE = new InfluxdbPointProducer();
+
+    public static InfluxdbPointProducer getInstance() {
+        return INSTANCE;
+    }
+
+    public Point createPoint(
+            Metric metric, String metricName, List<Map.Entry<String, String>> tags, Instant time) {
+        Point point = Point.measurement(metricName).setTimestamp(time);
+
+        for (Map.Entry<String, String> tag : tags) {
+            point.setTag(tag.getKey(), tag.getValue());

Review Comment:
   `createPoint(...)` assumes `tags` is non-null and iterates over it. Given the reporter stores tags in a separate map from names, this can be null in some cases (e.g., concurrent removal). Guard against null by treating it as an empty list to avoid NPEs.
   



##########
fluss-metrics/fluss-metrics-influxdb/src/main/java/org/apache/fluss/metrics/influxdb/InfluxdbPointProducer.java:
##########
@@ -0,0 +1,104 @@
+/*
+ * 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.metrics.Counter;
+import org.apache.fluss.metrics.Gauge;
+import org.apache.fluss.metrics.Histogram;
+import org.apache.fluss.metrics.HistogramStatistics;
+import org.apache.fluss.metrics.Meter;
+import org.apache.fluss.metrics.Metric;
+
+import com.influxdb.v3.client.Point;
+
+import java.time.Instant;
+import java.util.List;
+import java.util.Map;
+
+/** Producer that creates InfluxDB {@link Point Points} from Fluss {@link Metric Metrics}. */
+public class InfluxdbPointProducer {
+
+    private static final InfluxdbPointProducer INSTANCE = new InfluxdbPointProducer();
+
+    public static InfluxdbPointProducer getInstance() {
+        return INSTANCE;
+    }
+
+    public Point createPoint(
+            Metric metric, String metricName, List<Map.Entry<String, String>> tags, Instant time) {
+        Point point = Point.measurement(metricName).setTimestamp(time);
+
+        for (Map.Entry<String, String> tag : tags) {
+            point.setTag(tag.getKey(), tag.getValue());
+        }
+
+        if (metric instanceof Counter) {
+            return createPointForCounter((Counter) metric, point);
+        }
+
+        if (metric instanceof Gauge) {
+            return createPointForGauge((Gauge<?>) metric, point);
+        }
+
+        if (metric instanceof Meter) {
+            return createPointForMeter((Meter) metric, point);
+        }
+
+        if (metric instanceof Histogram) {
+            return createPointForHistogram((Histogram) metric, point);
+        }
+
+        throw new IllegalArgumentException("Unknown metric type: " + metric.getClass());
+    }
+
+    private Point createPointForCounter(Counter counter, Point point) {
+        return point.setField("count", counter.getCount());
+    }
+
+    private Point createPointForGauge(Gauge<?> gauge, Point point) {
+        Object value = gauge.getValue();
+
+        if (value instanceof Number) {
+            return point.setField("value", ((Number) value));
+        } else if (value instanceof Boolean) {
+            return point.setField("value", ((boolean) value));

Review Comment:
   In `createPointForGauge`, `value` is an `Object`; casting it to primitive with `(boolean) value` is invalid Java and won't compile. Use `(Boolean) value` (auto-unboxing) or `((Boolean) value).booleanValue()` when the value is a `Boolean`.
   



##########
fluss-metrics/pom.xml:
##########
@@ -34,6 +34,7 @@
     <modules>
         <module>fluss-metrics-prometheus</module>
         <module>fluss-metrics-jmx</module>
+        <module>fluss-metrics-influxdb</module>

Review Comment:
   This adds the new `fluss-metrics-influxdb` module, but the distribution packaging doesn't currently include the new plugin (e.g. `fluss-dist/src/main/assemblies/plugins.xml` only lists prometheus/jmx). Without wiring it into `fluss-dist`, the module will build but the plugin jar won't be shipped in the Fluss distribution.
   



##########
fluss-metrics/fluss-metrics-influxdb/src/test/resources/log4j2-test.properties:
##########
@@ -0,0 +1,28 @@
+w#

Review Comment:
   The first line is `w#`, which looks like an accidental extra character before the comment marker. This may break log4j2 properties parsing; it should likely start with `#` like the other modules' `log4j2-test.properties` files.
   



-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [metric] Support influxdb reporter [fluss]

Posted by "zhuyufeng0809 (via GitHub)" <gi...@apache.org>.
zhuyufeng0809 commented on PR #2839:
URL: https://github.com/apache/fluss/pull/2839#issuecomment-4036941560

   Hi @swuferhong  , could you help me to review this PR ?


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [metric] Support influxdb reporter [fluss]

Posted by "luoyuxia (via GitHub)" <gi...@apache.org>.
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [metric] Support influxdb reporter [fluss]

Posted by "zhuyufeng0809 (via GitHub)" <gi...@apache.org>.
zhuyufeng0809 commented on code in PR #2839:
URL: https://github.com/apache/fluss/pull/2839#discussion_r3245372388


##########
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:
   Thank you for the reminder. I checked Influxdb's specifications regarding Tag Values:
   https://docs.influxdata.com/influxdb/v2/reference/syntax/line-protocol/#tag-set
   https://docs.influxdata.com/influxdb3/core/reference/naming-restrictions/#tag-values-and-field-values
   Indeed, no filtering is required. I've already made the modification. PTAL
   @luoyuxia 



-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [metric] Support influxdb reporter [fluss]

Posted by "swuferhong (via GitHub)" <gi...@apache.org>.
swuferhong commented on PR #2839:
URL: https://github.com/apache/fluss/pull/2839#issuecomment-4428303291

   Hi, @luoyuxia Could you take a look at this pr?


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [metric] Support influxdb reporter [fluss]

Posted by "zhuyufeng0809 (via GitHub)" <gi...@apache.org>.
zhuyufeng0809 closed pull request #2839: [metric] Support influxdb reporter
URL: https://github.com/apache/fluss/pull/2839


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [metric] Support influxdb reporter [fluss]

Posted by "luoyuxia (via GitHub)" <gi...@apache.org>.
luoyuxia commented on PR #2839:
URL: https://github.com/apache/fluss/pull/2839#issuecomment-4429333606

   will have a quick review later


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [metric] Support influxdb reporter [fluss]

Posted by "zhuyufeng0809 (via GitHub)" <gi...@apache.org>.
zhuyufeng0809 commented on PR #2839:
URL: https://github.com/apache/fluss/pull/2839#issuecomment-4394721672

   @luoyuxia Hi, PTAL


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [metric] Support influxdb reporter [fluss]

Posted by "luoyuxia (via GitHub)" <gi...@apache.org>.
luoyuxia merged PR #2839:
URL: https://github.com/apache/fluss/pull/2839


-- 
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: issues-unsubscribe@fluss.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org