Skip to content

Commit 458cac3

Browse files
committed
Fixes #48
1 parent 4e65d78 commit 458cac3

File tree

6 files changed

+46
-4
lines changed

6 files changed

+46
-4
lines changed

.github/workflows/ci.yml

+15-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ jobs:
1313
build_docker:
1414
name: Build Docker
1515
runs-on: ubuntu-latest
16+
env:
17+
TEST_TAG: dbhi/prometheus-sql:test
1618
steps:
1719
- name: Checkout
1820
uses: actions/[email protected]
@@ -24,9 +26,21 @@ jobs:
2426
uses: docker/[email protected]
2527
with:
2628
load: true
27-
tags: test
29+
tags: ${{ env.TEST_TAG }}
2830
context: .
2931

32+
- name: Start containers
33+
run: docker-compose -f "docker-compose.yml" -f "../../test-resources/docker-compose.test.yml" up -d
34+
working-directory: ./examples/working_examples
35+
36+
- name: Wait
37+
run: sleep 10 && curl http://localhost:8080/metrics
38+
39+
- name: Stop containers
40+
if: always()
41+
run: docker-compose -f "docker-compose.yml" -f "../../test-resources/docker-compose.test.yml" down
42+
working-directory: ./examples/working_examples
43+
3044
build_pkgs:
3145
name: Build Packages
3246
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

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
+15
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

0 commit comments

Comments
 (0)