<commit_before>package main

import (
    "encoding/json"
    "io/ioutil"
    "net/http"
    "os"
    "time"

    "github.com/michaelklishin/rabbit-hole"
    "github.com/prometheus/client_golang/prometheus"
    "github.com/Sirupsen/logrus"
)

const (
    namespace = "rabbitmq"
    configPath = "config.json"
)

var log = logrus.New()

// Listed available metrics
var (
    channelsTotal = prometheus.NewGauge(
        prometheus.GaugeOpts{
            Namespace: namespace,
            Name:      "channels_total",
            Help:      "Total number of open channels.",
        },
    )
    connectionsTotal = prometheus.NewGauge(
        prometheus.GaugeOpts{
            Namespace: namespace,
            Name:      "connections_total",
            Help:      "Total number of open connections.",
        },
    )
)

type Config struct {
    Nodes      *[]Node     `json:"nodes"`
    Port       string      `json:"port"`
    Interval   string      `json:"req_interval"`
}

type Node struct {
    Name       string      `json:"name"`
    Url        string      `json:"url"`
    Uname      string      `json:"uname"`
    Password   string      `json:"password"`
    Interval   string      `json:"req_interval,omitempty"`
}

func updateNodesStats(config *Config) {
    for _, node := range *config.Nodes {

        if len(node.Interval) == 0 {
            node.Interval = config.Interval
        }
        go runRequestLoop(node)
    }
}

func runRequestLoop(node Node) {
    for {
        rmqc, err := rabbithole.NewClient(node.Url, node.Uname, node.Password)

        updateMetrics(rmqc)

        dt, err := time.ParseDuration(node.Interval)
        if err != nil {
            log.Warningln(err)
            dt = 30 * time.Second
        }
        time.Sleep(dt)
    }
}

func updateMetrics(client *rabbithole.Client) {
    r1, _ := client.ListConnections()
    r2, _ := client.ListChannels()

    channelsTotal.Set(float64(len(r1)))
    connectionsTotal.Set(float64(len(r2)))
}

func newConfig(path string) (*Config, error) {
    var config Config

    file, err := ioutil.ReadFile(path)
    if err != nil {
        return nil, err
    }
    err = json.Unmarshal(file, &config)
    return &config, err
}

func main() {
    log.Out = os.Stdout
    config, _ := newConfig(configPath)
    updateNodesStats(config)

    http.Handle("/metrics", prometheus.Handler())
    log.Infof("Starting RabbitMQ exporter on port: %s.\n", config.Port)
    http.ListenAndServe(":" + config.Port, nil)
}

// Register metrics to Prometheus
func init() {
    prometheus.MustRegister(channelsTotal)
    prometheus.MustRegister(connectionsTotal)
}
<commit_msg>remove rabbit-hole dependency, add new metrics<commit_after>package main

import (
    "encoding/json"
    "io/ioutil"
    "net/http"
    "os"
    "time"
    "fmt"

    "github.com/prometheus/client_golang/prometheus"
    "github.com/Sirupsen/logrus"
)

const (
    namespace = "rabbitmq"
    configPath = "config.json"
)

var log = logrus.New()

// Listed available metrics
var (
    connectionsTotal = prometheus.NewGauge(
        prometheus.GaugeOpts{
            Namespace: namespace,
            Name:      "connections_total",
            Help:      "Total number of open connections.",
        },
    )
    channelsTotal = prometheus.NewGauge(
        prometheus.GaugeOpts{
            Namespace: namespace,
            Name:      "channels_total",
            Help:      "Total number of open channels.",
        },
    )
    queuesTotal = prometheus.NewGauge(
        prometheus.GaugeOpts{
            Namespace: namespace,
            Name:      "queues_total",
            Help:      "Total number of queues in use.",
        },
    )
    consumersTotal = prometheus.NewGauge(
        prometheus.GaugeOpts{
            Namespace: namespace,
            Name:      "consumers_total",
            Help:      "Total number of message consumers.",
        },
    )
    exchangesTotal = prometheus.NewGauge(
        prometheus.GaugeOpts{
            Namespace: namespace,
            Name:      "exchanges_total",
            Help:      "Total number of exchanges in use.",
        },
    )
)

type Config struct {
    Nodes      *[]Node     `json:"nodes"`
    Port       string      `json:"port"`
    Interval   string      `json:"req_interval"`
}

type Node struct {
    Name       string      `json:"name"`
    Url        string      `json:"url"`
    Uname      string      `json:"uname"`
    Password   string      `json:"password"`
    Interval   string      `json:"req_interval,omitempty"`
}


func unpackMetrics(d *json.Decoder) map[string]float64 {
    var output map[string]interface{}

    if err := d.Decode(&output); err != nil {
        fmt.Printf("Error : %s", err)
    }
    metrics := make(map[string]float64)

    for k, v := range output["object_totals"].(map[string]interface{}) {
        metrics[k] = v.(float64)
    }
    return metrics
}


func getOverview(hostname, username, password string) *json.Decoder {
    client := &http.Client{}
    req, err := http.NewRequest("GET", hostname + "/api/overview", nil)
    req.SetBasicAuth(username, password)

    resp, err := client.Do(req)

    if err != nil {
        fmt.Printf("Error : %s", err)
    }

    return json.NewDecoder(resp.Body)
}


func updateNodesStats(config *Config) {
    for _, node := range *config.Nodes {

        if len(node.Interval) == 0 {
            node.Interval = config.Interval
        }
        go runRequestLoop(node)
    }
}

func runRequestLoop(node Node) {
    for {
        decoder := getOverview(node.Url, node.Uname, node.Password)
        metrics := unpackMetrics(decoder)

        updateMetrics(metrics)

        dt, err := time.ParseDuration(node.Interval)
        if err != nil {
            log.Warningln(err)
            dt = 30 * time.Second
        }
        time.Sleep(dt)
    }
}

func updateMetrics(metrics map[string]float64) {
    channelsTotal.Set(metrics["channels"])
    connectionsTotal.Set(metrics["connections"])
    consumersTotal.Set(metrics["consumers"])
    queuesTotal.Set(metrics["queues"])
    exchangesTotal.Set(metrics["exchanges"])
}


func newConfig(path string) (*Config, error) {
    var config Config

    file, err := ioutil.ReadFile(path)
    if err != nil {
        return nil, err
    }
    err = json.Unmarshal(file, &config)
    return &config, err
}


func main() {
    log.Out = os.Stdout
    config, _ := newConfig(configPath)
    updateNodesStats(config)

    http.Handle("/metrics", prometheus.Handler())
    log.Infof("Starting RabbitMQ exporter on port: %s.\n", config.Port)
    http.ListenAndServe(":" + config.Port, nil)
}

// Register metrics to Prometheus
func init() {
    prometheus.MustRegister(channelsTotal)
    prometheus.MustRegister(connectionsTotal)
    prometheus.MustRegister(queuesTotal)
    prometheus.MustRegister(exchangesTotal)
    prometheus.MustRegister(consumersTotal)
}

{'fraction_non_alphanumeric': 0.11905452436194895, 'fraction_numerical': 0.0031902552204176333, 'mean_word_length': 2.304743651173934, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '427316', 'n_tokens_mistral': 2177, 'n_tokens_neox': 2073, 'n_words': 448}