Skip to content

Commit

Permalink
Merge pull request #2 from RabotaRu/develop
Browse files Browse the repository at this point in the history
renamed module accoring to migration on github, goreleaser setup
  • Loading branch information
Oleg Maryin authored Mar 29, 2022
2 parents b8e21af + fc69e46 commit e652c01
Show file tree
Hide file tree
Showing 12 changed files with 804 additions and 32 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
.idea/
*.geany
*~
.project*

.go-build/
dist/
main
sbercdn-exporter

Expand Down
5 changes: 1 addition & 4 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,10 @@ changelog:
- typo

dockers:
- dockerfile: Dockerfile
- dockerfile: GoReleaserDockerfile
image_templates:
- "ghcr.io/rabotaru/sbercdn-exporter:{{ .Version }}"
- "ghcr.io/rabotaru/sbercdn-exporter:latest"

- "rabotaru/sbercdn-exporter:{{ .Version }}"
- "rabotaru/sbercdn-exporter:latest"
build_flag_templates:
- "--pull"
- "--label=org.opencontainers.image.created={{.Date}}"
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ARG GOLANG_VERSION=1.17
ARG VERSION=0.3.0
ARG VERSION=0.3.0rc1

FROM golang:${GOLANG_VERSION} AS builder
ARG VERSION
Expand Down
8 changes: 8 additions & 0 deletions GoReleaserDockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
ARG GOLANG_VERSION=1.17
ARG VERSION=0.3.0rc1

FROM alpine
LABEL maintainer="o.marin@rabota.ru"
COPY sbercdn-exporter /bin/
EXPOSE 9921/tcp
ENTRYPOINT ["/bin/sbercdn-exporter"]
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ At this moment service may be configured from config and/or from env.
There is only one cli flag now `-config` to get settings from YAML formatted config file for eg.:
```
listen:
address: ":9921" # mandatory, address to listen on
address: ":9921" # optional, address to listen on defaults to :9921
cert_file: # optional, path certificate file for endpoint encryption
privkey_file: # optional, path to unencrypted private key file for endpoint encryption
# if both cert_file and privkey_file are not empty exporter will serve metrics through HTTPS
api:
url: "https://api.cdn.sber.cloud" # optional, API URL, defaults to https://api.cdn.sber.cloud
Expand All @@ -29,6 +30,12 @@ yaml params full path in upper case with "SCE" prefix joined with "_" for eg. op
```
SCE_LISTEN_ADDRESS=":9921"
SCE_API_URL="https://api.cdn.sber.cloud"
SCE_API_AUTH_USERNAME="username@example.com"
SCE_API_AUTH_PASSWORD="password"
SCE_API_USERNAME="username@example.com"
SCE_API_PASSWORD="password"
```

## Running in docker container

```
docker run --rm -e SCE_API_USERNAME="username@example.com" -e SCE_API_PASSWORD="password" -p 9921:9921/tcp ghcr.io/rabotaru/sbercdn-exporter:latest
```
2 changes: 1 addition & 1 deletion apiclient/certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"log"
"net/url"

cmn "git.rabota.space/infrastructure/sbercdn-exporter/common"
cmn "github.com/RabotaRu/sbercdn-exporter/common"
)

type CertItem struct {
Expand Down
30 changes: 19 additions & 11 deletions apiclient/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"strings"
"time"

cmn "git.rabota.space/infrastructure/sbercdn-exporter/common"
cmn "github.com/RabotaRu/sbercdn-exporter/common"
)

const (
Expand Down Expand Up @@ -83,18 +83,21 @@ func (c *SberCdnApiClient) FindActiveAccounts(ctx context.Context) (accounts []s
active_accs = append(active_accs, accs[i]["name"])
}
}
c.active_accs = active_accs
t := time.Now()
c.accs_update_time = &t
if fmt.Sprintf("%v", c.active_accs) != fmt.Sprintf("%v", active_accs) {
log.Println("active accounts now:", active_accs)
c.active_accs = active_accs
t := time.Now()
c.accs_update_time = &t
}
}
return c.active_accs
}

func (c *SberCdnApiClient) auth(ctx context.Context) (auth_token string, err error) {
defer func() {
if r := recover(); r != nil {
log.Println("failed to update auth token:", r)
auth_token = ""
log.Println("failed to update auth token:", r)
}
}()
if c.auth_token != "" && time.Since(c.auth_token_time) < (c.TokenLifetime-c.MaxQueryTime) {
Expand All @@ -112,20 +115,25 @@ func (c *SberCdnApiClient) auth(ctx context.Context) (auth_token string, err err
),
)
if err != nil {
log.Panicln("Error creating new auth request")
err = fmt.Errorf("Error creating new auth request")
log.Panicln(err)
}
resp, err := c.hc.Do(req)
if err != nil {
log.Panicln("Error sending auth request")
err = fmt.Errorf("Error sending auth request")
log.Panicln(err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
log.Panicln(fmt.Errorf("auth response status code %v", resp.Status))
}
body, err := io.ReadAll(resp.Body)
if err != nil {
log.Panicln("Error reading auth response body")
}
if resp.StatusCode != 200 {
reqbody, _ := io.ReadAll(req.Body)
err = fmt.Errorf("auth response status code %v", resp.Status)
log.Println(string(reqbody))
log.Panicln(err)
}

var data map[string]interface{}
err = json.Unmarshal(body, &data)
Expand All @@ -136,7 +144,7 @@ func (c *SberCdnApiClient) auth(ctx context.Context) (auth_token string, err err
if auth_token, ok := data["token"].(string); ok {
c.auth_token = auth_token
c.auth_token_time = time.Now()
log.Println("Authorized successfully!")
log.Println("Authorized successfully on", c.Url)
} else {
err := fmt.Errorf("token is not a string")
log.Panicln("%w", err)
Expand Down
14 changes: 8 additions & 6 deletions collector/cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"log"
"sync"

"git.rabota.space/infrastructure/sbercdn-exporter/apiclient"
cmn "git.rabota.space/infrastructure/sbercdn-exporter/common"
"github.com/RabotaRu/sbercdn-exporter/apiclient"
cmn "github.com/RabotaRu/sbercdn-exporter/common"
"github.com/prometheus/client_golang/prometheus"
)

Expand All @@ -17,18 +17,19 @@ type SberCdnCertCollector struct {
}

func NewSberCdnCertCollector(client *apiclient.SberCdnApiClient) *SberCdnCertCollector {
labels := []string{"account", "comment", "cn"}
return &SberCdnCertCollector{
client,
map[string]*prometheus.Desc{
"cert_start": prometheus.NewDesc(
"sbercdn_certificate_valid_since",
"UNIX time in seconds since EPOCH since which certificate is valid",
[]string{"account", "cn"},
labels,
nil),
"cert_end": prometheus.NewDesc(
"sbercdn_certificate_valid_till",
"UNIX time in seconds since EPOCH till which certificate is valid",
[]string{"account", "cn"},
labels,
nil),
},
"certList",
Expand Down Expand Up @@ -66,11 +67,12 @@ func (c *SberCdnCertCollector) Collect(mch chan<- prometheus.Metric) {
for i := 0; i < len(certs); i++ {
ci := &certs[i]
mch <- prometheus.MustNewConstMetric(c.descriptions["cert_start"], prometheus.CounterValue,
ci.Start, account, ci.Cn)
ci.Start, account, ci.Comment, ci.Cn)
mch <- prometheus.MustNewConstMetric(c.descriptions["cert_end"], prometheus.CounterValue,
ci.End, account, ci.Cn)
ci.End, account, ci.Comment, ci.Cn)
}
}

for _, acc := range c.client.FindActiveAccounts(ctx_root) {
wag.Add(1)
getCertMetrics(context.WithValue(ctx_root, cmn.CtxKey("account"), acc))
Expand Down
4 changes: 2 additions & 2 deletions collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"sync"
"time"

"git.rabota.space/infrastructure/sbercdn-exporter/apiclient"
cmn "git.rabota.space/infrastructure/sbercdn-exporter/common"
"github.com/RabotaRu/sbercdn-exporter/apiclient"
cmn "github.com/RabotaRu/sbercdn-exporter/common"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module git.rabota.space/infrastructure/sbercdn-exporter
module github.com/RabotaRu/sbercdn-exporter

go 1.16

Expand Down
10 changes: 7 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import (
"syscall"
"time"

"git.rabota.space/infrastructure/sbercdn-exporter/apiclient"
col "git.rabota.space/infrastructure/sbercdn-exporter/collector"
cmn "git.rabota.space/infrastructure/sbercdn-exporter/common"
"github.com/RabotaRu/sbercdn-exporter/apiclient"
col "github.com/RabotaRu/sbercdn-exporter/collector"
cmn "github.com/RabotaRu/sbercdn-exporter/common"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
Expand Down Expand Up @@ -53,6 +53,10 @@ func main() {
config.Listen.Address = "0.0.0.0" + config.Listen.Address
}

if config.Client.Username == "" || config.Client.Username == "" {
log.Fatalln("API username or password is missing or empty")
}

apiClient, err := apiclient.NewSberCdnApiClient(&config.Client)
if err != nil {
log.Fatalf("failed to start api client: %v", err)
Expand Down
Loading

0 comments on commit e652c01

Please sign in to comment.