<commit_before>package main

import (
	"fmt"
	"os"
	"reflect"
	"regexp"
	//"sort"
	"errors"
	"strings"
	"sync"
	"time"

	"net/url"

	"flag"
	"github.com/golang/glog"

	"encoding/json"
	"runtime"

	"golang.org/x/net/context"

	"github.com/signalfx/golib/datapoint"
	"github.com/signalfx/metricproxy/protocol/signalfx"

	"github.com/codegangsta/cli"
	"github.com/goinggo/workpool"
	kubeAPI "k8s.io/kubernetes/pkg/api"
	kube "k8s.io/kubernetes/pkg/client/unversioned"
	kubeFields "k8s.io/kubernetes/pkg/fields"
	kubeLabels "k8s.io/kubernetes/pkg/labels"

	"github.com/google/cadvisor/client"
	info "github.com/google/cadvisor/info/v1"
)

// Set by build system
var toolVersion = "NOT SET"

// Config for prometheusScraper
type Config struct {
	IngestURL              string
	CadvisorURL            []string
	APIToken               string
	DataSendRate           string
	ClusterName            string
	NodeServiceRefreshRate string
	CadvisorPort           int
}

type prometheusScraper struct {
	forwarder *signalfx.Forwarder
	cfg       *Config
}

// workProxy will call work.DoWork and then callback
/*type workProxy struct {
	work     workpool.PoolWorker
	callback func()
}

func (wp *workProxy) DoWork(workRoutine int) {
	wp.work.DoWork(workRoutine)
	wp.callback()
}*/

type scrapWork2 struct {
	serverURL  string
	collector  *CadvisorCollector
	chRecvOnly chan datapoint.Datapoint
}

func (scrapWork *scrapWork2) DoWork(workRoutine int) {
	scrapWork.collector.Collect(scrapWork.chRecvOnly)
}

/*type sortableDatapoint []*datapoint.Datapoint

func (sd sortableDatapoint) Len() int {
	return len(sd)
}

func (sd sortableDatapoint) Swap(i, j int) {
	sd[i], sd[j] = sd[j], sd[i]
}

func (sd sortableDatapoint) Less(i, j int) bool {
	return sd[i].Timestamp.Unix() < sd[j].Timestamp.Unix()
}*/

type cadvisorInfoProvider struct {
	cc         *client.Client
	lastUpdate time.Time
}

func (cip *cadvisorInfoProvider) SubcontainersInfo(containerName string) ([]info.ContainerInfo, error) {
	curTime := time.Now()
	info, err := cip.cc.AllDockerContainers(&info.ContainerInfoRequest{Start: cip.lastUpdate, End: curTime})
	if len(info) > 0 {
		cip.lastUpdate = curTime
	}
	return info, err
}

func (cip *cadvisorInfoProvider) GetMachineInfo() (*info.MachineInfo, error) {
	return cip.cc.MachineInfo()
}

func newCadvisorInfoProvider(cadvisorClient *client.Client) *cadvisorInfoProvider {
	return &cadvisorInfoProvider{
		cc:         cadvisorClient,
		lastUpdate: time.Now(),
	}
}

const autoFlushTimerDuration = 500 * time.Millisecond
const maxDatapoints = 50

const ingestURL = "ingestURL"
const apiToken = "apiToken"
const dataSendRate = "sendRate"
const nodeServiceDiscoveryRate = "nodeServiceDiscoveryRate"
const clusterName = "clusterName"
const cadvisorPort = "cadvisorPort"

var dataSendRates = map[string]time.Duration{
	"5s":  5 * time.Second,
	"10s": 10 * time.Second,
	"30s": 30 * time.Second,
	"1m":  time.Minute,
	"5m":  5 * time.Minute,
}

var nodeServiceDiscoveryRates = map[string]time.Duration{
	"3m":  3 * time.Minute,
	"5m":  5 * time.Minute,
	"10m": 10 * time.Minute,
	"15m": 15 * time.Minute,
	"20m": 20 * time.Minute,
}

func printVersion() {
	glog.Infof("git build commit: %v\n", toolVersion)
}

func main() {
	flag.Parse()
	app := cli.NewApp()
	app.Name = "prometheustosfx"
	app.Usage = "scraps metrics from cAdvisor and forwards them to SignalFx."
	app.Version = "git commit: " + toolVersion

	app.Flags = []cli.Flag{

		cli.StringFlag{
			Name:  ingestURL,
			Value: "https://ingest.signalfx.com",
			Usage: "SignalFx ingest URL.",<commit_msg>enhance integration to send to signalbox based on environment variable if desired<commit_after>package main

import (
	"fmt"
	"os"
	"reflect"
	"regexp"
	//"sort"
	"errors"
	"strings"
	"sync"
	"time"

	"net/url"

	"flag"
	"github.com/golang/glog"

	"encoding/json"
	"runtime"

	"golang.org/x/net/context"

	"github.com/signalfx/golib/datapoint"
	"github.com/signalfx/metricproxy/protocol/signalfx"

	"github.com/codegangsta/cli"
	"github.com/goinggo/workpool"
	kubeAPI "k8s.io/kubernetes/pkg/api"
	kube "k8s.io/kubernetes/pkg/client/unversioned"
	kubeFields "k8s.io/kubernetes/pkg/fields"
	kubeLabels "k8s.io/kubernetes/pkg/labels"

	"github.com/google/cadvisor/client"
	info "github.com/google/cadvisor/info/v1"
)

// Set by build system
var toolVersion = "NOT SET"

// Config for prometheusScraper
type Config struct {
	IngestURL              string
	CadvisorURL            []string
	APIToken               string
	DataSendRate           string
	ClusterName            string
	NodeServiceRefreshRate string
	CadvisorPort           int
}

type prometheusScraper struct {
	forwarder *signalfx.Forwarder
	cfg       *Config
}

// workProxy will call work.DoWork and then callback
/*type workProxy struct {
	work     workpool.PoolWorker
	callback func()
}

func (wp *workProxy) DoWork(workRoutine int) {
	wp.work.DoWork(workRoutine)
	wp.callback()
}*/

type scrapWork2 struct {
	serverURL  string
	collector  *CadvisorCollector
	chRecvOnly chan datapoint.Datapoint
}

func (scrapWork *scrapWork2) DoWork(workRoutine int) {
	scrapWork.collector.Collect(scrapWork.chRecvOnly)
}

/*type sortableDatapoint []*datapoint.Datapoint

func (sd sortableDatapoint) Len() int {
	return len(sd)
}

func (sd sortableDatapoint) Swap(i, j int) {
	sd[i], sd[j] = sd[j], sd[i]
}

func (sd sortableDatapoint) Less(i, j int) bool {
	return sd[i].Timestamp.Unix() < sd[j].Timestamp.Unix()
}*/

type cadvisorInfoProvider struct {
	cc         *client.Client
	lastUpdate time.Time
}

func (cip *cadvisorInfoProvider) SubcontainersInfo(containerName string) ([]info.ContainerInfo, error) {
	curTime := time.Now()
	info, err := cip.cc.AllDockerContainers(&info.ContainerInfoRequest{Start: cip.lastUpdate, End: curTime})
	if len(info) > 0 {
		cip.lastUpdate = curTime
	}
	return info, err
}

func (cip *cadvisorInfoProvider) GetMachineInfo() (*info.MachineInfo, error) {
	return cip.cc.MachineInfo()
}

func newCadvisorInfoProvider(cadvisorClient *client.Client) *cadvisorInfoProvider {
	return &cadvisorInfoProvider{
		cc:         cadvisorClient,
		lastUpdate: time.Now(),
	}
}

const autoFlushTimerDuration = 500 * time.Millisecond
const maxDatapoints = 50

const ingestURL = "ingestURL"
const apiToken = "apiToken"
const dataSendRate = "sendRate"
const nodeServiceDiscoveryRate = "nodeServiceDiscoveryRate"
const clusterName = "clusterName"
const cadvisorPort = "cadvisorPort"

var dataSendRates = map[string]time.Duration{
	"5s":  5 * time.Second,
	"10s": 10 * time.Second,
	"30s": 30 * time.Second,
	"1m":  time.Minute,
	"5m":  5 * time.Minute,
}

var nodeServiceDiscoveryRates = map[string]time.Duration{
	"3m":  3 * time.Minute,
	"5m":  5 * time.Minute,
	"10m": 10 * time.Minute,
	"15m": 15 * time.Minute,
	"20m": 20 * time.Minute,
}

func printVersion() {
	glog.Infof("git build commit: %v\n", toolVersion)
}

func main() {
	flag.Parse()
	app := cli.NewApp()
	app.Name = "prometheustosfx"
	app.Usage = "scraps metrics from cAdvisor and forwards them to SignalFx."
	app.Version = "git commit: " + toolVersion

	app.Flags = []cli.Flag{

		cli.StringFlag{
			Name:   ingestURL,
			Value:  "https://ingest.signalfx.com",
			EnvVar: "SFX_SCRAPPER_INGEST_URL",
			Usage:  "SignalFx ingest URL.",
{'fraction_non_alphanumeric': 0.12626332548802435, 'fraction_numerical': 0.011629516821265402, 'mean_word_length': 4.108910891089109, 'pattern_counts': {'":': 20, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '6241693', 'n_tokens_mistral': 2809, 'n_tokens_neox': 2682, 'n_words': 576}