Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 19 Mar 2019 16:07:57 +0000 (UTC)
From:      Ryan Steinmetz <zi@FreeBSD.org>
To:        ports-committers@freebsd.org, svn-ports-all@freebsd.org, svn-ports-head@freebsd.org
Subject:   svn commit: r496269 - in head/net-mgmt/nsd_exporter: . files
Message-ID:  <201903191607.x2JG7vfr009701@repo.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: zi
Date: Tue Mar 19 16:07:57 2019
New Revision: 496269
URL: https://svnweb.freebsd.org/changeset/ports/496269

Log:
  - Merge in metric handling patches from upstream
  - Bump PORTREVISION

Modified:
  head/net-mgmt/nsd_exporter/Makefile
  head/net-mgmt/nsd_exporter/distinfo
  head/net-mgmt/nsd_exporter/files/patch-nsd__exporter.go

Modified: head/net-mgmt/nsd_exporter/Makefile
==============================================================================
--- head/net-mgmt/nsd_exporter/Makefile	Tue Mar 19 16:04:10 2019	(r496268)
+++ head/net-mgmt/nsd_exporter/Makefile	Tue Mar 19 16:07:57 2019	(r496269)
@@ -3,7 +3,7 @@
 
 PORTNAME=	nsd_exporter
 PORTVERSION=	0.1.0
-PORTREVISION=	1
+PORTREVISION=	2
 CATEGORIES=	net-mgmt
 
 MAINTAINER=	zi@FreeBSD.org
@@ -34,6 +34,7 @@ USE_RC_SUBR=	nsd_exporter
 
 post-patch:
 	${REINPLACE_CMD} -e 's|"/etc|"${LOCALBASE}/etc|g' ${WRKSRC_go_nsdctl}/nsdctl.go
+	${REINPLACE_CMD} -e 's|"/etc|"${LOCALBASE}/etc|g' ${WRKSRC}/nsd_exporter.go
 
 pre-build:
 	${MKDIR} ${GO_WRKDIR_SRC}/github.com/prometheus ${GO_WRKDIR_SRC}/gopkg.in \

Modified: head/net-mgmt/nsd_exporter/distinfo
==============================================================================
--- head/net-mgmt/nsd_exporter/distinfo	Tue Mar 19 16:04:10 2019	(r496268)
+++ head/net-mgmt/nsd_exporter/distinfo	Tue Mar 19 16:07:57 2019	(r496269)
@@ -1,4 +1,4 @@
-TIMESTAMP = 1551277938
+TIMESTAMP = 1553011211
 SHA256 (optix2000-nsd_exporter-0.1.0_GH0.tar.gz) = 7fb9fae9f434e904e79eceaef9e5640e8be66d57384f87a9a6a85ab663dcf51c
 SIZE (optix2000-nsd_exporter-0.1.0_GH0.tar.gz) = 8144
 SHA256 (prometheus-client_golang-v0.8.0_GH0.tar.gz) = 88b0040393276116f848de5bdd636717d339667273cfc45a4edda40b5e5682bd

Modified: head/net-mgmt/nsd_exporter/files/patch-nsd__exporter.go
==============================================================================
--- head/net-mgmt/nsd_exporter/files/patch-nsd__exporter.go	Tue Mar 19 16:04:10 2019	(r496268)
+++ head/net-mgmt/nsd_exporter/files/patch-nsd__exporter.go	Tue Mar 19 16:07:57 2019	(r496269)
@@ -1,15 +1,14 @@
 --- nsd_exporter.go.orig	2018-02-08 23:45:14 UTC
 +++ nsd_exporter.go
-@@ -19,7 +19,7 @@ import (
- var listenAddr = flag.String("listen-address", ":8080", "The address to listen on for HTTP requests.")
- var metricPath = flag.String("metric-path", "/metrics", "The path to export Prometheus metrocs to.")
- var metricConfigPath = flag.String("metric-config", "", "Mapping file for metrics. Defaults to built in file for NSD 4.1.x. This allows you to add or change any metrics that this scrapes")
--var nsdConfig = flag.String("config-file", "/etc/nsd/nsd.conf", "Configuration file for nsd/unbound to autodetect configuration from. Defaults to /etc/nsd/nsd.conf. Mutually exclusive with -nsd-address, -cert, -key and -ca")
-+var nsdConfig = flag.String("config-file", "/usr/local/etc/nsd/nsd.conf", "Configuration file for nsd/unbound to autodetect configuration from. Defaults to /usr/local/etc/nsd/nsd.conf. Mutually exclusive with -nsd-address, -cert, -key and -ca")
- var nsdType = flag.String("type", "nsd", "What nsd-like daemon to scrape (nsd or unbound). Defaults to nsd")
- var cert = flag.String("cert", "", "Client cert file location. Mutually exclusive with -config-file.")
- var key = flag.String("key", "", "Client key file location. Mutually exclusive with -config-file.")
-@@ -58,7 +58,7 @@ func (c *NSDCollector) Collect(ch chan<-
+@@ -5,6 +5,7 @@ package main
+ import (
+ 	"bufio"
+ 	"flag"
++	"fmt"
+ 	"log"
+ 	"net/http"
+ 	"strconv"
+@@ -58,10 +59,20 @@ func (c *NSDCollector) Collect(ch chan<-
  	s := bufio.NewScanner(r)
  	for s.Scan() {
  		line := strings.Split(s.Text(), "=")
@@ -17,4 +16,148 @@
 +		metricName := strings.TrimSpace(line[0])
  		m, ok := c.metrics[metricName]
  		if !ok {
- 			log.Println("Unknown Metric ", metricName, ". Skipping.")
+-			log.Println("Unknown Metric ", metricName, ". Skipping.")
++			log.Println("New metric " + metricName + " found. Refreshing.")
++			// Try to update the metrics list
++			err = c.updateMetric(s.Text())
++			if err != nil {
++				log.Println(err.Error())
++			}
++			// Refetch metric
++			m, ok = c.metrics[metricName]
++			if !ok {
++				log.Println("Metric " + metricName + "not configured. Skipping")
++			}
+ 			continue
+ 		}
+ 		value, err := strconv.ParseFloat(line[1], 64)
+@@ -84,7 +95,57 @@ func (c *NSDCollector) Collect(ch chan<-
+ 
+ }
+ 
+-func (c *NSDCollector) updateMetricsList() error {
++func (c *NSDCollector) updateMetric(s string) error {
++	// Assume line is in "metric=#" format
++	line := strings.Split(s, "=")
++	metricName := strings.TrimSpace(line[0])
++
++	_, exists := c.metrics[metricName]
++	if !exists {
++		metricConf, ok := metricConfiguration.Metrics[metricName]
++		if ok {
++			promName := nsdToProm.Replace(line[0])
++			c.metrics[metricName] = &promMetric{
++				desc: prometheus.NewDesc(
++					prometheus.BuildFQName(*nsdType, "", promName),
++					metricConf.Help,
++					nil,
++					nil,
++				),
++				valueType: metricConf.Type,
++			}
++		} else { // Try labeled metric
++			for _, v := range metricConfiguration.LabelMetrics {
++				labels := v.Regex.FindStringSubmatch(metricName)
++				if labels != nil {
++					var promName string
++					if v.Name != "" {
++						promName = v.Name
++					} else {
++						promName = nsdToProm.Replace(line[0])
++					}
++					c.metrics[metricName] = &promMetric{
++						desc: prometheus.NewDesc(
++							prometheus.BuildFQName(*nsdType, "", promName),
++							v.Help,
++							v.Labels,
++							nil,
++						),
++						valueType: v.Type,
++						labels:    labels[1:len(labels)],
++					}
++					// python "for-else"
++					goto Found
++				}
++			}
++			return fmt.Errorf("Metric ", metricName, " not found in config.")
++		Found:
++		}
++	}
++	return nil
++}
++
++func (c *NSDCollector) initMetricsList() error {
+ 	r, err := c.client.Command("stats_noreset")
+ 	if err != nil {
+ 		log.Println(err)
+@@ -98,49 +159,9 @@ func (c *NSDCollector) updateMetricsList
+ 	// Grab metrics
+ 	s := bufio.NewScanner(r)
+ 	for s.Scan() {
+-		// Assume line is in "metric=#" format
+-		line := strings.Split(s.Text(), "=")
+-		metricName := line[0]
+-
+-		_, exists := c.metrics[metricName]
+-		if !exists {
+-			metricConf, ok := metricConfiguration.Metrics[metricName]
+-			if ok {
+-				promName := nsdToProm.Replace(line[0])
+-				c.metrics[metricName] = &promMetric{
+-					desc: prometheus.NewDesc(
+-						prometheus.BuildFQName(*nsdType, "", promName),
+-						metricConf.Help,
+-						nil,
+-						nil,
+-					),
+-					valueType: metricConf.Type,
+-				}
+-			} else { // Try labeled metric
+-				for _, v := range metricConfiguration.LabelMetrics {
+-					// TODO: Move this to Collect(), to catch any metrics that are created at runtime
+-					// Unfortunately prom needs all metrics defined at the beginning
+-					labels := v.Regex.FindStringSubmatch(metricName)
+-					if labels != nil {
+-						var promName string
+-						if v.Name != "" {
+-							promName = v.Name
+-						} else {
+-							promName = nsdToProm.Replace(line[0])
+-						}
+-						c.metrics[metricName] = &promMetric{
+-							desc: prometheus.NewDesc(
+-								prometheus.BuildFQName(*nsdType, "", promName),
+-								v.Help,
+-								v.Labels,
+-								nil,
+-							),
+-							valueType: v.Type,
+-							labels:    labels[1:len(labels)],
+-						}
+-					}
+-				}
+-			}
++		err = c.updateMetric(s.Text())
++		if err != nil {
++			log.Println(err.Error(), "Skipping.")
+ 		}
+ 	}
+ 	return s.Err()
+@@ -156,7 +177,7 @@ func NewNSDCollector(nsdType string, hos
+ 		client: client,
+ 	}
+ 
+-	err = collector.updateMetricsList()
++	err = collector.initMetricsList()
+ 	if err != nil {
+ 		log.Println(err)
+ 		return nil, err
+@@ -174,7 +195,7 @@ func NewNSDCollectorFromConfig(path stri
+ 		client: client,
+ 	}
+ 
+-	err = collector.updateMetricsList()
++	err = collector.initMetricsList()
+ 	if err != nil {
+ 		log.Println(err)
+ 		return nil, err



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201903191607.x2JG7vfr009701>