Skip to content

Instantly share code, notes, and snippets.

@Jamesits
Last active July 9, 2023 04:42
Show Gist options
  • Save Jamesits/7c33df3484bc52b0daf96e0e3a1887e1 to your computer and use it in GitHub Desktop.
Save Jamesits/7c33df3484bc52b0daf96e0e3a1887e1 to your computer and use it in GitHub Desktop.
Latency/packet loss graph demo for InfluxDB 2 (Flux Query Language) + Grafana

Grafana dashboard setup:

  • Query options
    • Max data points: 600 if your data collection interval is 1min, adjust on your needs
  • Visualization: Graph
  • Display:
    • Bars: off
    • Lines: off
    • Points: off
  • Series overrides
    • max
      • Color: Yellow
      • Fill gradient: 2
      • Fill below to: min
    • mean
      • Color: Green
      • Fill gradient: 2
      • Fill below to: min
    • min
      • Color: Blue
      • Lines: true
    • packet_loss%:
      • Color: Red
      • Y-axis: 2
      • Lines: true
      • Line width: 2
      • Staircase line: true
  • Axes
    • Left Y
      • Unit: ms
      • Y-Min: 0
    • Right Y
      • Unit: Percent
      • Y-Min: 0
      • Y-Max: 100

References:

bucket_name = "telegraf"
src_hostname = "hostname"
dst_url = "8.8.8.8"
outputColumns = ["_field", "_time", "_value"]
rawLatencyQuery = from(bucket: bucket_name)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "ping")
|> filter(fn: (r) => r["_field"] == "average_response_ms")
|> filter(fn: (r) => r["host"] == src_hostname)
|> filter(fn: (r) => r["url"] == dst_url)
meanLatencyRet = rawLatencyQuery
|> map(fn: (r) => ({_value: r._value, _time: r._time, _field: "mean"}))
|> keep(columns: outputColumns)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "mean")
maxLatencyRet = rawLatencyQuery
|> map(fn: (r) => ({_value: r._value, _time: r._time, _field: "max"}))
|> keep(columns: outputColumns)
|> aggregateWindow(every: v.windowPeriod, fn: max, createEmpty: false)
|> yield(name: "max")
minLatencyRet = rawLatencyQuery
|> map(fn: (r) => ({_value: r._value, _time: r._time, _field: "min"}))
|> keep(columns: outputColumns)
|> aggregateWindow(every: v.windowPeriod, fn: min, createEmpty: false)
|> yield(name: "min")
packetLossRateRawQuery = from(bucket: bucket_name)
|> range(start: v.timeRangeStart, stop: v.timeRangeStop)
|> filter(fn: (r) => r["_measurement"] == "ping")
|> filter(fn: (r) => r["_field"] == "percent_packet_loss")
|> filter(fn: (r) => r["host"] == src_hostname)
|> filter(fn: (r) => r["url"] == dst_url)
packetLossRateRet = packetLossRateRawQuery
|> map(fn: (r) => ({_value: r._value, _time: r._time, _field: "packet_loss%"}))
|> keep(columns: outputColumns)
|> aggregateWindow(every: v.windowPeriod, fn: mean, createEmpty: false)
|> yield(name: "packet_loss_rate")
[global_tags]
dc = "he-fmt2"
[agent]
interval = "60s"
round_interval = true
metric_batch_size = 1000
metric_buffer_limit = 100000
collection_jitter = "10s"
flush_interval = "10s"
flush_jitter = "10s"
precision = "1s"
omit_hostname = false
[[outputs.influxdb_v2]]
urls = ["https://influxdb.corp.contoso.com"]
token = "5pyJ5YaF6ay877yM5YGc5q2i5Lqk5piT77yB"
organization = "demo_org"
bucket = "telegraf"
user_agent = "telegraf"
content_encoding = "gzip"
influx_uint_support = true
# tls_ca = "/etc/telegraf/rootca.crt"
# tls_cert = "/etc/telegraf/cert.pem"
# tls_key = "/etc/telegraf/key.pem"
insecure_skip_verify = false
[[inputs.ping]]
urls = [
"8.8.8.8",
]
method = "exec" # do not use "native" as of Telegraf 1.16.1
count = 10
ping_interval = 1.0
timeout = 1.0
deadline = 20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment