Skip to content

Commit

Permalink
Support now multiple Columns/Rows in Self defined queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Neumann committed Dec 3, 2019
1 parent 4d84f80 commit 11cabf7
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
28 changes: 21 additions & 7 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net"
"net/http"
"time"
"strconv"
_ "github.com/mattn/go-oci8"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -54,7 +55,7 @@ type Exporter struct {

var (
// Version will be set at build time.
Version = "1.1.3"
Version = "1.1.4"
listenAddress = flag.String("web.listen-address", ":9161", "Address to listen on for web interface and telemetry.")
metricPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
pTabRows = flag.Bool("tablerows", false, "Expose Table rows (CAN TAKE VERY LONG)")
Expand Down Expand Up @@ -185,7 +186,7 @@ func NewExporter() *Exporter {
Namespace: namespace,
Name: "query",
Help: "Self defined Queries from Configuration File.",
}, []string{"database","dbinstance","name"}),
}, []string{"database","dbinstance","name","column","row"}),
asmspace: prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: "asmspace",
Expand Down Expand Up @@ -221,21 +222,34 @@ func (e *Exporter) ScrapeQuery() {
err error
)
for _, conn := range config.Cfgs {
//num metric_name
//43 sessions
if conn.db != nil {
for _, query := range conn.Queries {
rows, err = conn.db.Query(query.Sql)
if err != nil {
continue
}

cols, _ := rows.Columns()
vals := make([]interface{}, len(cols))
var rownum int = 1

defer rows.Close()
for rows.Next() {
var value float64
if err := rows.Scan(&value); err != nil {
for i := range cols {
vals[i] = &vals[i]
}

err = rows.Scan(vals...)
if err != nil {
break
}
e.query.WithLabelValues(conn.Database,conn.Instance,query.Name).Set(value)

for i := range cols {
if value, ok := vals[i].(float64); ok {
e.query.WithLabelValues(conn.Database,conn.Instance,query.Name,cols[i],strconv.Itoa(rownum)).Set(value)
}
}
rownum ++
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions oracle.conf.example
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ connections:
- ORA-609
- ORA-3136
queries:
- sql: "select 1 from dual"
- sql: "select 1 as column1, 2 as column2 from dual"
name: sample1
- sql: "select 2 from dual"
- sql: "select 2 as column1 from dual"
name: sample2

- connection: <user>/<pass>@<tnsname>
Expand All @@ -36,9 +36,9 @@ connections:
- ORA-609
- ORA-3136
queries:
- sql: "select 3 from dual"
- sql: "select 3 as column1 from dual"
name: sample3
- sql: "select 4 from dual"
- sql: "select 4 as column1 from dual"
name: sample3

- connection:
Expand Down

0 comments on commit 11cabf7

Please sign in to comment.