Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make table name configurable #125

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading