Skip to content

Commit

Permalink
[IOTDB-5663] Add connection num metrics (apache#9307)
Browse files Browse the repository at this point in the history
  • Loading branch information
THUMarkLau authored Mar 14, 2023
1 parent 5fe8f28 commit b15630b
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private void setUpInternalServices() throws StartupException {
MetricService.getInstance().addMetricSet(new ProcessMetrics());
MetricService.getInstance().addMetricSet(new SystemMetrics(false));
MetricService.getInstance().addMetricSet(new DiskMetrics(IoTDBConstant.CN_ROLE));
MetricService.getInstance().addMetricSet(new NetMetrics());
MetricService.getInstance().addMetricSet(new NetMetrics(IoTDBConstant.CN_ROLE));

LOGGER.info("Successfully setup internal services.");
}
Expand Down
92 changes: 91 additions & 1 deletion docs/UserGuide/Monitor-Alert/Apache-IoTDB-Network-Dashboard.json
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,96 @@
],
"title": "Packet Speed",
"type": "timeseries"
},
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"fieldConfig": {
"defaults": {
"color": {
"mode": "palette-classic"
},
"custom": {
"axisCenteredZero": false,
"axisColorMode": "text",
"axisGridShow": true,
"axisLabel": "",
"axisPlacement": "auto",
"barAlignment": 0,
"drawStyle": "line",
"fillOpacity": 0,
"gradientMode": "none",
"hideFrom": {
"legend": false,
"tooltip": false,
"viz": false
},
"lineInterpolation": "linear",
"lineWidth": 1,
"pointSize": 5,
"scaleDistribution": {
"type": "linear"
},
"showPoints": "auto",
"spanNulls": false,
"stacking": {
"group": "A",
"mode": "none"
},
"thresholdsStyle": {
"mode": "off"
}
},
"decimals": 0,
"mappings": [],
"thresholds": {
"mode": "absolute",
"steps": [
{
"color": "green",
"value": null
}
]
}
},
"overrides": []
},
"gridPos": {
"h": 10,
"w": 24,
"x": 0,
"y": 29
},
"id": 8,
"options": {
"legend": {
"calcs": [],
"displayMode": "list",
"placement": "bottom",
"showLegend": true
},
"tooltip": {
"mode": "multi",
"sort": "none"
}
},
"targets": [
{
"datasource": {
"type": "prometheus",
"uid": "${DS_PROMETHEUS}"
},
"editorMode": "code",
"expr": "connection_num{instance=~\"$instance\"}",
"legendFormat": "{{job}}-{{instance}}",
"range": true,
"refId": "A"
}
],
"title": "Connection Num",
"type": "timeseries"
}
],
"refresh": "15s",
Expand Down Expand Up @@ -439,6 +529,6 @@
"timezone": "",
"title": "Apache IoTDB Network Dashboard",
"uid": "AXEPYc-Vz",
"version": 17,
"version": 19,
"weekStart": ""
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,8 @@ default Map<String, Long> getTransmittedPackets() {
default Set<String> getIfaceSet() {
return Collections.emptySet();
}

default int getConnectionNum() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@

package org.apache.iotdb.metrics.metricsets.net;

import org.apache.iotdb.metrics.config.MetricConfig;
import org.apache.iotdb.metrics.config.MetricConfigDescriptor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand All @@ -50,11 +55,16 @@ public class LinuxNetMetricManager implements INetMetricManager {
private static final long UPDATE_INTERVAL = 10_000L;

private static final int IFACE_NAME_INDEX = 0;

private static final MetricConfig METRIC_CONFIG =
MetricConfigDescriptor.getInstance().getMetricConfig();
// initialized after reading status file
private int receivedBytesIndex = 0;
private int transmittedBytesIndex = 0;
private int receivedPacketsIndex = 0;
private int transmittedPacketsIndex = 0;
private int connectionNum = 0;
private final String[] getConnectNumCmd;
private Set<String> ifaceSet;

private final Map<String, Long> receivedBytesMapForIface;
Expand All @@ -70,6 +80,12 @@ public LinuxNetMetricManager() {
receivedPacketsMapForIface = new HashMap<>(ifaceSet.size() + 1, 1);
transmittedPacketsMapForIface = new HashMap<>(ifaceSet.size() + 1, 1);
collectNetStatusIndex();
this.getConnectNumCmd =
new String[] {
"/bin/sh",
"-c",
String.format("ls -l /proc/%s/fd | grep socket: | wc -l", METRIC_CONFIG.getPid())
};
}

private long lastUpdateTime = 0L;
Expand Down Expand Up @@ -104,6 +120,11 @@ public Map<String, Long> getTransmittedPackets() {
return transmittedPacketsMapForIface;
}

@Override
public int getConnectionNum() {
return connectionNum;
}

private void checkUpdate() {
if (System.currentTimeMillis() - lastUpdateTime >= UPDATE_INTERVAL) {
updateNetStatus();
Expand Down Expand Up @@ -193,5 +214,21 @@ private void updateNetStatus() {
} catch (IOException e) {
log.error("Meets error when reading {} for net status", NET_STATUS_PATH, e);
}

// update socket num
try {
Process process = Runtime.getRuntime().exec(this.getConnectNumCmd);
StringBuilder result = new StringBuilder();
try (BufferedReader input =
new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = input.readLine()) != null) {
result.append(line);
}
}
this.connectionNum = Integer.parseInt(result.toString().trim());
} catch (IOException e) {
log.error("Failed to get socket num", e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ public class NetMetrics implements IMetricSet {
private static final String RECEIVED_PACKETS = "received_packets";
private static final String TRANSMITTED_BYTES = "transmitted_bytes";
private static final String TRANSMITTED_PACKETS = "transmitted_packets";
private static final String CONNECTION_NUM = "connection_num";

private static final String TYPE = "type";
private static final String IFACE_NAME = "iface_name";
private static final String RECEIVE = "receive";
private static final String TRANSMIT = "transmit";
private static final String PROCESS_NAME = "process_num";

private final String processName;

public NetMetrics(String processName) {
this.processName = processName;
}

@Override
public void bindTo(AbstractMetricService metricService) {
Expand Down Expand Up @@ -81,6 +89,13 @@ public void bindTo(AbstractMetricService metricService) {
IFACE_NAME,
iface);
}
metricService.createAutoGauge(
CONNECTION_NUM,
MetricLevel.IMPORTANT,
netMetricManager,
x -> x.getConnectionNum(),
PROCESS_NAME,
this.processName);
}

@Override
Expand All @@ -94,5 +109,6 @@ public void unbindFrom(AbstractMetricService metricService) {
metricService.remove(
MetricType.AUTO_GAUGE, TRANSMITTED_PACKETS, TYPE, TRANSMIT, IFACE_NAME, iface);
}
metricService.remove(MetricType.AUTO_GAUGE, CONNECTION_NUM, PROCESS_NAME, this.processName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static void bind() {
MetricService.getInstance().addMetricSet(new ProcessMetrics());
MetricService.getInstance().addMetricSet(new SystemMetrics(true));
MetricService.getInstance().addMetricSet(new DiskMetrics(IoTDBConstant.DN_ROLE));
MetricService.getInstance().addMetricSet(new NetMetrics());
MetricService.getInstance().addMetricSet(new NetMetrics(IoTDBConstant.DN_ROLE));
MetricService.getInstance().addMetricSet(new WritingMetrics());

// bind query related metrics
Expand Down

0 comments on commit b15630b

Please sign in to comment.