Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: change packets metrics to counter #53

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions kea_exporter/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
from urllib.parse import urlparse

import click
from prometheus_client import Gauge
from prometheus_client import Counter, Gauge, disable_created_metrics

from kea_exporter import DHCPVersion
from kea_exporter.http import KeaHTTPClient
from kea_exporter.uds import KeaSocketClient

disable_created_metrics()


class Exporter:
subnet_pattern = re.compile(
Expand Down Expand Up @@ -68,14 +70,14 @@ def update(self):
def setup_dhcp4_metrics(self):
self.metrics_dhcp4 = {
# Packets
"sent_packets": Gauge(f"{self.prefix_dhcp4}_packets_sent_total", "Packets sent", ["operation"]),
"received_packets": Gauge(
"sent_packets": Counter(f"{self.prefix_dhcp4}_packets_sent_total", "Packets sent", ["operation"]),
"received_packets": Counter(
f"{self.prefix_dhcp4}_packets_received_total",
"Packets received",
["operation"],
),
# per Subnet or Subnet pool
"addresses_allocation_fail": Gauge(
"addresses_allocation_fail": Counter(
f"{self.prefix_dhcp4}_allocations_failed_total",
"Allocation fail count",
[
Expand All @@ -94,12 +96,12 @@ def setup_dhcp4_metrics(self):
"Declined counts",
["subnet", "subnet_id", "pool"],
),
"addresses_declined_reclaimed_total": Gauge(
"addresses_declined_reclaimed_total": Counter(
f"{self.prefix_dhcp4}_addresses_declined_reclaimed_total",
"Declined addresses that were reclaimed",
["subnet", "subnet_id", "pool"],
),
"addresses_reclaimed_total": Gauge(
"addresses_reclaimed_total": Counter(
f"{self.prefix_dhcp4}_addresses_reclaimed_total",
"Expired addresses that were reclaimed",
["subnet", "subnet_id", "pool"],
Expand All @@ -109,7 +111,7 @@ def setup_dhcp4_metrics(self):
"Size of subnet address pool",
["subnet", "subnet_id", "pool"],
),
"reservation_conflicts_total": Gauge(
"reservation_conflicts_total": Counter(
f"{self.prefix_dhcp4}_reservation_conflicts_total",
"Reservation conflict count",
["subnet", "subnet_id"],
Expand Down Expand Up @@ -246,25 +248,25 @@ def setup_dhcp4_metrics(self):
def setup_dhcp6_metrics(self):
self.metrics_dhcp6 = {
# Packets sent/received
"sent_packets": Gauge(f"{self.prefix_dhcp6}_packets_sent_total", "Packets sent", ["operation"]),
"received_packets": Gauge(
"sent_packets": Counter(f"{self.prefix_dhcp6}_packets_sent_total", "Packets sent", ["operation"]),
"received_packets": Counter(
f"{self.prefix_dhcp6}_packets_received_total",
"Packets received",
["operation"],
),
# DHCPv4-over-DHCPv6
"sent_dhcp4_packets": Gauge(
"sent_dhcp4_packets": Counter(
f"{self.prefix_dhcp6}_packets_sent_dhcp4_total",
"DHCPv4-over-DHCPv6 Packets received",
"DHCPv4-over-DHCPv6 Packets sent",
["operation"],
),
"received_dhcp4_packets": Gauge(
"received_dhcp4_packets": Counter(
f"{self.prefix_dhcp6}_packets_received_dhcp4_total",
"DHCPv4-over-DHCPv6 Packets received",
["operation"],
),
# per Subnet or pool
"addresses_allocation_fail": Gauge(
"addresses_allocation_fail": Counter(
f"{self.prefix_dhcp6}_allocations_failed_total",
"Allocation fail count",
[
Expand All @@ -278,17 +280,17 @@ def setup_dhcp6_metrics(self):
"Declined addresses",
["subnet", "subnet_id", "pool"],
),
"addresses_declined_reclaimed_total": Gauge(
"addresses_declined_reclaimed_total": Counter(
f"{self.prefix_dhcp6}_addresses_declined_reclaimed_total",
"Declined addresses that were reclaimed",
["subnet", "subnet_id", "pool"],
),
"addresses_reclaimed_total": Gauge(
"addresses_reclaimed_total": Counter(
f"{self.prefix_dhcp6}_addresses_reclaimed_total",
"Expired addresses that were reclaimed",
["subnet", "subnet_id", "pool"],
),
"reservation_conflicts_total": Gauge(
"reservation_conflicts_total": Counter(
f"{self.prefix_dhcp6}_reservation_conflicts_total",
"Reservation conflict count",
["subnet", "subnet_id"],
Expand Down Expand Up @@ -572,4 +574,16 @@ def parse_metrics(self, dhcp_version, arguments, subnets):
labels = {key: val for key, val in labels.items() if key in metric._labelnames}

# export labels and value
metric.labels(**labels).set(value)
if isinstance(metric, Gauge):
metric.labels(**labels).set(value)
else:
current_value = metric.labels(**labels)._value.get()

# Attempt to handle counter resets (may not catch all cases)
# e.g resetting the metric with kea command, let the metric grow to its previous value, query the statistics
if value < current_value:
current_value = 0
metric.labels(**labels).reset()

if value > current_value:
metric.labels(**labels).inc(value - current_value)
Loading