-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c18e181
commit bb60352
Showing
5 changed files
with
231 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package exporter | ||
|
||
import ( | ||
"fmt" | ||
"github.com/ktsstudio/selectel-exporter/pkg/selapi" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"time" | ||
) | ||
|
||
func buildGaugeKey(metricName, ip, dbName string) string { | ||
return fmt.Sprintf("%s_%s_%s", metricName, ip, dbName) | ||
} | ||
|
||
type databaseCollector struct { | ||
project selapi.Project | ||
datastore selapi.Datastore | ||
metrics map[string]prometheus.Gauge | ||
} | ||
|
||
func NewDatabaseCollector(project selapi.Project, datastore selapi.Datastore) *databaseCollector { | ||
return &databaseCollector{ | ||
project: project, | ||
datastore: datastore, | ||
metrics: make(map[string]prometheus.Gauge), | ||
} | ||
} | ||
|
||
func (col *databaseCollector) registerGauge(metricName string, metric selapi.DatabaseMetric) { | ||
key := buildGaugeKey(metricName, metric.Ip, metric.DbName) | ||
g, ok := col.metrics[key] | ||
if !ok { | ||
g = prometheus.NewGauge(prometheus.GaugeOpts{ | ||
Name: metricName, | ||
ConstLabels: prometheus.Labels{ | ||
"project": col.project.Name, | ||
"datastore": col.datastore.Name, | ||
"ip": metric.Ip, | ||
"database": metric.DbName, | ||
}, | ||
}) | ||
prometheus.MustRegister(g) | ||
col.metrics[key] = g | ||
} | ||
g.Set(metric.Last) | ||
} | ||
|
||
func (col *databaseCollector) loadMetrics(metricName string, metrics []selapi.DatabaseMetric) { | ||
for _, m := range metrics { | ||
col.registerGauge(metricName, m) | ||
} | ||
} | ||
|
||
func (col *databaseCollector) GetInfo() string { | ||
return fmt.Sprintf( | ||
"project: %s, datastore: %s - collect database metrics", col.project.Name, col.datastore.Name) | ||
} | ||
|
||
func (col *databaseCollector) Collect(e *exporter) error { | ||
start := time.Now().Add(-1 * time.Minute).Unix() | ||
end := time.Now().Unix() | ||
res, err := selapi.FetchDatabaseMetrics(e.openstackAccountToken, e.region, col.datastore.Id, start, end) | ||
if err != nil { | ||
return err | ||
} | ||
col.loadMetrics("selectel_database_locks", res.Metrics.Locks) | ||
col.loadMetrics("selectel_database_deadlocks", res.Metrics.Deadlocks) | ||
col.loadMetrics("selectel_database_cache_hit_ratio", res.Metrics.CacheHitRatio) | ||
col.loadMetrics("selectel_database_tup_updated", res.Metrics.TupUpdated) | ||
col.loadMetrics("selectel_database_tup_returned", res.Metrics.TupReturned) | ||
col.loadMetrics("selectel_database_tup_inserted", res.Metrics.TupInserted) | ||
col.loadMetrics("selectel_database_tup_fetched", res.Metrics.TupFetched) | ||
col.loadMetrics("selectel_database_tup_deleted", res.Metrics.TupDeleted) | ||
col.loadMetrics("selectel_database_xact_rollback", res.Metrics.XActRollback) | ||
col.loadMetrics("selectel_database_xact_commit", res.Metrics.XActCommit) | ||
col.loadMetrics("selectel_database_xact_commit_rollback", res.Metrics.XActCommitRollback) | ||
col.loadMetrics("selectel_database_max_tx_duration", res.Metrics.MaxTxDuration) | ||
col.loadMetrics("selectel_database_connections", res.Metrics.Connections) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package selapi | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/ktsstudio/selectel-exporter/pkg/apperrors" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
type DatabaseMetric struct { | ||
DatastoreMetric | ||
DbName string `json:"db_name"` | ||
} | ||
|
||
type DatabaseMetricsResponses struct { | ||
Metrics struct { | ||
Connections []DatabaseMetric `json:"memory_percent"` | ||
MaxTxDuration []DatabaseMetric `json:"max_tx_duration"` | ||
XActCommitRollback []DatabaseMetric `json:"xact_commit_rollback"` | ||
XActCommit []DatabaseMetric `json:"xact_commit"` | ||
XActRollback []DatabaseMetric `json:"xact_rollback"` | ||
TupDeleted []DatabaseMetric `json:"tup_deleted"` | ||
TupFetched []DatabaseMetric `json:"tup_fetched"` | ||
TupInserted []DatabaseMetric `json:"tup_inserted"` | ||
TupReturned []DatabaseMetric `json:"tup_returned"` | ||
TupUpdated []DatabaseMetric `json:"tup_updated"` | ||
CacheHitRatio []DatabaseMetric `json:"cache_hit_ratio"` | ||
Deadlocks []DatabaseMetric `json:"deadlocks"` | ||
Locks []DatabaseMetric `json:"locks"` | ||
} `json:"metrics"` | ||
} | ||
|
||
func FetchDatabaseMetrics(token, region, datastoreId string, start, end int64) (*DatabaseMetricsResponses, error) { | ||
client := &http.Client{} | ||
req, err := http.NewRequest( | ||
"GET", | ||
fmt.Sprintf("https://%s.dbaas.selcloud.ru/v1/datastores/%s/database-metrics", region, datastoreId), | ||
nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
req.Header.Add("X-Auth-Token", token) | ||
q := req.URL.Query() | ||
q.Add("start", strconv.FormatInt(start, 10)) | ||
q.Add("end", strconv.FormatInt(end, 10)) | ||
req.URL.RawQuery = q.Encode() | ||
|
||
data, err := fetch(client, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
resp := &DatabaseMetricsResponses{} | ||
if err := json.Unmarshal(data, resp); err != nil { | ||
return nil, apperrors.NewResponseFormatError("DatabaseMetricsResponses") | ||
} | ||
return resp, nil | ||
} |