Skip to content

Commit

Permalink
fix database usage metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
motoki317 committed Mar 10, 2020
1 parent 5177128 commit 57c7268
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
31 changes: 27 additions & 4 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ type Usage struct {
Data [][]float64 `json:"data"`
}

// オブジェクトストレージへのリクエスト数を取得
func (cc *ConohaClient) ObjectStorageRequests() (map[string]float64, error) {
// メトリクス取得
resp, err := cc.get(cc.accountEndpoint + "/object-storage/rrd/request")
Expand Down Expand Up @@ -185,6 +186,7 @@ type ObjectStorageSizeResponse struct {
Size Usage `json:"size"`
}

// オブジェクトストレージの使用容量を取得
func (cc *ConohaClient) ObjectStorageUsage() (map[string]float64, error) {
// メトリクス取得
resp, err := cc.get(cc.accountEndpoint + "/object-storage/rrd/size")
Expand Down Expand Up @@ -227,8 +229,8 @@ type Database struct {
Type string `json:"type"`
}

// データベース一覧取得
func (cc *ConohaClient) Databases() ([]*Database, error) {
// データベース一覧取得
resp, err := cc.get(cc.databaseHostingEndpoint + "/databases")
if err != nil {
return nil, err
Expand Down Expand Up @@ -259,9 +261,9 @@ type Quota struct {
Quota int `json:"quota"`
}

func (cc *ConohaClient) DatabaseQuota(d *Database) (*Quota, error) {
// データベース使用容量/上限値取得(GB単位)
resp, err := cc.get(cc.databaseHostingEndpoint + "/services/" + d.ServiceID + "/quotas")
// データベース上限値取得(GB単位)
func (cc *ConohaClient) DatabaseQuota(serviceID string) (*Quota, error) {
resp, err := cc.get(cc.databaseHostingEndpoint + "/services/" + serviceID + "/quotas")
if err != nil {
return nil, err
}
Expand All @@ -274,3 +276,24 @@ func (cc *ConohaClient) DatabaseQuota(d *Database) (*Quota, error) {

return &uResp.Quota, nil
}

// JSON 受け取り用
type DatabaseInfoResponse struct {
Database Database `json:"database"`
}

// データベース情報取得(GB単位)
func (cc *ConohaClient) DatabaseInfo(databaseID string) (*Database, error) {
resp, err := cc.get(cc.databaseHostingEndpoint + "/databases/" + databaseID)
if err != nil {
return nil, err
}

// JSONを読む
var uResp DatabaseInfoResponse
if err := json.Unmarshal(resp, &uResp); err != nil {
return nil, err
}

return &uResp.Database, nil
}
19 changes: 16 additions & 3 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewConohaCollector(client *ConohaClient) (*ConohaCollector, error) {
// 4番目のnilには、固定ラベルをprometheus.Labelsで渡せる
prometheus.NewDesc("object_storage_requests", "Requests to Object Storage", []string{"method"}, nil),
prometheus.NewDesc("object_storage_usage", "Usage of Object Storage", []string{}, nil),
prometheus.NewDesc("database_usage", "Usage of Database Server (GB)", []string{"database"}, nil),
prometheus.NewDesc("database_usage", "Usage of Database (GB)", []string{"database"}, nil),
},
[]prometheus.Metric{},
databases,
Expand All @@ -56,13 +56,26 @@ func (cc *ConohaCollector) AutoUpdate() {
usage, err := cc.ObjectStorageUsage()
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[1], prometheus.GaugeValue, usage["value"]))

serviceIDs := make(map[string]bool)

for _, db := range cc.databases {
// データベース使用状況を取得
quota, err := cc.DatabaseQuota(db)
info, err := cc.DatabaseInfo(db.DatabaseID)
if err != nil {
log.Fatal(err)
}
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[2], prometheus.GaugeValue, info.DbSize, db.DbName))

serviceIDs[db.ServiceID] = true
}

for serviceID := range serviceIDs {
// データベース上限値取得
quota, err := cc.DatabaseQuota(serviceID)
if err != nil {
log.Fatal(err)
}
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[2], prometheus.GaugeValue, quota.TotalUsage, db.DbName))
metrics = append(metrics, prometheus.MustNewConstMetric(cc.describes[2], prometheus.GaugeValue, float64(quota.Quota), "Quota "+serviceID))
}

// メトリクスデータ更新
Expand Down

0 comments on commit 57c7268

Please sign in to comment.