Skip to content

Commit

Permalink
glog -> logrus
Browse files Browse the repository at this point in the history
  • Loading branch information
vikramraman committed Feb 12, 2020
1 parent 6727540 commit fa78d1b
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 49 deletions.
35 changes: 26 additions & 9 deletions cmd/wavefront-adapter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,17 @@ package main

import (
"flag"
"k8s.io/client-go/kubernetes"
"net/url"
"os"
"runtime"
"time"

log "github.com/sirupsen/logrus"

"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/apiserver/pkg/util/logs"
"k8s.io/client-go/kubernetes"

"github.com/golang/glog"
basecmd "github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/cmd"
customprovider "github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"

Expand Down Expand Up @@ -55,31 +56,33 @@ type WavefrontAdapter struct {
CustomMetricPrefix string
// The file containing the metrics discovery configuration
AdapterConfigFile string
// The log level
LogLevel string
}

func (a *WavefrontAdapter) makeProviderOrDie() customprovider.MetricsProvider {
conf, err := a.ClientConfig()
if err != nil {
glog.Fatalf("error getting kube config: %v", err)
log.Fatalf("error getting kube config: %v", err)
}
kubeClient, err := kubernetes.NewForConfig(conf)
if err != nil {
glog.Fatalf("error creating kube client: %v", err)
log.Fatalf("error creating kube client: %v", err)
}

dynClient, err := a.DynamicClient()
if err != nil {
glog.Fatalf("unable to construct dynamic client: %v", err)
log.Fatalf("unable to construct dynamic client: %v", err)
}

mapper, err := a.RESTMapper()
if err != nil {
glog.Fatalf("unable to construct discovery REST mapper: %v", err)
log.Fatalf("unable to construct discovery REST mapper: %v", err)
}

waveURL, err := url.Parse(a.WavefrontServerURL)
if err != nil {
glog.Fatalf("unable to parse wavefront url: %v", err)
log.Fatalf("unable to parse wavefront url: %v", err)
}
waveClient := client.NewWavefrontClient(waveURL, a.WavefrontAPIToken)

Expand All @@ -97,6 +100,10 @@ func (a *WavefrontAdapter) makeProviderOrDie() customprovider.MetricsProvider {
}

func main() {
log.SetFormatter(&log.TextFormatter{})
log.SetLevel(log.InfoLevel)
log.SetOutput(os.Stdout)

logs.InitLogs()
defer logs.FlushLogs()

Expand All @@ -120,16 +127,26 @@ func main() {
"Wavefront Kubernetes Metrics Prefix")
flags.StringVar(&cmd.AdapterConfigFile, "external-metrics-config", "",
"Configuration file for driving external metrics API")
flags.StringVar(&cmd.LogLevel, "log-level", "info", "one of info, debug or trace")
flags.StringVar(&cmd.Message, "msg", "starting wavefront adapter", "startup message")
flags.AddGoFlagSet(flag.CommandLine) // make sure we get the glog flags
flags.Parse(os.Args)

switch cmd.LogLevel {
case "trace":
log.SetLevel(log.TraceLevel)
case "debug":
log.SetLevel(log.DebugLevel)
case "warn":
log.SetLevel(log.WarnLevel)
}

wavefrontProvider := cmd.makeProviderOrDie()
cmd.WithCustomMetrics(wavefrontProvider)
cmd.WithExternalMetrics(wavefrontProvider)

glog.Infof("%s version: %s commit tip: %s", cmd.Message, version, commit)
log.Infof("%s version: %s commit tip: %s", cmd.Message, version, commit)
if err := cmd.Run(wait.NeverStop); err != nil {
glog.Fatalf("unable to run custom metrics adapter: %v", err)
log.Fatalf("unable to run custom metrics adapter: %v", err)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ spec:
- --metrics-relist-interval=15m
- --external-metrics-config=/etc/adapter/config.yaml
- --logtostderr=true
- --v=5
ports:
- containerPort: 6443
volumeMounts:
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ require (
github.com/coreos/go-semver v0.0.0-20150304020126-568e959cd898 // indirect
github.com/coreos/go-systemd v0.0.0-20161114122254-48702e0da86b // indirect
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f // indirect
github.com/davecgh/go-spew v1.1.1-0.20170626231645-782f4967f2dc // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/elazarl/go-bindata-assetfs v0.0.0-20150624150248-3dcc96556217 // indirect
github.com/emicklei/go-restful v2.2.1+incompatible // indirect
Expand Down Expand Up @@ -53,6 +52,7 @@ require (
github.com/prometheus/client_model v0.0.0-20150212101744-fa8ad6fec335 // indirect
github.com/prometheus/common v0.0.0-20170427095455-13ba4ddd0caa // indirect
github.com/prometheus/procfs v0.0.0-20170519190837-65c1f6f8f0fc // indirect
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.2-0.20180319062004-c439c4fa0937 // indirect
github.com/spf13/pflag v1.0.1 // indirect
github.com/stretchr/testify v1.4.0 // indirect
Expand All @@ -66,7 +66,7 @@ require (
gopkg.in/natefinch/lumberjack.v2 v2.0.0-20150622162204-20b71e5b60d7 // indirect
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0 // indirect
gopkg.in/yaml.v2 v2.2.4
k8s.io/api v0.0.0-20180621030718-91b2d7a92a89 // indirect
k8s.io/api v0.0.0-20180621030718-91b2d7a92a89
k8s.io/apimachinery v0.0.0-20180608150058-fda675fbe852
k8s.io/apiserver v0.0.0-20180621034955-44b612291bb7
k8s.io/client-go v0.0.0-20180621033303-4cacfee698b0
Expand Down
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfc
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1-0.20170626231645-782f4967f2dc h1:NlbIJbqL8zjb55Vdrsr5uqyVC6/NoUUd2YrLojfE2zI=
github.com/davecgh/go-spew v1.1.1-0.20170626231645-782f4967f2dc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/elazarl/go-bindata-assetfs v0.0.0-20150624150248-3dcc96556217 h1:y2RUuraT/W7WKNBxj2QDThNkJ+dQpNS9kEk3Y75S3mk=
Expand Down Expand Up @@ -90,6 +92,7 @@ github.com/jonboulle/clockwork v0.1.0 h1:VKV+ZcuP6l3yW9doeqz6ziZGgcynBVQO+obU0+0
github.com/jonboulle/clockwork v0.1.0/go.mod h1:Ii8DK3G1RaLaWxj9trq07+26W01tbo22gdxWY5EU2bo=
github.com/json-iterator/go v0.0.0-20180424004623-2ddf6d758266 h1:UMlGe18KUyX1k5TDVPqzuMZZMi0xPuMxsVNoQ1im0Yk=
github.com/json-iterator/go v0.0.0-20180424004623-2ddf6d758266/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down Expand Up @@ -127,10 +130,14 @@ github.com/prometheus/common v0.0.0-20170427095455-13ba4ddd0caa/go.mod h1:daVV7q
github.com/prometheus/procfs v0.0.0-20170519190837-65c1f6f8f0fc h1:eEx6/InsHC/w5bo5ADfs4u7uf7NXgmDDui12UF205Ag=
github.com/prometheus/procfs v0.0.0-20170519190837-65c1f6f8f0fc/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
github.com/spf13/cobra v0.0.2-0.20180319062004-c439c4fa0937/go.mod h1:1l0Ry5zgKvJasoi3XT1TypsSe7PqH0Sj9dhYf7v3XqQ=
github.com/spf13/pflag v1.0.1 h1:aCvUg6QPl3ibpQUxyLkrEkCHtPqYJL4x9AuhqVqFis4=
github.com/spf13/pflag v1.0.1/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go v0.0.0-20170107133203-ded73eae5db7 h1:BPPUhSq7uU6E9lFzyb81vjwVOhiWwMXp0EpKL75NX+8=
Expand Down Expand Up @@ -168,6 +175,8 @@ golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.0.0-20170810154203-b19bf474d317 h1:WKW+OPdYPlvOTVGHuMfjnIC6yY2SI93yFB0pZ7giBmQ=
golang.org/x/text v0.0.0-20170810154203-b19bf474d317/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
Expand Down
12 changes: 7 additions & 5 deletions pkg/client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package client
import (
"encoding/json"
"fmt"
"github.com/golang/glog"
"io"
"net/http"
"net/url"
"path"
"strconv"
"time"

log "github.com/sirupsen/logrus"
)

type WavefrontClient interface {
Expand Down Expand Up @@ -50,7 +51,7 @@ func (w DefaultWavefrontClient) Do(verb, endpoint string, query url.Values) (*ht
u.Path = path.Join(u.Path, endpoint)
u.RawQuery = query.Encode()

glog.V(4).Infof("DEBUG:---DefaultWavefrontClient.Do, query: %s", u.String())
log.Debugf("DefaultWavefrontClient.Do, query: %s", u.String())

req, err := http.NewRequest(verb, u.String(), nil)
if err != nil {
Expand All @@ -72,7 +73,7 @@ func (w DefaultWavefrontClient) Do(verb, endpoint string, query url.Values) (*ht
}

func (w DefaultWavefrontClient) ListMetrics(prefix string) ([]string, error) {
glog.V(4).Info("DEBUG:---DefaultWavefrontClient.ListMetrics")
log.Debugf("DefaultWavefrontClient.ListMetrics")

vals := url.Values{}
vals.Set("m", prefix)
Expand All @@ -92,12 +93,12 @@ func (w DefaultWavefrontClient) ListMetrics(prefix string) ([]string, error) {
Msg: err.Error(),
}
}
glog.V(8).Info("DEBUG:---DefaultWavefrontClient.ListMetrics", result.Metrics)
log.Trace("DefaultWavefrontClient.ListMetrics", result.Metrics)
return result.Metrics, nil
}

func (w DefaultWavefrontClient) Query(start int64, query string) (QueryResult, error) {
glog.V(4).Infof("DEBUG:---WavefrontClient.Query: start=%d, query=%s", start, query)
log.Debugf("DefaultWavefrontClient.Query: start=%d, query=%s", start, query)
if query == "" {
return QueryResult{}, &Error{
Type: ErrBadData,
Expand Down Expand Up @@ -125,5 +126,6 @@ func (w DefaultWavefrontClient) Query(start int64, query string) (QueryResult, e
Msg: err.Error(),
}
}
log.Trace("DefaultWavefrontClient.Query", result)
return result, nil
}
14 changes: 7 additions & 7 deletions pkg/provider/external.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import (
"sync"
"time"

"github.com/golang/glog"
log "github.com/sirupsen/logrus"

"github.com/wavefronthq/wavefront-kubernetes-adapter/pkg/config"

"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -41,17 +42,16 @@ func NewExternalMetricsDriver(client kubernetes.Interface, cfgFile string) Exter
}

func (d *WavefrontExternalDriver) loadConfig() {
//TODO: don't quit if failed to load config?
go wait.Until(func() {
fileInfo, err := os.Stat(d.cfgFile)
if err != nil {
glog.Fatalf("unable to get external config file stats: %v", err)
log.Fatalf("unable to get external config file stats: %v", err)
}

if fileInfo.ModTime().After(d.cfgModTime) {
metricsConfig, err := config.FromFile(d.cfgFile)
if err != nil {
glog.Fatalf("unable to load external metrics discovery configuration: %v", err)
log.Fatalf("unable to load external metrics discovery configuration: %v", err)
}
d.cfgModTime = fileInfo.ModTime()
d.addRules(metricsConfig.Rules)
Expand All @@ -74,7 +74,7 @@ func (d *WavefrontExternalDriver) addRules(rules []config.MetricRule) {
if d.listener != nil {
d.listener.configChanged()
}
glog.V(5).Info("added external metrics rules", rules)
log.Debugf("added external metrics rules: %v", rules)
}

func (d *WavefrontExternalDriver) deleteRules(rules []config.MetricRule) {
Expand All @@ -92,14 +92,14 @@ func (d *WavefrontExternalDriver) deleteRules(rules []config.MetricRule) {
if d.listener != nil {
d.listener.configChanged()
}
glog.V(5).Info("deleted external metrics rules", rules)
log.Debugf("deleted external metrics rules: %v", rules)
}

func (d *WavefrontExternalDriver) registerListener(listener ExternalConfigListener) {
d.lock.RLock()
defer d.lock.RUnlock()
d.listener = listener
glog.V(5).Info("External configuration listener registered")
log.Info("external configuration listener registered")
}

func (d *WavefrontExternalDriver) getMetricNames() []string {
Expand Down
13 changes: 8 additions & 5 deletions pkg/provider/hpa.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package provider

import (
"github.com/golang/glog"
"reflect"
"strings"

log "github.com/sirupsen/logrus"

"github.com/wavefronthq/wavefront-kubernetes-adapter/pkg/config"

"k8s.io/api/autoscaling/v2beta1"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"reflect"
"strings"
)

const (
Expand All @@ -33,7 +36,7 @@ func StartHPAListener(client kubernetes.Interface, addFunc, deleteFunc RuleHandl
}

func (l *hpaListener) listen() {
glog.V(2).Info("listening for HPA instances")
log.Info("listening for HPA instances")

rc := l.kubeClient.AutoscalingV2beta1().RESTClient()
lw := cache.NewListWatchFromClient(rc, "horizontalpodautoscalers", v1.NamespaceAll, fields.Everything())
Expand All @@ -54,7 +57,7 @@ func (l *hpaListener) listen() {
// HPA objects are updated frequently when status changes
// validate if annotations have changed
if reflect.DeepEqual(oldHPA.Annotations, newHPA.Annotations) {
glog.V(5).Infof("annotations have not changed for %s", newHPA.Name)
log.Debugf("annotations have not changed for %s", newHPA.Name)
return
}

Expand Down
10 changes: 5 additions & 5 deletions pkg/provider/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"sync"
"time"

log "github.com/sirupsen/logrus"

"k8s.io/apimachinery/pkg/util/wait"

"github.com/golang/glog"
"github.com/kubernetes-incubator/custom-metrics-apiserver/pkg/provider"

wave "github.com/wavefronthq/wavefront-kubernetes-adapter/pkg/client"
)

Expand Down Expand Up @@ -37,7 +37,7 @@ type WavefrontMetricsLister struct {
}

func (l *WavefrontMetricsLister) configChanged() {
glog.V(5).Info("configuration changed. updating metrics.")
log.Info("configuration changed. updating metrics.")
l.updateMetrics()
}

Expand All @@ -51,7 +51,7 @@ func (l *WavefrontMetricsLister) RunUntil(stopChan <-chan struct{}) {

go wait.Until(func() {
if err := l.updateMetrics(); err != nil {
glog.Errorf("error updating metrics: %v", err)
log.Errorf("error updating metrics: %v", err)
}
}, l.UpdateInterval, stopChan)
}
Expand All @@ -71,7 +71,7 @@ func (l *WavefrontMetricsLister) updateMetrics() error {
func (l *WavefrontMetricsLister) updateCustomMetrics() error {
metrics, err := l.waveClient.ListMetrics(l.Prefix + ".*")
if err != nil {
glog.Errorf("error retrieving list of custom metrics from Wavefront: %v", err)
log.Errorf("error retrieving list of custom metrics from Wavefront: %v", err)
l.customMetrics = []provider.CustomMetricInfo{}
return err
}
Expand Down
Loading

0 comments on commit fa78d1b

Please sign in to comment.