Skip to content

Commit e3db0f7

Browse files
committedJan 1, 2022
Fixes #48
1 parent 4e65d78 commit e3db0f7

File tree

6 files changed

+44
-4
lines changed

6 files changed

+44
-4
lines changed
 

‎.github/workflows/ci.yml

+13-1
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,21 @@ jobs:
2424
uses: docker/build-push-action@v2.7.0
2525
with:
2626
load: true
27-
tags: test
27+
tags: dbhi/prometheus-sql:test
2828
context: .
2929

30+
- name: Start containers
31+
run: docker-compose -f "docker-compose.yml" -f "docker-compose.test.yml" up -d
32+
working-directory: ./examples/working_example
33+
34+
- name: Wait and Test Metrics Endpoint
35+
run: sleep 30 && curl http://localhost:8080/metrics
36+
37+
- name: Stop containers
38+
if: always()
39+
run: docker-compose -f "docker-compose.yml" -f "docker-compose.test.yml" down
40+
working-directory: ./examples/working_example
41+
3042
build_pkgs:
3143
name: Build Packages
3244
needs: build_docker

‎config.go

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ type DataSource struct {
4949
// Query defines a SQL statement and parameters as well as configuration for the monitoring behavior
5050
type Query struct {
5151
Name string
52+
Help string
5253
DataSourceRef string `yaml:"data-source"`
5354
Driver string
5455
Connection map[string]interface{}

‎examples/example-queries.yml

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
# Name of the metric. Will be exposed as query_result_num_products
99
- num_products:
10+
# Help text seen for metrics exposed
11+
help: Products belonging to category ID 5
1012
# Name of the driver to use.
1113
driver: postgresql
1214

@@ -45,6 +47,8 @@
4547

4648
# For faceted metrics provide the name of the metric-column in config, and return a resultset of multiple columns and rows
4749
- sales_by_country:
50+
# Help text seen for metrics exposed
51+
help: Number of sales by country
4852
# Name of the driver to use.
4953
driver: postgresql
5054

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
version: "2.4"
2+
services:
3+
prometheus-sql:
4+
image: dbhi/prometheus-sql:test
5+
links:
6+
- sqlagent:sqlagent
7+
depends_on:
8+
- sqlagent
9+
ports:
10+
- 8080:8080
11+
command:
12+
- -config
13+
- /config.yml
14+
- -service
15+
- http://sqlagent:5000

‎examples/working_example/queries.yml

+4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
# Use default data source
22
- nr_companies_per_country:
3+
help: Number of companies per country
34
sql: >
45
select country, count(1) as cnt from Companies group by country
56
data-field: cnt
67

78
# Missing database user, data source 'my-ds-missing-user' shall be used instead of the default 'my-ds'
89
- error_missing_user:
10+
help: Data source error
911
data-source: my-ds-missing-user
1012
sql: >
1113
select * from dual
1214
interval: 30s
1315

1416
# Missing table
1517
- error_missing_table:
18+
help: Missing table
1619
sql: >
1720
select * from missing_table
1821
interval: 30s
@@ -22,6 +25,7 @@
2225
# - response_time_count with cnt as value
2326
# - response_time_sum with rt as value
2427
- response_time:
28+
help: Sub metrics
2529
sql: >
2630
select count(*) as cnt, sum(response_time) as rt from Requests
2731
sub-metrics:

‎set.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (r *QueryResult) generateMetricUniqueKey(facets map[string]interface{}, suf
5050
return fmt.Sprintf("%s%s", r.generateMetricName(suffix), string(jsonData))
5151
}
5252

53-
func (r *QueryResult) createMetric(facets map[string]interface{}, suffix string) (string, metricStatus) {
53+
func (r *QueryResult) createMetric(facets map[string]interface{}, suffix string, help string) (string, metricStatus) {
5454
metricName := r.generateMetricName(suffix)
5555
resultKey := r.generateMetricUniqueKey(facets, suffix)
5656

@@ -64,10 +64,14 @@ func (r *QueryResult) createMetric(facets map[string]interface{}, suffix string)
6464
return resultKey, registered
6565
}
6666

67+
if len(help) == 0 {
68+
help = "Result of an SQL query"
69+
}
70+
6771
fmt.Println("Creating", resultKey)
6872
r.Result[resultKey] = prometheus.NewGauge(prometheus.GaugeOpts{
6973
Name: fmt.Sprintf("query_result_%s", metricName),
70-
Help: "Result of an SQL query",
74+
Help: help,
7175
ConstLabels: labels,
7276
})
7377
return resultKey, unregistered
@@ -164,7 +168,7 @@ func (r *QueryResult) SetMetrics(recs records, valueOnError string) error {
164168
return errors.New("Data field not found in result set")
165169
}
166170

167-
key, status := r.createMetric(facet, suffix)
171+
key, status := r.createMetric(facet, suffix, r.Query.Help)
168172
err := setValueForResult(r.Result[key], dataVal)
169173
if err != nil {
170174
return err

0 commit comments

Comments
 (0)