<commit_before>/*
Copyright 2017 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package collector

import (
	"net"
	"net/http"
	"net/url"

	"github.com/gravitational/roundtrip"
	"github.com/gravitational/trace"
	"github.com/prometheus/client_golang/prometheus"
)

const (
	dockerSocketPath = "/var/run/docker.sock"
	dockerURL        = "http://docker/"
)

// DockerCollector collect metrics about docker service status
type DockerCollector struct {
	client   *roundtrip.Client
	dockerUp typedDesc
}

// NewDockerCollector returns initialized DockerCollector
func NewDockerCollector() (*DockerCollector, error) {
	transport := &http.Transport{
		Dial: func(network, addr string) (net.Conn, error) {
			return net.Dial("unix", dockerSocketPath)
		},
	}
	client, err := NewRoundtripClient(dockerURL, roundtrip.HTTPClient(&http.Client{
		Transport: transport,
		Timeout:   collectMetricsTimeout,
	}))
	if err != nil {
		return nil, trace.Wrap(err)
	}

	return &DockerCollector{
		client: client,
		dockerUp: typedDesc{prometheus.NewDesc(
			prometheus.BuildFQName(namespace, "docker", "health"),
			"Status of Docker daemon",
			nil, nil,
		), prometheus.GaugeValue},
	}, nil
}

// Collect implements the prometheus.Collector interface
func (d *DockerCollector) Collect(ch chan<- prometheus.Metric) error {
	healthy, err := d.healtStatus()
	if err != nil {
		return trace.Wrap(err)
	}
	if healthy {
		ch <- d.dockerUp.mustNewConstMetric(1.0)
		return nil
	}

	ch <- d.dockerUp.mustNewConstMetric(0.0)
	return nil
}

// healthStatus determines status of docker service
// by checking url http://docker/version from daemon socket
func (d *DockerCollector) healtStatus() (bool, error) {
	resp, err := d.client.Get(d.client.Endpoint("version"), url.Values{})
	if err != nil {
		return false, trace.Wrap(err, "HTTP request failed: %v", err)<commit_msg>adressing CR<commit_after>/*
Copyright 2017 Gravitational, Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package collector

import (
	"net"
	"net/http"
	"net/url"

	"github.com/gravitational/roundtrip"
	"github.com/gravitational/trace"
	"github.com/prometheus/client_golang/prometheus"
)

const (
	dockerSocketPath = "/var/run/docker.sock"
	dockerURL        = "http://docker/"
)

// DockerCollector collect metrics about docker service status
type DockerCollector struct {
	client   *roundtrip.Client
	dockerUp typedDesc
}

// NewDockerCollector returns initialized DockerCollector
func NewDockerCollector() (*DockerCollector, error) {
	transport := &http.Transport{
		Dial: func(network, addr string) (net.Conn, error) {
			return net.Dial("unix", dockerSocketPath)
		},
	}
	client, err := NewRoundtripClient(dockerURL, roundtrip.HTTPClient(&http.Client{
		Transport: transport,
		Timeout:   collectMetricsTimeout,
	}))
	if err != nil {
		return nil, trace.Wrap(err)
	}

	return &DockerCollector{
		client: client,
		dockerUp: typedDesc{prometheus.NewDesc(
			prometheus.BuildFQName(namespace, "docker", "health"),
			"Status of Docker daemon",
			nil, nil,
		), prometheus.GaugeValue},
	}, nil
}

// Collect is called by the Prometheus registry when collecting metrics.
func (d *DockerCollector) Collect(ch chan<- prometheus.Metric) error {
	var metric prometheus.Metric

	healthy, err := d.healthStatus()
	if err != nil {
		return trace.Wrap(err)
	}
	if healthy {
		if metric, err = d.dockerUp.newConstMetric(1.0); err != nil {
			return trace.Wrap(err, "failed to create prometheus metric: %v", err)
		}
	} else {
		if metric, err = d.dockerUp.newConstMetric(0.0); err != nil {
			return trace.Wrap(err, "failed to create prometheus metric: %v", err)
		}
	}

	ch <- metric
	return nil
}

// healthStatus determines status of docker service
// by fetching the docker's version HTTP endpoint from daemon socket
func (d *DockerCollector) healthStatus() (bool, error) {
	resp, err := d.client.Get(d.client.Endpoint("version"), url.Values{})
	if err != nil {
		return false, trace.Wrap(err, "HTTP request failed: %v", err)
{'fraction_non_alphanumeric': 0.11023622047244094, 'fraction_numerical': 0.004845548152634767, 'mean_word_length': 4.355675675675676, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '6250163', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1515, 'n_words': 488}