Skip to content

Commit

Permalink
service label to service_name & service_id labels (#6)
Browse files Browse the repository at this point in the history
* service label to service_name & service_id labels

* making flag name and getServiceNames functionality explicit

* reordering service id & name ;)
  • Loading branch information
mrnetops authored and peterbourgon committed Oct 23, 2018
1 parent e67554b commit 0f5e615
Show file tree
Hide file tree
Showing 4 changed files with 204 additions and 204 deletions.
20 changes: 10 additions & 10 deletions fastly.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,27 @@ import (
"github.com/go-kit/kit/log/level"
)

func getServiceNames(token string, services []string, logger log.Logger) map[string]string {
func getServiceNames(token string, serviceIDs []string, logger log.Logger) map[string]string {
serviceNames := map[string]string{}
for _, service := range services {
serviceNames[service] = getServiceName(token, service, log.With(logger, "service", service))
for _, serviceID := range serviceIDs {
serviceNames[serviceID] = getServiceName(token, serviceID, log.With(logger, "service_id", serviceID))
}
return serviceNames
}

func getServiceName(token, service string, logger log.Logger) string {
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.fastly.com/service/%s/details", url.QueryEscape(service)), nil)
func getServiceName(token, serviceID string, logger log.Logger) string {
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.fastly.com/service/%s/details", url.QueryEscape(serviceID)), nil)
if err != nil {
level.Error(logger).Log("err", err)
return service
return serviceID
}

req.Header.Set("Fastly-Key", token)
req.Header.Set("Accept", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
level.Error(logger).Log("err", err)
return service
return serviceID
}
defer resp.Body.Close()

Expand All @@ -41,11 +41,11 @@ func getServiceName(token, service string, logger log.Logger) string {
}
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
level.Error(logger).Log("err", err)
return service
return serviceID
}
if response.Name == "" {
level.Error(logger).Log("err", "Fastly returned blank service name")
return service
return serviceID
}

return response.Name
Expand Down Expand Up @@ -85,7 +85,7 @@ func queryLoop(ctx context.Context, token, serviceID, serviceName string, m *pro
rterr = "<none>"
}
level.Debug(logger).Log("response_ts", rt.Timestamp, "err", rterr)
process(rt, serviceName, m)
process(rt, serviceID, serviceName, m)
ts = rt.Timestamp
}
}
Expand Down
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ var version = "dev"
func main() {
fs := flag.NewFlagSet("fastly-exporter", flag.ExitOnError)
var (
token = fs.String("token", "", "Fastly API token")
services = stringslice{}
addr = fs.String("endpoint", "http://127.0.0.1:8080/metrics", "Prometheus /metrics endpoint")
namespace = fs.String("namespace", "", "Prometheus namespace")
subsystem = fs.String("subsystem", "", "Prometheus subsystem")
debug = fs.Bool("debug", false, "log debug information")
token = fs.String("token", "", "Fastly API token")
serviceIDs = stringslice{}
addr = fs.String("endpoint", "http://127.0.0.1:8080/metrics", "Prometheus /metrics endpoint")
namespace = fs.String("namespace", "", "Prometheus namespace")
subsystem = fs.String("subsystem", "", "Prometheus subsystem")
debug = fs.Bool("debug", false, "log debug information")
)
fs.Var(&services, "service", "Fastly service (repeatable)")
fs.Var(&serviceIDs, "service", "Fastly service ID (repeatable)")
fs.Usage = usageFor(fs, "fastly-exporter [flags]")
fs.Parse(os.Args[1:])

Expand All @@ -49,13 +49,13 @@ func main() {
level.Error(logger).Log("err", "-token is required")
os.Exit(1)
}
if len(services) <= 0 {
level.Error(logger).Log("err", "at least one -service is required")
if len(serviceIDs) <= 0 {
level.Error(logger).Log("err", "at least one -service ID is required")
os.Exit(1)
}

level.Debug(logger).Log("msg", "looking up service names")
serviceNames := getServiceNames(*token, services, log.With(logger, "query", "api.fastly.com"))
serviceNames := getServiceNames(*token, serviceIDs, log.With(logger, "query", "api.fastly.com"))
for service, name := range serviceNames {
level.Info(logger).Log("fastly_service", service, "name", name)
}
Expand Down
Loading

0 comments on commit 0f5e615

Please sign in to comment.