Skip to content

Commit

Permalink
Make table name configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
amotl committed Jan 23, 2024
1 parent c2c1d5e commit a7c602c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
14 changes: 9 additions & 5 deletions backlog.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
# Backlog

## Iteration +1
- Make schema **and** table name configurable
- Regular expressions not working
- https://github.com/crate/cratedb-prometheus-adapter/issues/24
- https://prometheus.io/docs/prometheus/latest/querying/basics/
- https://prometheus.io/docs/prometheus/latest/querying/examples/
- Make `tablename` setting configurable. Right now, it is hard-coded to `schema`
- Expose metrics about both database connection pools
https://github.com/crate/cratedb-prometheus-adapter/pull/105

Expand All @@ -29,3 +25,11 @@
- Refactor metric names
> Generated/dynamic metric names are a sign that you should be using labels instead.
> -- https://prometheus.io/docs/instrumenting/writing_clientlibs/#metric-names

## Done
- Regular expressions not working?
- https://github.com/crate/cratedb-prometheus-adapter/issues/24
- https://prometheus.io/docs/prometheus/latest/querying/basics/
- https://prometheus.io/docs/prometheus/latest/querying/examples/
- Make `schema` setting configurable
7 changes: 5 additions & 2 deletions crate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"github.com/prometheus/common/model"
)

const crateWriteStatement = `INSERT INTO metrics ("labels", "labels_hash", "timestamp", "value", "valueRaw") VALUES ($1, $2, $3, $4, $5)`
const crateWriteStatement = `INSERT INTO "%s" ("labels", "labels_hash", "timestamp", "value", "valueRaw") VALUES ($1, $2, $3, $4, $5)`

type crateRow struct {
labels model.Metric
Expand All @@ -37,6 +37,7 @@ type crateReadResponse struct {

type crateEndpoint struct {
poolConf *pgxpool.Config
tableName string
readPoolSize int
writePoolSize int
readTimeout time.Duration
Expand Down Expand Up @@ -96,14 +97,16 @@ func newCrateEndpoint(ep *endpointConfig) *crateEndpoint {
}
}

_, err := conn.Prepare(ctx, "write_statement", crateWriteStatement)
writeStatement := fmt.Sprintf(crateWriteStatement, ep.Table)
_, err := conn.Prepare(ctx, "write_statement", writeStatement)
if err != nil {
return fmt.Errorf("error preparing write statement: %v", err)
}
return err
}
return &crateEndpoint{
poolConf: poolConf,
tableName: ep.Table,
readPoolSize: ep.ReadPoolSize,
writePoolSize: ep.WritePoolSize,
readTimeout: time.Duration(ep.ReadTimeout) * time.Second,
Expand Down
7 changes: 6 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ type crateDbPrometheusAdapter struct {
}

func (ca *crateDbPrometheusAdapter) runQuery(q *prompb.Query) ([]*prompb.TimeSeries, error) {
//fmt.Printf("QUERY: %s", config{}.Endpoints[0].Table)
query, err := queryToSQL(q)
if err != nil {
return nil, err
Expand Down Expand Up @@ -357,6 +358,7 @@ type endpointConfig struct {
Port uint16 `yaml:"port"`
User string `yaml:"user"`
Password string `yaml:"password"`
Table string `yaml:"table"`
Schema string `yaml:"schema"`
MaxConnections int `yaml:"max_connections"`
ReadPoolSize int `yaml:"read_pool_size_max"`
Expand All @@ -383,8 +385,11 @@ func (ep *endpointConfig) toDSN() string {
if ep.Password != "" {
params = append(params, fmt.Sprintf("password=%s", ep.Password))
}
if ep.Table != "" {
params = append(params, fmt.Sprintf("table=%s", ep.Table))
}
if ep.Schema != "" {
params = append(params, fmt.Sprintf("database=%s", ep.Schema))
params = append(params, fmt.Sprintf("schema=%s", ep.Schema))
}
if ep.ConnectTimeout != 0 {
params = append(params, fmt.Sprintf("connect_timeout=%v", ep.ConnectTimeout))
Expand Down

0 comments on commit a7c602c

Please sign in to comment.