You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jmeter.apache.org by pm...@apache.org on 2017/08/26 13:50:03 UTC

svn commit: r1806300 - in /jmeter/trunk: src/components/org/apache/jmeter/visualizers/backend/influxdb/UdpMetricsSender.java xdocs/changes.xml

Author: pmouawad
Date: Sat Aug 26 13:50:03 2017
New Revision: 1806300

URL: http://svn.apache.org/viewvc?rev=1806300&view=rev
Log:
Bug 61457 - InfluxDB backend listener client : Support sending result to InfluxDB through UDP protocol 
This closes #302

Bugzilla Id: 61457

Added:
    jmeter/trunk/src/components/org/apache/jmeter/visualizers/backend/influxdb/UdpMetricsSender.java   (with props)
Modified:
    jmeter/trunk/xdocs/changes.xml

Added: jmeter/trunk/src/components/org/apache/jmeter/visualizers/backend/influxdb/UdpMetricsSender.java
URL: http://svn.apache.org/viewvc/jmeter/trunk/src/components/org/apache/jmeter/visualizers/backend/influxdb/UdpMetricsSender.java?rev=1806300&view=auto
==============================================================================
--- jmeter/trunk/src/components/org/apache/jmeter/visualizers/backend/influxdb/UdpMetricsSender.java (added)
+++ jmeter/trunk/src/components/org/apache/jmeter/visualizers/backend/influxdb/UdpMetricsSender.java Sat Aug 26 13:50:03 2017
@@ -0,0 +1,122 @@
+/*
+ * 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.jmeter.visualizers.backend.influxdb;
+
+import java.io.IOException;
+import java.net.DatagramPacket;
+import java.net.DatagramSocket;
+import java.net.InetAddress;
+import java.net.SocketException;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Influxdb sender base on The Line Protocol. <br>
+ * The Line Protocol is a text based format for writing points to InfluxDB. <br>
+ * Syntax : <br>
+ * <code>
+ * &lt;measurement&gt;[,&lt;tag_key&gt;=&lt;tag_value&gt;[,&lt;tag_key&gt;=&lt;tag_value&gt;]] &lt;field_key&gt;=&lt;field_value&gt;[,&lt;field_key&gt;=
+ * &lt;field_value&gt;] [&lt;timestamp&gt;] 
+ * </code><br>
+ * Each line, separated by the newline character, represents a single point in InfluxDB.<br> 
+ * Line Protocol is whitespace sensitive.
+ * 
+ */
+class UdpMetricsSender extends AbstractInfluxdbMetricsSender {
+    private static final Logger log = LoggerFactory.getLogger(UdpMetricsSender.class);
+
+    private final Object lock = new Object();
+
+    private InetAddress hostAddress;
+    private int udpPort;
+
+    private List<MetricTuple> metrics = new ArrayList<>();
+
+    UdpMetricsSender() {
+        super();
+    }
+
+    @Override
+    public void setup(String influxdbUrl) throws Exception {
+        try {
+            log.debug("Setting up with url:{}", influxdbUrl);
+            String[] urlComponents = influxdbUrl.split(":");
+            if(urlComponents.length == 2) {
+                hostAddress = InetAddress.getByName(urlComponents[0]);
+                udpPort = Integer.parseInt(urlComponents[1]);
+            } else {
+                throw new IllegalArgumentException("Influxdb url '"+influxdbUrl+"' is wrong. The format shoule be <host/ip>:<port>");
+            }
+        } catch (Exception e) {
+            throw new IllegalArgumentException("Influxdb url '"+influxdbUrl+"' is wrong. The format shoule be <host/ip>:<port>", e);
+        }
+    }
+
+    @Override
+    public void addMetric(String mesurement, String tag, String field) {
+        synchronized (lock) {
+            metrics.add(new MetricTuple(mesurement, tag, field, System.currentTimeMillis()));
+        }
+    }
+
+    @Override
+    public void writeAndSendMetrics() {
+        List<MetricTuple> tempMetrics;
+        synchronized (lock) {
+            if (metrics.isEmpty()) {
+                return;
+            }
+            tempMetrics = metrics;
+            metrics = new ArrayList<>(tempMetrics.size());
+        }
+        final List<MetricTuple> copyMetrics = tempMetrics;
+
+        if (!copyMetrics.isEmpty()) {
+            StringBuilder sb = new StringBuilder(copyMetrics.size() * 35);
+            for (MetricTuple metric : copyMetrics) {
+                // Add TimeStamp in nanosecond from epoch ( default in InfluxDB
+                // )
+                sb.append(metric.measurement).append(metric.tag).append(" ") //$NON-NLS-1$
+                    .append(metric.field).append(" ").append(metric.timestamp + "000000").append("\n"); //$NON-NLS-3$
+            }
+
+            try (DatagramSocket ds = new DatagramSocket()) {
+                byte[] buf = sb.toString().getBytes();
+                DatagramPacket dp = new DatagramPacket(buf, buf.length, this.hostAddress, this.udpPort);
+                ds.send(dp);
+            } catch (SocketException e) {
+                log.error("Cannot open udp port!", e);
+                return;
+            } catch (IOException e) {
+                log.error("Error in transferring udp package", e);
+            } finally {
+                // We drop metrics in all cases
+                copyMetrics.clear();
+            }
+        }
+    }
+
+    @Override
+    public void destroy() {
+        // NOOP
+    }
+}

Propchange: jmeter/trunk/src/components/org/apache/jmeter/visualizers/backend/influxdb/UdpMetricsSender.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: jmeter/trunk/src/components/org/apache/jmeter/visualizers/backend/influxdb/UdpMetricsSender.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: jmeter/trunk/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/jmeter/trunk/xdocs/changes.xml?rev=1806300&r1=1806299&r2=1806300&view=diff
==============================================================================
--- jmeter/trunk/xdocs/changes.xml [utf-8] (original)
+++ jmeter/trunk/xdocs/changes.xml [utf-8] Sat Aug 26 13:50:03 2017
@@ -113,7 +113,8 @@ Incorporated feed back about unclear doc
 <h3>Listeners</h3>
 <ul>
     <li><bug>61167</bug>InfluxdbBackendListener : add number of errors by response code and message for each transaction</li>
-    <li><bug>61068</bug>Introduce property <code>resultcollector.action_if_file_exists</code> to control the popup "File already exists" when starting a test</li> 
+    <li><bug>61068</bug>Introduce property <code>resultcollector.action_if_file_exists</code> to control the popup "File already exists" when starting a test</li>
+    <li><bug>61457</bug>InfluxDB backend listener client : Support sending result to InfluxDB through UDP protocol. Partly based on <pr>302</pr> by Junlong Wu (github id mybreeze77)</li> 
 </ul>
 
 <h3>Timers, Assertions, Config, Pre- &amp; Post-Processors</h3>
@@ -246,6 +247,7 @@ Incorporated feed back about unclear doc
 <li>Liu XP (liu_xp2003 at sina.com)</li>
 <li><a href="http://ubikloadpack.com">Ubik Load Pack</a></li>
 <li>Wolfgang Wagner (internetwolf2000 at hotmail.com)</li>
+<li>Junlong Wu (github id mybreeze77)</li>
 </ul>
 <p>We also thank bug reporters who helped us improve JMeter. <br/>
 For this release we want to give special thanks to the following reporters for the clear reports and tests made after our fixes:</p>