Skip to content
Merged
Show file tree
Hide file tree
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
41 changes: 0 additions & 41 deletions bind/auto/auto.go

This file was deleted.

46 changes: 0 additions & 46 deletions bind/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@
package bind

import (
"encoding/xml"
"fmt"
"net/http"
"net/url"
"path"
"time"
)

Expand All @@ -28,47 +23,6 @@ type Client interface {
Stats(...StatisticGroup) (Statistics, error)
}

// XMLClient is a generic BIND API client to retrieve statistics in XML format.
type XMLClient struct {
url string
http *http.Client
}

// NewXMLClient returns an initialized XMLClient.
func NewXMLClient(url string, c *http.Client) *XMLClient {
return &XMLClient{
url: url,
http: c,
}
}

// Get queries the given path and stores the result in the value pointed to by
// v. The endpoint must return a valid XML representation which can be
// unmarshaled into the provided value.
func (c *XMLClient) Get(p string, v interface{}) error {
u, err := url.Parse(c.url)
if err != nil {
return fmt.Errorf("invalid URL %q: %s", c.url, err)
}
u.Path = path.Join(u.Path, p)

resp, err := c.http.Get(u.String())
if err != nil {
return fmt.Errorf("error querying stats: %s", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status for %q: %s", u, resp.Status)
}

if err := xml.NewDecoder(resp.Body).Decode(v); err != nil {
return fmt.Errorf("failed to unmarshal XML response: %s", err)
}

return nil
}

const (
// QryRTT is the common prefix of query round-trip histogram counters.
QryRTT = "QryRTT"
Expand Down
135 changes: 0 additions & 135 deletions bind/v2/v2.go

This file was deleted.

51 changes: 42 additions & 9 deletions bind/v3/v3.go → bind/xml/xml.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package v3
package xml

import (
"encoding/xml"
"fmt"
"net/http"
"net/url"
"path"
"time"

"github.com/prometheus-community/bind_exporter/bind"
Expand All @@ -40,11 +44,9 @@ const (
)

type Statistics struct {
Memory struct{} `xml:"memory"`
Server Server `xml:"server"`
Socketmgr struct{} `xml:"socketmgr"`
Taskmgr bind.TaskManager `xml:"taskmgr"`
Views []View `xml:"views>view"`
Server Server `xml:"server"`
Taskmgr bind.TaskManager `xml:"taskmgr"`
Views []View `xml:"views>view"`
}

type ZoneStatistics struct {
Expand Down Expand Up @@ -84,14 +86,45 @@ type ZoneCounter struct {
Serial string `xml:"serial"`
}

// Client implements bind.Client and can be used to query a BIND v3 API.
// Client implements bind.Client and can be used to query a BIND XML v3 API.
type Client struct {
*bind.XMLClient
url string
http *http.Client
}

// NewClient returns an initialized Client.
func NewClient(url string, c *http.Client) *Client {
return &Client{XMLClient: bind.NewXMLClient(url, c)}
return &Client{
url: url,
http: c,
}
}

// Get queries the given path and stores the result in the value pointed to by
// v. The endpoint must return a valid XML representation which can be
// unmarshaled into the provided value.
func (c *Client) Get(p string, v interface{}) error {
u, err := url.Parse(c.url)
if err != nil {
return fmt.Errorf("invalid URL %q: %s", c.url, err)
}
u.Path = path.Join(u.Path, p)

resp, err := c.http.Get(u.String())
if err != nil {
return fmt.Errorf("error querying stats: %s", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("unexpected status %s", resp.Status)
}

if err := xml.NewDecoder(resp.Body).Decode(v); err != nil {
return fmt.Errorf("failed to unmarshal XML response: %s", err)
}

return nil
}

// Stats implements bind.Stats.
Expand Down
18 changes: 6 additions & 12 deletions bind_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ import (
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/bind_exporter/bind"
"github.com/prometheus-community/bind_exporter/bind/auto"
"github.com/prometheus-community/bind_exporter/bind/json"
"github.com/prometheus-community/bind_exporter/bind/v2"
"github.com/prometheus-community/bind_exporter/bind/v3"
"github.com/prometheus-community/bind_exporter/bind/xml"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -402,14 +400,10 @@ type Exporter struct {
func NewExporter(logger log.Logger, version, url string, timeout time.Duration, g []bind.StatisticGroup) *Exporter {
var c bind.Client
switch version {
case "json":
c = json.NewClient(url, &http.Client{Timeout: timeout})
case "xml.v2":
c = v2.NewClient(url, &http.Client{Timeout: timeout})
case "xml.v3":
c = v3.NewClient(url, &http.Client{Timeout: timeout})
case "xml", "xml.v3":
c = xml.NewClient(url, &http.Client{Timeout: timeout})
default:
c = auto.NewClient(url, &http.Client{Timeout: timeout})
c = json.NewClient(url, &http.Client{Timeout: timeout})
}

var cs []collectorConstructor
Expand Down Expand Up @@ -538,8 +532,8 @@ func main() {
"Path to BIND's pid file to export process information",
).Default("/run/named/named.pid").String()
bindVersion = kingpin.Flag("bind.stats-version",
"BIND statistics version. Can be detected automatically.",
).Default("auto").Enum("json", "xml.v2", "xml.v3", "auto")
"BIND statistics channel",
).Default("json").Enum("json", "xml", "xml.v3", "auto")
metricsPath = kingpin.Flag(
"web.telemetry-path", "Path under which to expose metrics",
).Default("/metrics").String()
Expand Down
Loading