wu-sheng commented on code in PR #9620:
URL: https://github.com/apache/skywalking/pull/9620#discussion_r1013587002


##########
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,129 @@
+/*
+ * 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.google.common.collect.ImmutableMap;
+import com.linecorp.armeria.server.annotation.Post;
+import com.linecorp.armeria.server.annotation.RequestConverter;
+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.meter.analyzer.prometheus.rule.Rule;
+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.handler.pojo.TelegrafData;
+import 
org.apache.skywalking.oap.server.receiver.telegraf.provider.handler.pojo.TelegrafDatum;
+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.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<Rule> 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(TelegrafDatum telegrafData) {
+
+        List<Sample> sampleList = new ArrayList<>();
+
+        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) -> {
+            if (value instanceof Number) {
+                Sample.SampleBuilder builder = Sample.builder();
+                Sample sample = builder.name(name + "_" + key)
+                        .timestamp(timestamp * 1000L)
+                        .value(((Number) value).doubleValue())
+                        .labels(immutableTags).build();
+
+                sampleList.add(sample);
+            }
+        });
+        return sampleList;
+
+    }
+
+    public ImmutableMap<String, SampleFamily> convertSampleFamily(TelegrafData 
telegrafData) {
+        List<Sample> allSamples = new ArrayList<>();
+
+        List<TelegrafDatum> metrics = telegrafData.getMetrics();
+        for (TelegrafDatum m : metrics) {
+            List<Sample> samples = convertTelegraf(m);
+            allSamples.addAll(samples);
+        }
+
+        // Grouping all samples by their name, then build sampleFamily

Review Comment:
   This policy seems not right. The samples should be grouped by bulk/sampling 
ID. 
   Because 
   1. Metrics in the same name with different timestamps should belong to 
different sample families.
   2. Different metrics(name) collected in the same timestamp(one collecting) 
should belong to the same sample family. You could check the MAL doc, which 
mentioned metrics should be calculated through one formula, ref to 
https://skywalking.apache.org/docs/main/next/en/concepts-and-designs/mal/#binary-operators,
 `instance_trace_analysis_error_count / instance_trace_count`



-- 
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]

Reply via email to