|
| 1 | +// Copyright 2020 The Prometheus Authors |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +// Scrape `performance_schema.status_by_host` column information. |
| 15 | + |
| 16 | +package collector |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + "database/sql" |
| 21 | + |
| 22 | + "github.com/go-kit/kit/log" |
| 23 | + "github.com/prometheus/client_golang/prometheus" |
| 24 | +) |
| 25 | + |
| 26 | +const perfStatusByHostQuery = ` |
| 27 | + SELECT HOST, VARIABLE_NAME, VARIABLE_VALUE |
| 28 | + FROM performance_schema.status_by_host |
| 29 | + WHERE HOST IS NOT NULL; |
| 30 | + ` |
| 31 | + |
| 32 | +// Metric descriptors. |
| 33 | +var ( |
| 34 | + performanceSchemaStatusByHostDesc = prometheus.NewDesc( |
| 35 | + prometheus.BuildFQName(namespace, performanceSchema, "status_by_host"), |
| 36 | + "The status variable value for a given host.", |
| 37 | + []string{"host", "variable_name"}, nil, |
| 38 | + ) |
| 39 | +) |
| 40 | + |
| 41 | +// ScrapePerfStatusByHost collects status_by_host table information. |
| 42 | +type ScrapePerfStatusByHost struct{} |
| 43 | + |
| 44 | +// Name of the Scraper. Should be unique. |
| 45 | +func (ScrapePerfStatusByHost) Name() string { |
| 46 | + return "perf_schema.status_by_host" |
| 47 | +} |
| 48 | + |
| 49 | +// Help describes the role of the Scraper. |
| 50 | +func (ScrapePerfStatusByHost) Help() string { |
| 51 | + return "Collect status_by_host gauges in performance_schema" |
| 52 | +} |
| 53 | + |
| 54 | +// Version of MySQL from which scraper is available. |
| 55 | +func (ScrapePerfStatusByHost) Version() float64 { |
| 56 | + return 5.7 |
| 57 | +} |
| 58 | + |
| 59 | +// Scrape collects data from database connection and sends it over channel as prometheus metric. |
| 60 | +func (ScrapePerfStatusByHost) Scrape(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric, logger log.Logger) error { |
| 61 | + statusByHostRows, err := db.QueryContext(ctx, perfStatusByHostQuery) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + defer statusByHostRows.Close() |
| 66 | + |
| 67 | + var ( |
| 68 | + host, variable_name string |
| 69 | + value float64 |
| 70 | + ) |
| 71 | + |
| 72 | + for statusByHostRows.Next() { |
| 73 | + if err := statusByHostRows.Scan( |
| 74 | + &host, &variable_name, &value, |
| 75 | + ); err != nil { |
| 76 | + return err |
| 77 | + } |
| 78 | + ch <- prometheus.MustNewConstMetric( |
| 79 | + performanceSchemaStatusByHostDesc, prometheus.GaugeValue, value, |
| 80 | + host, variable_name, |
| 81 | + ) |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// check interface |
| 87 | +var _ Scraper = ScrapePerfStatusByHost{} |
0 commit comments