Skip to content

Add structured logging #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ require (
golang.org/x/oauth2 v0.0.0-20220822191816-0ebed06d0094
)

require (
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
)

require (
github.com/andybalholm/brotli v1.0.4 // indirect
github.com/beorn7/perks v1.0.1 // indirect
Expand All @@ -32,9 +37,11 @@ require (
github.com/savsgio/gotils v0.0.0-20220530130905-52f3993e8d6d // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
go.uber.org/zap v1.23.0
golang.org/x/crypto v0.0.0-20220214200702-86341886e292 // indirect
golang.org/x/exp v0.0.0-20221012211006-4de253d81b95
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8=
go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.3/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.opencensus.io v0.22.4/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
Expand All @@ -261,6 +267,8 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20221012211006-4de253d81b95 h1:sBdrWpxhGDdTAYNqbgBLAR+ULAPPhfgncLr1X0lyWtg=
golang.org/x/exp v0.0.0-20221012211006-4de253d81b95/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down Expand Up @@ -375,6 +383,8 @@ golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a h1:dGzPydgVsqGcTRVwiLJ1jVbufYwmzD3LfVPLKsKg+0k=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
22 changes: 15 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
package main

import (
"log"
"os"

"github.com/urfave/cli/v2"
"go.uber.org/zap"

"github.com/spendesk/github-actions-exporter/pkg/config"
"github.com/spendesk/github-actions-exporter/pkg/logging"
"github.com/spendesk/github-actions-exporter/pkg/server"
)

var (
version = "development"
logger *zap.SugaredLogger
)

func main() {
app := cli.NewApp()
app.Name = "github-actions-exporter"
app.Flags = config.InitConfiguration()
app.Version = version
app.Action = server.RunServer
app := &cli.App{
Name: "github-actions-exporter",
Flags: config.InitConfiguration(),
Action: func(ctx *cli.Context) error {
logger = logging.InitLogger()
server.RunServer(ctx, logger)
return nil
},
Version: version,
}

err := app.Run(os.Args)

if err != nil {
log.Fatal(err)
logger.Fatal(err)
}
}
8 changes: 8 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var (
Debug bool
EnterpriseName string
WorkflowFields string
LogFormat string
)

// InitConfiguration - set configuration from env vars or command parameters
Expand Down Expand Up @@ -127,5 +128,12 @@ func InitConfiguration() []cli.Flag {
Usage: "Size of Github HTTP cache in bytes",
Destination: &Github.CacheSizeBytes,
},
&cli.StringFlag{
Name: "log_format",
EnvVars: []string{"LOG_FORMAT"},
Usage: "Log output format. Valid formats: json, plain",
Value: "json",
Destination: &LogFormat,
},
}
}
40 changes: 40 additions & 0 deletions pkg/logging/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package logging

import (
"log"

"github.com/spendesk/github-actions-exporter/pkg/config"
"go.uber.org/zap"
"golang.org/x/exp/slices"
)

var logger *zap.SugaredLogger

func validateFormat(text string) {
if !slices.Contains([]string{"json", "plain"}, text) {
logger.Fatalf("Invalid log_format '%v'", text)
}
}

func InitLogger() *zap.SugaredLogger {
var (
freshLogger *zap.Logger
err error
)
if config.LogFormat == "plain" {
freshLogger, err = zap.NewDevelopment()
} else {
freshLogger, err = zap.NewProduction()
}

if err != nil {
log.Fatalf("Can't initialize logger: %v", err)
}

defer freshLogger.Sync()
logger = freshLogger.Sugar()

validateFormat(config.LogFormat)

return logger
}
5 changes: 2 additions & 3 deletions pkg/metrics/get_billable_from_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package metrics

import (
"context"
"log"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -33,11 +32,11 @@ func getBillableFromGithub() {
for {
resp, _, err := client.Actions.GetWorkflowUsageByID(context.Background(), r[0], r[1], k)
if rl_err, ok := err.(*github.RateLimitError); ok {
log.Printf("GetWorkflowUsageByID ratelimited. Pausing until %s", rl_err.Rate.Reset.Time.String())
logger.Infof("GetWorkflowUsageByID ratelimited. Pausing until %s", rl_err.Rate.Reset.Time.String())
time.Sleep(time.Until(rl_err.Rate.Reset.Time))
continue
} else if err != nil {
log.Printf("GetWorkflowUsageByID error for %s: %s", repo, err.Error())
logger.Infof("GetWorkflowUsageByID error for %s: %s", repo, err.Error())
break
}
workflowBillGauge.WithLabelValues(repo, strconv.FormatInt(*v.ID, 10), *v.NodeID, *v.Name, *v.State, "MACOS").Set(float64(resp.GetBillable().MacOS.GetTotalMS()) / 1000)
Expand Down
133 changes: 66 additions & 67 deletions pkg/metrics/get_runners_enterprise_from_github.go
Original file line number Diff line number Diff line change
@@ -1,67 +1,66 @@
package metrics

import (
"context"
"log"
"strconv"
"time"

"github.com/spendesk/github-actions-exporter/pkg/config"

"github.com/google/go-github/v45/github"
"github.com/prometheus/client_golang/prometheus"
)

var (
runnersEnterpriseGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "github_runner_enterprise_status",
Help: "runner status",
},
[]string{"os", "name", "id"},
)
)

func getAllEnterpriseRunners() []*github.Runner {
var runners []*github.Runner
opt := &github.ListOptions{PerPage: 200}

for {
resp, rr, err := client.Enterprise.ListRunners(context.Background(), config.EnterpriseName, nil)
if rl_err, ok := err.(*github.RateLimitError); ok {
log.Printf("ListRunners ratelimited. Pausing until %s", rl_err.Rate.Reset.Time.String())
time.Sleep(time.Until(rl_err.Rate.Reset.Time))
continue
} else if err != nil {
log.Printf("ListRunners error for enterprise %s: %s", config.EnterpriseName, err.Error())
return nil
}

runners = append(runners, resp.Runners...)
if rr.NextPage == 0 {
break
}
opt.Page = rr.NextPage
}

return runners
}

func getRunnersEnterpriseFromGithub() {
if config.EnterpriseName == "" {
return
}
for {
runners := getAllEnterpriseRunners()

for _, runner := range runners {
var integerStatus float64
if integerStatus = 0; runner.GetStatus() == "online" {
integerStatus = 1
}
runnersEnterpriseGauge.WithLabelValues(*runner.OS, *runner.Name, strconv.FormatInt(runner.GetID(), 10)).Set(integerStatus)
}

time.Sleep(time.Duration(config.Github.Refresh) * time.Second)
}
}
package metrics

import (
"context"
"strconv"
"time"

"github.com/spendesk/github-actions-exporter/pkg/config"

"github.com/google/go-github/v45/github"
"github.com/prometheus/client_golang/prometheus"
)

var (
runnersEnterpriseGauge = prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Name: "github_runner_enterprise_status",
Help: "runner status",
},
[]string{"os", "name", "id"},
)
)

func getAllEnterpriseRunners() []*github.Runner {
var runners []*github.Runner
opt := &github.ListOptions{PerPage: 200}

for {
resp, rr, err := client.Enterprise.ListRunners(context.Background(), config.EnterpriseName, nil)
if rl_err, ok := err.(*github.RateLimitError); ok {
logger.Infof("ListRunners ratelimited. Pausing until %s", rl_err.Rate.Reset.Time.String())
time.Sleep(time.Until(rl_err.Rate.Reset.Time))
continue
} else if err != nil {
logger.Infof("ListRunners error for enterprise %s: %s", config.EnterpriseName, err.Error())
return nil
}

runners = append(runners, resp.Runners...)
if rr.NextPage == 0 {
break
}
opt.Page = rr.NextPage
}

return runners
}

func getRunnersEnterpriseFromGithub() {
if config.EnterpriseName == "" {
return
}
for {
runners := getAllEnterpriseRunners()

for _, runner := range runners {
var integerStatus float64
if integerStatus = 0; runner.GetStatus() == "online" {
integerStatus = 1
}
runnersEnterpriseGauge.WithLabelValues(*runner.OS, *runner.Name, strconv.FormatInt(runner.GetID(), 10)).Set(integerStatus)
}

time.Sleep(time.Duration(config.Github.Refresh) * time.Second)
}
}
5 changes: 2 additions & 3 deletions pkg/metrics/get_runners_from_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package metrics

import (
"context"
"log"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -30,11 +29,11 @@ func getAllRepoRunners(owner string, repo string) []*github.Runner {
for {
resp, rr, err := client.Actions.ListRunners(context.Background(), owner, repo, opt)
if rl_err, ok := err.(*github.RateLimitError); ok {
log.Printf("ListRunners ratelimited. Pausing until %s", rl_err.Rate.Reset.Time.String())
logger.Infof("ListRunners ratelimited. Pausing until %s", rl_err.Rate.Reset.Time.String())
time.Sleep(time.Until(rl_err.Rate.Reset.Time))
continue
} else if err != nil {
log.Printf("ListRunners error for repo %s: %s", repo, err.Error())
logger.Infof("ListRunners error for repo %s: %s", repo, err.Error())
return nil
}

Expand Down
5 changes: 2 additions & 3 deletions pkg/metrics/get_runners_organization_from_github.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package metrics

import (
"context"
"log"
"strconv"
"time"

Expand All @@ -29,11 +28,11 @@ func getAllOrgRunners(orga string) []*github.Runner {
for {
resp, rr, err := client.Actions.ListOrganizationRunners(context.Background(), orga, opt)
if rl_err, ok := err.(*github.RateLimitError); ok {
log.Printf("ListOrganizationRunners ratelimited. Pausing until %s", rl_err.Rate.Reset.Time.String())
logger.Infof("ListOrganizationRunners ratelimited. Pausing until %s", rl_err.Rate.Reset.Time.String())
time.Sleep(time.Until(rl_err.Rate.Reset.Time))
continue
} else if err != nil {
log.Printf("ListOrganizationRunners error for org %s: %s", orga, err.Error())
logger.Infof("ListOrganizationRunners error for org %s: %s", orga, err.Error())
return runners
}

Expand Down
Loading