Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
Implement metrics.smi-spec.io for Linkerd
Browse files Browse the repository at this point in the history
  • Loading branch information
grampelberg committed May 9, 2019
1 parent ed3bbc7 commit 9971f71
Show file tree
Hide file tree
Showing 27 changed files with 2,594 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
tmp
*.yml
*.yaml
Makefile
Tiltfile
*.md
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,10 @@

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Output of swag generator
pkg/docs

tmp

.vscode
40 changes: 40 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
linters:
enable-all: true
disable:
- maligned
- prealloc
- gochecknoglobals

skip-dirs:
# ignore the system libraries
- libexec/src/strings

linters-settings:
govet:
check-shadowing: true
golint:
min-confidence: 0
gocyclo:
min-complexity: 10
maligned:
suggest-new: true
dupl:
threshold: 100
goconst:
min-len: 2
min-occurrences: 2
misspell:
locale: US
lll:
line-length: 140
goimports:
local-prefixes: github.com/golangci/golangci-lint
gocritic:
enabled-tags:
- performance
- style
- experimental
disabled-checks:
- wrapperFunc
- dupImport # https://github.com/go-critic/go-critic/issues/845
- rangeValCopy
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM golang:1.12-alpine as builder

WORKDIR /src

RUN apk add --no-cache \
git

COPY go.mod .
COPY go.sum .

RUN go mod download

COPY . .

RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./cmd/smi-metrics/...

FROM scratch

WORKDIR /src

COPY --from=builder /src/smi-metrics /

CMD /smi-metrics
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
ROOT_DIR := $(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

HAS_TILT := $(shell command -v tilt;)

.PHONY: bootstrap
bootstrap:
@# Bootstrap the required binaries
ifndef HAS_TILT
echo "Install tilt from https://docs.tilt.dev/install.html"
endif

.PHONY: dev
dev: bootstrap
tilt up
5 changes: 5 additions & 0 deletions Tiltfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
k8s_yaml('k8s/dev.yaml')

docker_build('thomasr/smi-metrics', '.')

k8s_resource('smi-metrics', port_forwards=['8080:8080', '8081:8081'])
20 changes: 20 additions & 0 deletions bin/wait.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/sh -x

exit_script() {
echo "Shutting down..."
trap - EXIT HUP INT QUIT PIPE TERM # clear the trap
kill -- -$$ # Sends SIGTERM to child/sub processes
}

trap exit_script EXIT HUP INT QUIT PIPE TERM

echo "Sleeping. Pid=$$"
sleep 2147483647 &

# Install dev helpers
apk add --no-cache \
alpine-sdk

go mod download

wait $!
222 changes: 222 additions & 0 deletions cmd/smi-metrics/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
package main

import (
"flag"
"fmt"
"strings"

"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
yaml "gopkg.in/yaml.v2"
"k8s.io/klog"

"github.com/deislabs/smi-metrics/pkg/server"
)

var (
globalUsage = "Expose metrics.smi-spec.io"

cmd = &cobra.Command{
Use: "smi-metrics",
Short: "Expose metrics.smi-spec.io",
Long: globalUsage,
Run: run,
}

envRoot = "smi-metrics"
)

// bindFlag moves cobra flags into viper for exclusive use there.
func bindFlag(f *pflag.Flag) error {
v := viper.GetViper()

if err := v.BindPFlag(f.Name, f); err != nil {
return err
}

if err := v.BindEnv(
f.Name,
strings.Replace(
strings.ToUpper(
fmt.Sprintf(
"%s_%s", envRoot, f.Name)), "-", "_", -1)); err != nil {
return err
}

return nil
}

func main() {
if err := cmd.Execute(); err != nil {
log.Fatalf("unable to execute: %s", err)
}
}

func cmdFlags(flags *pflag.FlagSet) error {
flags.String(
"config",
"",
"config file",
)
if err := bindFlag(flags.Lookup("config")); err != nil {
return err
}

flags.String(
"log-level",
"info",
"log level to use",
)
if err := bindFlag(flags.Lookup("log-level")); err != nil {
return err
}

flags.Int(
"admin-port",
8081,
"port listen on for admin related requests",
)
if err := bindFlag(flags.Lookup("admin-port")); err != nil {
return err
}

flags.Int(
"api-port",
8080,
"port listen on for api related requests",
)
if err := bindFlag(flags.Lookup("api-port")); err != nil {
return err
}

flags.String(
"tls-cert-file",
"/var/run/smi-metrics/tls.crt",
"TLS certificate to use",
)
if err := bindFlag(flags.Lookup("tls-cert-file")); err != nil {
return err
}

flags.String(
"tls-private-key",
"/var/run/smi-metrics/tls.key",
"TLS private key to use",
)
if err := bindFlag(flags.Lookup("tls-private-key")); err != nil {
return err
}

flags.String(
"prometheus-url",
"http://prometheus.default.svc.cluster.local:9090",
"URL to use for connecting to prometheus of the format: hostname:port",
)
if err := bindFlag(flags.Lookup("prometheus-url")); err != nil {
return err
}

return nil
}

func logConfig() error {
out, err := yaml.Marshal(viper.AllSettings())
if err != nil {
return err
}

log.Debugf("Configuration:\n---\n%s---", out)

return nil
}

func initConfig() {
flags := cmd.PersistentFlags()
cfgPath, err := flags.GetString("config")
if err != nil {
log.Fatalf("unable to fetch config flag value: %s", err)
}

if cfgPath == "" {
return
}

viper.SetConfigFile(cfgPath)

if err := viper.ReadInConfig(); err != nil {
log.Fatalf("unable to read config: %s", err)
}

viper.WatchConfig()
viper.OnConfigChange(func(e fsnotify.Event) {
log.Infof("Config file %s changed", cfgPath)

if err := logConfig(); err != nil {
log.Fatalf("unable to marshal and output configuration: %s", err)
}
})
}

func initLog() error {
klog.InitFlags(nil)

level, err := log.ParseLevel(viper.GetString("log-level"))
if err != nil {
return err
}

log.SetLevel(level)

if level == log.DebugLevel {
if err := flag.Set("stderrthreshold", "INFO"); err != nil {
return err
}
if err := flag.Set("logtostderr", "true"); err != nil {
return err
}
// At 7 and higher, authorization tokens get logged.
if err := flag.Set("v", "6"); err != nil {
return err
}
}

return nil
}

//nolint:gochecknoinits
func init() {
if err := cmdFlags(cmd.PersistentFlags()); err != nil {
log.Fatalf("unable to parse flags: %s", err)
}

cobra.OnInitialize(func() {
initConfig()
if err := initLog(); err != nil {
log.Fatalf("unable to init logging: %s", err)
}

if err := logConfig(); err != nil {
log.Fatalf("unable to marshal and output configuration: %s", err)
}
})
}

func run(_ *cobra.Command, args []string) {
log.Infof("api listening on %d", viper.GetInt("api-port"))
log.Infof("admin listening on %d", viper.GetInt("admin-port"))

s := server.Server{
APIPort: viper.GetInt("api-port"),
AdminPort: viper.GetInt("admin-port"),
TLSCertificate: viper.GetString("tls-cert-file"),
TLSPrivateKey: viper.GetString("tls-private-key"),
PrometheusURL: viper.GetString("prometheus-url"),
}

if err := s.Listen(); err != nil {
log.Fatalf("Unable to start listening: %s", err)
}
}
36 changes: 36 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module github.com/deislabs/smi-metrics

go 1.12

require (
github.com/Masterminds/goutils v1.1.0 // indirect
github.com/Masterminds/semver v1.4.2 // indirect
github.com/Masterminds/sprig v2.18.0+incompatible // indirect
github.com/davecgh/go-spew v1.1.1
github.com/deislabs/smi-sdk-go v0.0.0-20190509175445-11b06e5654f7
github.com/fsnotify/fsnotify v1.4.7
github.com/go-chi/chi v4.0.2+incompatible
github.com/google/uuid v1.1.1 // indirect
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0 // indirect
github.com/hellofresh/health-go v2.0.2+incompatible
github.com/huandu/xstrings v1.2.0 // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/improbable-eng/go-httpwares v0.0.0-20190118142334-33c6690a604c
github.com/masterminds/sprig v2.18.0+incompatible
github.com/prometheus/client_golang v0.9.2
github.com/prometheus/common v0.3.0
github.com/sirupsen/logrus v1.4.1
github.com/spf13/cobra v0.0.3
github.com/spf13/pflag v1.0.3
github.com/spf13/viper v1.3.2
github.com/stretchr/testify v1.3.0
github.com/unrolled/render v1.0.0
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734 // indirect
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8
google.golang.org/grpc v1.20.1 // indirect
gopkg.in/yaml.v2 v2.2.2
k8s.io/api v0.0.0-20190409021203-6e4e0e4f393b
k8s.io/apimachinery v0.0.0-20190404173353-6a84e37a896d
k8s.io/client-go v11.0.0+incompatible
k8s.io/klog v0.3.0
)
Loading

0 comments on commit 9971f71

Please sign in to comment.