soander commented on code in PR #9620:
URL: https://github.com/apache/skywalking/pull/9620#discussion_r977221289


##########
oap-server/server-receiver-plugin/skywalking-telegraf-receiver-plugin/src/main/java/org/apache/skywalking/oap/server/receiver/telegraf/provider/handler/TelegrafServiceHandler.java:
##########
@@ -0,0 +1,128 @@
+/*
+ * 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.skywalking.oap.server.receiver.telegraf.provider.handler;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableMap;
+import com.linecorp.armeria.server.annotation.Post;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.skywalking.apm.network.common.v3.Commands;
+import org.apache.skywalking.oap.meter.analyzer.MetricConvert;
+import org.apache.skywalking.oap.meter.analyzer.dsl.Sample;
+import org.apache.skywalking.oap.meter.analyzer.dsl.SampleFamily;
+import org.apache.skywalking.oap.meter.analyzer.dsl.SampleFamilyBuilder;
+import org.apache.skywalking.oap.server.core.analysis.meter.MeterSystem;
+import org.apache.skywalking.oap.server.library.module.ModuleManager;
+import 
org.apache.skywalking.oap.server.receiver.telegraf.provider.config.TelegrafConfig;
+import 
org.apache.skywalking.oap.server.receiver.telegraf.provider.handler.pojo.TelegrafData;
+import org.apache.skywalking.oap.server.telemetry.TelemetryModule;
+import org.apache.skywalking.oap.server.telemetry.api.CounterMetrics;
+import org.apache.skywalking.oap.server.telemetry.api.HistogramMetrics;
+import org.apache.skywalking.oap.server.telemetry.api.MetricsCreator;
+import org.apache.skywalking.oap.server.telemetry.api.MetricsTag;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+@Slf4j
+public class TelegrafServiceHandler {
+
+    private final HistogramMetrics histogram;
+    private final CounterMetrics errorCounter;
+    private List<MetricConvert> metricConvert;
+
+    public TelegrafServiceHandler(ModuleManager moduleManager, MeterSystem 
meterSystem, List<TelegrafConfig> rules) {
+
+        this.metricConvert = rules.stream().map(r -> new MetricConvert(r, 
meterSystem)).collect(Collectors.toList());
+
+        final MetricsCreator metricsCreator = 
moduleManager.find(TelemetryModule.NAME)
+                                                           .provider()
+                                                           
.getService(MetricsCreator.class);
+
+        histogram = metricsCreator.createHistogramMetric(
+                "telegraf_in_latency", "The process latency of telegraf data",
+                new MetricsTag.Keys("protocol"), new MetricsTag.Values("http")
+        );
+
+        errorCounter = metricsCreator.createCounter(
+                "telegraf_error_count", "The error number of telegraf 
analysis",
+                new MetricsTag.Keys("protocol"), new MetricsTag.Values("http")
+        );
+    }
+
+    /**
+    * Convert the TelegrafData object to meter {@link Sample}
+    **/
+    public List<Sample> convertTelegraf(TelegrafData telegrafData) {
+
+        List<Sample> sampleList = new ArrayList<>(Collections.emptyList());
+
+        Map<String, Object> fields = telegrafData.getFields();
+        String name = telegrafData.getName();
+        Map<String, String> tags = telegrafData.getTags();
+        ImmutableMap<String, String> immutableTags = ImmutableMap.copyOf(tags);
+        long timestamp = telegrafData.getTimestamp();
+
+        fields.forEach((key, value) -> {
+            Sample.SampleBuilder builder = Sample.builder();
+            if (value instanceof String) return;
+            Sample sample = builder.name(name + "_" + key)
+                    .timestamp(timestamp * 1000L)
+                    .value(((Number) value).doubleValue())
+                    .labels(immutableTags).build();
+
+            sampleList.add(sample);
+        });
+        return sampleList;
+
+    }
+
+    @Post("/telegraf")
+    public Commands collectData(String jsonInfo) {
+        TelegrafData telegrafData;
+        try (HistogramMetrics.Timer ignored = histogram.createTimer()) {
+            List<Sample> allSamples = new ArrayList<>();
+            ObjectMapper mapper = new ObjectMapper();
+            JsonNode node = mapper.readTree(jsonInfo);

Review Comment:
   > Can you rename the original `TelegrafData` class to `TelegrafDatum`, and 
define a class `TelegrafData` to contain a field `List<TelegrafDatum> metrics`, 
so you don't need to `readTree` here, and just declare the method `public 
Commands collectData(TelegrafData telegrafData) {`
   
   I change code according to your suggestion. Everything works fine.
   
   This is `TelegrafData` class.
   
![image](https://user-images.githubusercontent.com/79000735/191666192-76d80f30-eed9-4569-ae26-3619578f0b98.png)
   
   This is `public Commands collectData(TelegrafData telegrafData)` method.
   
![image](https://user-images.githubusercontent.com/79000735/191666960-bf395584-b0db-494f-b02e-3ed922c7863c.png)
   
   Is this modification good? If there are still problems, what else do I need 
to fix? Thank you for your guidance!
   



-- 
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: notifications-unsubscr...@skywalking.apache.org

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

Reply via email to