Skip to content

Commit

Permalink
Fixes #48
Browse files Browse the repository at this point in the history
  • Loading branch information
haxorof committed Dec 28, 2021
1 parent 4e65d78 commit 458cac3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 4 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ jobs:
build_docker:
name: Build Docker
runs-on: ubuntu-latest
env:
TEST_TAG: dbhi/prometheus-sql:test
steps:
- name: Checkout
uses: actions/checkout@v2.4.0
Expand All @@ -24,9 +26,21 @@ jobs:
uses: docker/build-push-action@v2.7.0
with:
load: true
tags: test
tags: ${{ env.TEST_TAG }}
context: .

- name: Start containers
run: docker-compose -f "docker-compose.yml" -f "../../test-resources/docker-compose.test.yml" up -d
working-directory: ./examples/working_examples

- name: Wait
run: sleep 10 && curl http://localhost:8080/metrics

- name: Stop containers
if: always()
run: docker-compose -f "docker-compose.yml" -f "../../test-resources/docker-compose.test.yml" down
working-directory: ./examples/working_examples

build_pkgs:
name: Build Packages
needs: build_docker
Expand Down
1 change: 1 addition & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type DataSource struct {
// Query defines a SQL statement and parameters as well as configuration for the monitoring behavior
type Query struct {
Name string
Help string
DataSourceRef string `yaml:"data-source"`
Driver string
Connection map[string]interface{}
Expand Down
4 changes: 4 additions & 0 deletions examples/example-queries.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

# Name of the metric. Will be exposed as query_result_num_products
- num_products:
# Help text seen for metrics exposed
help: Products belonging to category ID 5
# Name of the driver to use.
driver: postgresql

Expand Down Expand Up @@ -45,6 +47,8 @@

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

Expand Down
4 changes: 4 additions & 0 deletions examples/working_example/queries.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
# Use default data source
- nr_companies_per_country:
help: Number of companies per country
sql: >
select country, count(1) as cnt from Companies group by country
data-field: cnt

# Missing database user, data source 'my-ds-missing-user' shall be used instead of the default 'my-ds'
- error_missing_user:
help: Data source error
data-source: my-ds-missing-user
sql: >
select * from dual
interval: 30s

# Missing table
- error_missing_table:
help: Missing table
sql: >
select * from missing_table
interval: 30s
Expand All @@ -22,6 +25,7 @@
# - response_time_count with cnt as value
# - response_time_sum with rt as value
- response_time:
help: Sub metrics
sql: >
select count(*) as cnt, sum(response_time) as rt from Requests
sub-metrics:
Expand Down
10 changes: 7 additions & 3 deletions set.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (r *QueryResult) generateMetricUniqueKey(facets map[string]interface{}, suf
return fmt.Sprintf("%s%s", r.generateMetricName(suffix), string(jsonData))
}

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

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

if len(help) == 0 {
help = "Result of an SQL query"
}

fmt.Println("Creating", resultKey)
r.Result[resultKey] = prometheus.NewGauge(prometheus.GaugeOpts{
Name: fmt.Sprintf("query_result_%s", metricName),
Help: "Result of an SQL query",
Help: help,
ConstLabels: labels,
})
return resultKey, unregistered
Expand Down Expand Up @@ -164,7 +168,7 @@ func (r *QueryResult) SetMetrics(recs records, valueOnError string) error {
return errors.New("Data field not found in result set")
}

key, status := r.createMetric(facet, suffix)
key, status := r.createMetric(facet, suffix, r.Query.Help)
err := setValueForResult(r.Result[key], dataVal)
if err != nil {
return err
Expand Down
15 changes: 15 additions & 0 deletions test-resources/docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: "2.4"
services:
prometheus-sql:
image: dbhi/prometheus-sql:test
links:
- sqlagent:sqlagent
depends_on:
- sqlagent
ports:
- 8080:8080
command:
- -config
- /config.yml
- -service
- http://sqlagent:5000

0 comments on commit 458cac3

Please sign in to comment.