Skip to content

Commit

Permalink
ウェブアクセラレータ対応 (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
yamamoto-febc authored Aug 25, 2022
1 parent 2b7064b commit 303276f
Show file tree
Hide file tree
Showing 9 changed files with 351 additions and 7 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ $ docker run -p 9542:9542 -e SAKURACLOUD_ACCESS_TOKEN=<YOUR-TOKEN> -e SAKURACLOU
### Flags

| Flag / Environment Variable | Required | Default | Description |
| -------------------------------------------- | -------- | ---------- | ------------------------- |
|------------------------------------------------| -------- | ---------- | ------------------------- |
| `--token` / `SAKURACLOUD_ACCESS_TOKEN` || | API Key(Token) |
| `--secret` / `SAKURACLOUD_ACCESS_TOKEN_SECRET` || | API Key(Secret) |
| `--ratelimit`/ `SAKURACLOUD_RATE_LIMIT` | | `5` | API request rate limit(maximum:10) |
Expand All @@ -64,6 +64,7 @@ $ docker run -p 9542:9542 -e SAKURACLOUD_ACCESS_TOKEN=<YOUR-TOKEN> -e SAKURACLOU
| `--no-collector.sim` | | `false` | Disable the SIM collector |
| `--no-collector.vpc-router` | | `false` | Disable the VPCRouter collector |
| `--no-collector.zone` | | `false` | Disable the Zone collector |
| `--no-collector.webaccel` | | `false` | Disable the WebAccel collector |


#### Flags for debug
Expand All @@ -81,7 +82,7 @@ Example fake store file(JSON) is here[examples/fake/generate-fake-store-json/exa
The exporter returns the following metrics:

| Resource Type | Metric Name Prefix |
| ------ | ----------- |
|---------------------------------|------------------------------|
| [AutoBackup](#autobackup) | sakuracloud_auto_backup_* |
| [Coupon](#coupon) | sakuracloud_coupon_* |
| [Database](#database) | sakuracloud_database_* |
Expand All @@ -96,6 +97,7 @@ The exporter returns the following metrics:
| [SIM](#sim) | sakuracloud_sim_* |
| [VPCRouter](#vpcrouter) | sakuracloud_vpc_router_* |
| [Zone](#zone) | sakuracloud_zone_* |
| [WebAccel](#webaccel) | webaccel_* |
| [Exporter](#exporter) | sakuracloud_exporter_* |


Expand Down Expand Up @@ -272,6 +274,20 @@ The exporter returns the following metrics:
| ------ | ----------- | ------ |
| sakuracloud_zone_info | A metric with a constant '1' value labeled by zone information | `id`, `name`, `description`, `region_id`, `region_name` |

#### WebAccel

| Metric | Description | Labels |
|--------------------------------|-----------------------------------------------------------|----------------------------------------------------|
| webaccel_site_info | A metric with a constant '1' value | `id`, `name`, `domain_type`, `domain`, `subdomain` |
| webaccel_access_count | Access count | `id` |
| webaccel_bytes_sent | Bytes sent | `id` |
| webaccel_cache_miss_bytes_sent | Cache miss bytes sent | `id` |
| webaccel_cache_hit_ratio | Cache hit ratio | `id` |
| webaccel_bytes_cache_hit_ratio | Bytes cache hit ratio | `id` |
| webaccel_price | Price | `id` |
| webaccel_cert_expire | Certificate expiration date in seconds since epoch (1970) | `id` |


#### Exporter

| Metric | Description | Labels |
Expand Down
198 changes: 198 additions & 0 deletions collector/webaccel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
// Copyright 2019-2022 The sakuracloud_exporter Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
"context"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/prometheus/client_golang/prometheus"
"github.com/sacloud/sakuracloud_exporter/platform"
)

// WebAccelCollector collects metrics about the webaccel's sites.
type WebAccelCollector struct {
ctx context.Context
logger log.Logger
errors *prometheus.CounterVec
client platform.WebAccelClient

SiteInfo *prometheus.Desc
AccessCount *prometheus.Desc
BytesSent *prometheus.Desc
CacheMissBytesSent *prometheus.Desc
CacheHitRatio *prometheus.Desc
BytesCacheHitRatio *prometheus.Desc
Price *prometheus.Desc

CertificateExpireDate *prometheus.Desc
}

// NewWebAccelCollector returns a new WebAccelCollector.
func NewWebAccelCollector(ctx context.Context, logger log.Logger, errors *prometheus.CounterVec, client platform.WebAccelClient) *WebAccelCollector {
errors.WithLabelValues("webaccel").Add(0)

labels := []string{"id"}

return &WebAccelCollector{
ctx: ctx,
logger: logger,
errors: errors,
client: client,
SiteInfo: prometheus.NewDesc(
"webaccel_site_info",
"A metric with a constant '1' value labeled by id, name, domain_type, domain, subdomain",
[]string{"id", "name", "domain_type", "domain", "subdomain"}, nil,
),
AccessCount: prometheus.NewDesc(
"webaccel_access_count",
"",
labels, nil,
),
BytesSent: prometheus.NewDesc(
"webaccel_bytes_sent",
"",
labels, nil,
),
CacheMissBytesSent: prometheus.NewDesc(
"webaccel_cache_miss_bytes_sent",
"",
labels, nil,
),
CacheHitRatio: prometheus.NewDesc(
"webaccel_cache_hit_ratio",
"",
labels, nil,
),
BytesCacheHitRatio: prometheus.NewDesc(
"webaccel_bytes_cache_hit_ratio",
"",
labels, nil,
),
Price: prometheus.NewDesc(
"webaccel_price",
"",
labels, nil,
),
CertificateExpireDate: prometheus.NewDesc(
"webaccel_cert_expire",
"Certificate expiration date in seconds since epoch (1970)",
labels, nil,
),
}
}

// Describe sends the super-set of all possible descriptors of metrics
// collected by this Collector.
func (c *WebAccelCollector) Describe(ch chan<- *prometheus.Desc) {
ch <- c.SiteInfo
ch <- c.AccessCount
ch <- c.BytesSent
ch <- c.CacheMissBytesSent
ch <- c.CacheHitRatio
ch <- c.BytesCacheHitRatio
ch <- c.Price
ch <- c.CertificateExpireDate
}

// Collect is called by the Prometheus registry when collecting metrics.
func (c *WebAccelCollector) Collect(ch chan<- prometheus.Metric) {
sites, err := c.client.Find(c.ctx)
if err != nil {
c.errors.WithLabelValues("webaccel").Add(1)
level.Warn(c.logger).Log( // nolint
"msg", "can't get webAccel info",
"err", err,
)
return
}

for _, site := range sites {
labels := []string{
site.ID,
site.Name,
site.DomainType,
site.Domain,
site.Subdomain,
}

ch <- prometheus.MustNewConstMetric(
c.SiteInfo,
prometheus.GaugeValue,
1.0,
labels...,
)

if site.HasCertificate {
ch <- prometheus.MustNewConstMetric(
c.CertificateExpireDate,
prometheus.GaugeValue,
float64(site.CertValidNotAfter),
[]string{site.ID}...,
)
}
}

usage, err := c.client.Usage(c.ctx)
if err != nil {
c.errors.WithLabelValues("webaccel").Add(1)
level.Warn(c.logger).Log( // nolint
"msg", "can't get webAccel monthly usage",
"err", err,
)
return
}
for _, u := range usage.MonthlyUsages {
labels := []string{u.SiteID.String()}

ch <- prometheus.MustNewConstMetric(
c.AccessCount,
prometheus.GaugeValue,
float64(u.AccessCount),
labels...,
)
ch <- prometheus.MustNewConstMetric(
c.BytesSent,
prometheus.GaugeValue,
float64(u.BytesSent),
labels...,
)
ch <- prometheus.MustNewConstMetric(
c.CacheMissBytesSent,
prometheus.GaugeValue,
float64(u.CacheMissBytesSent),
labels...,
)
ch <- prometheus.MustNewConstMetric(
c.CacheHitRatio,
prometheus.GaugeValue,
u.CacheHitRatio,
labels...,
)
ch <- prometheus.MustNewConstMetric(
c.BytesCacheHitRatio,
prometheus.GaugeValue,
u.BytesCacheHitRatio,
labels...,
)
ch <- prometheus.MustNewConstMetric(
c.Price,
prometheus.GaugeValue,
float64(u.Price),
labels...,
)
}
}
45 changes: 45 additions & 0 deletions collector/webaccel_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2019-2022 The sacloud/sakuracloud_exporter Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
"context"
"testing"

"github.com/sacloud/webaccel-api-go"
"github.com/stretchr/testify/require"
)

type dummyWebAccelClient struct {
sites []*webaccel.Site
usage *webaccel.MonthlyUsageResults
err error
}

func (d *dummyWebAccelClient) Find(ctx context.Context) ([]*webaccel.Site, error) {
return d.sites, d.err
}

func (d *dummyWebAccelClient) Usage(ctx context.Context) (*webaccel.MonthlyUsageResults, error) {
return d.usage, d.err
}

func TestWebAccelCollector_Describe(t *testing.T) {
initLoggerAndErrors()
c := NewWebAccelCollector(context.Background(), testLogger, testErrors, &dummyWebAccelClient{})

descs := collectDescs(c)
require.Len(t, descs, 8)
}
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ type Config struct {
NoCollectorSIM bool `arg:"--no-collector.sim" help:"Disable the SIM collector"`
NoCollectorVPCRouter bool `arg:"--no-collector.vpc-router" help:"Disable the VPCRouter collector"`
NoCollectorZone bool `arg:"--no-collector.zone" help:"Disable the Zone collector"`
NoCollectorWebAccel bool `arg:"--no-collector.webaccel" help:"Disable the WebAccel collector"`
}

func InitConfig() (Config, error) {
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ require (
github.com/prometheus/client_golang v1.13.0
github.com/prometheus/client_model v0.2.0
github.com/sacloud/api-client-go v0.2.1
github.com/sacloud/iaas-api-go v1.3.1
github.com/sacloud/iaas-api-go v1.3.2
github.com/sacloud/iaas-service-go v1.3.1
github.com/sacloud/packages-go v0.0.5
github.com/sacloud/webaccel-api-go v1.1.3
github.com/stretchr/testify v1.8.0
)

Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,14 @@ github.com/sacloud/api-client-go v0.2.1 h1:jl02ZG6cM+mcH4eDYg0cxCFFuTOVTOjUCLYL4
github.com/sacloud/api-client-go v0.2.1/go.mod h1:8fmYy5OpT3W8ltV5ZxF8evultNwKpduGN4YKmU9Af7w=
github.com/sacloud/go-http v0.1.2 h1:a84HkeDHxDD1vIA6HiOT72a3fwwJueZBwuGP6zVtEJU=
github.com/sacloud/go-http v0.1.2/go.mod h1:gvWaT8LFBFnSBFVrznOQXC62uad46bHZQM8w+xoH3eE=
github.com/sacloud/iaas-api-go v1.3.1 h1:UmP9r4NNd1YucmeTzzJtsFsT9d/RYvJn2tP+HmC4yMk=
github.com/sacloud/iaas-api-go v1.3.1/go.mod h1:CoqpRYBG2NRB5xfqTfZNyh2lVLKyLkE/HV9ISqmbhGc=
github.com/sacloud/iaas-api-go v1.3.2 h1:03obrdVdv/bGHK9p6CV7Uzg+ot2gLsddUMevm9DDZqQ=
github.com/sacloud/iaas-api-go v1.3.2/go.mod h1:CoqpRYBG2NRB5xfqTfZNyh2lVLKyLkE/HV9ISqmbhGc=
github.com/sacloud/iaas-service-go v1.3.1 h1:ltMadQPUGzpSHD3k6XrqWy1XykIgZFseu/AA67qHjAI=
github.com/sacloud/iaas-service-go v1.3.1/go.mod h1:2mkOWoEk8gdHuxShI6RLssw1g4Sd6vc92ErcNaL2vPk=
github.com/sacloud/packages-go v0.0.5 h1:NXTQNyyp/3ugM4CANtLBJLejFESzfWu4GPUURN4NJrA=
github.com/sacloud/packages-go v0.0.5/go.mod h1:XWMBSNHT9YKY3lCh6yJsx1o1RRQQGpuhNqJA6bSHdD4=
github.com/sacloud/webaccel-api-go v1.1.3 h1:JKsjVL8U9QbzsEjMxqUzr+H7ni3vKVPhpdJeLkUy0ZQ=
github.com/sacloud/webaccel-api-go v1.1.3/go.mod h1:GAmsQquhje9diSc/gAfVGYbavlD4S3KJCljOAxzAajU=
github.com/sebdah/goldie v1.0.0/go.mod h1:jXP4hmWywNEwZzhMuv2ccnqTSFpuq8iyQhtQdkkZBH4=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/sergi/go-diff v1.2.0 h1:XU+rvMAioB0UC3q1MFrIQy4Vo5/4VsRDQQXHsEya6xQ=
Expand Down
12 changes: 10 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,14 @@ func main() {
)

client := platform.NewSakuraCloudClient(c, Version)
if !client.HasValidAPIKeys(context.TODO()) {
ctx := context.Background()

if !client.HasValidAPIKeys(ctx) {
panic(errors.New("unauthorized: invalid API key is applied"))
}
if !c.NoCollectorWebAccel && !client.HasWebAccelPermission(ctx) {
logger.Log("warn", "API key doesn't have webaccel permission") // nolint
}

errs := prometheus.NewCounterVec(prometheus.CounterOpts{
Name: "sakuracloud_exporter_errors_total",
Expand All @@ -85,7 +90,7 @@ func main() {
PidFn: func() (int, error) { return os.Getpid(), nil },
}))

ctx, cancel := context.WithCancel(context.Background())
ctx, cancel := context.WithCancel(ctx)

// collector info
r.MustRegister(prometheus.NewGoCollector())
Expand Down Expand Up @@ -135,6 +140,9 @@ func main() {
if !c.NoCollectorZone {
r.MustRegister(collector.NewZoneCollector(ctx, logger, errs, client.Zone))
}
if !c.NoCollectorWebAccel {
r.MustRegister(collector.NewWebAccelCollector(ctx, logger, errs, client.WebAccel))
}

http.Handle(c.WebPath,
promhttp.HandlerFor(r, promhttp.HandlerOpts{}),
Expand Down
Loading

0 comments on commit 303276f

Please sign in to comment.