-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
115 lines (99 loc) · 3.31 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
// general
"errors"
"fmt"
"net/http"
"os"
"strconv"
"strings"
// Edgecast Client
"github.com/mre/edgecast"
// Prometheus for logging/metrics
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
// go-kit
"github.com/go-kit/kit/log"
kitprometheus "github.com/go-kit/kit/metrics/prometheus"
)
var (
// Platforms maps all possible media-types/platforms to it's IDs used in a request
Platforms = map[int]string{
2: "flash",
3: "http_large",
7: "ssl_http_large",
8: "http_small",
9: "ssl_http_small",
14: "adn",
15: "ssl_adn",
}
// user-defined environment-variables that handle access to the API
accountID = os.Getenv("EDGECAST_ACCOUNT_ID")
token = os.Getenv("EDGECAST_TOKEN")
platformsEnv = os.Getenv("EDGECAST_PLATFORMS")
)
func main() {
// check if account ID and token were properly specified using the environment variables
if len(accountID) == 0 || len(token) == 0 {
fmt.Println(errors.New("error: empty Account-ID or Token!\n-> Please specify using environment variables EDGECAST_ACCOUNT_ID and EDGECAST_TOKEN"))
os.Exit(1)
}
// Check if we can get the platforms to monitor from the environment.
// If not, use the default (monitor all platforms).
var platforms map[int]string
if len(platformsEnv) == 0 {
platforms = Platforms
} else {
split := strings.Split(platformsEnv, ",")
platforms := make(map[int]string, len(split))
for _, s := range split {
i, err := strconv.Atoi(s)
if err != nil {
fmt.Println(fmt.Errorf("Invalid platform: %s", s))
os.Exit(1)
}
if val, ok := Platforms[i]; ok {
platforms[i] = val
} else {
fmt.Println(fmt.Errorf("Invalid platform: %s", s))
os.Exit(1)
}
}
}
// create new logger on Stderr
logger := log.NewLogfmtLogger(os.Stderr)
// Prometheus metrics settings for this service
fieldKeys := []string{"method", "error"} // label names
requestCount := kitprometheus.NewCounterFrom(prometheus.CounterOpts{
Namespace: "Edgecast",
Subsystem: "service_metrics",
Name: "request_count",
Help: "Number of requests received.",
}, fieldKeys)
requestLatency := kitprometheus.NewSummaryFrom(prometheus.SummaryOpts{
Namespace: "Edgecast",
Subsystem: "service_metrics",
Name: "request_latency_distribution_seconds",
Help: "Total duration of requests in seconds.",
}, fieldKeys)
requestGauge := kitprometheus.NewGaugeFrom(prometheus.GaugeOpts{
Namespace: "Edgecast",
Subsystem: "service_metrics",
Name: "request_latency_seconds",
Help: "Duration of request in seconds.",
}, fieldKeys)
// create EdgecastClient that communicates with the Edgecast API
var svc EdgecastInterface = edgecast.NewEdgecastClient(accountID, token)
// attach logger to service
svc = loggingMiddleware{logger, svc}
// attach instrumenting middleware
svc = instrumentingMiddleware{requestCount, requestLatency, requestGauge, svc}
// create the prometheus collector that uses the EdgecastClient and register it to prometheus
collector := NewEdgecastCollector(&svc, platforms)
prometheus.MustRegister(collector)
// connect handlers
http.Handle("/metrics", promhttp.Handler())
// set up logger and start service on port 80
_ = logger.Log("msg", "HTTP", "addr", ":80")
_ = logger.Log("err", http.ListenAndServe(":80", nil))
}