feat(outputs.postgresql): Add option to create time column with timezone (#13763)
This commit is contained in:
parent
3f206f6ce0
commit
71905a7758
|
|
@ -58,6 +58,11 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
|
|||
## NOTE: Some tools (e.g. Grafana) require the default name so be careful!
|
||||
# timestamp_column_name = "time"
|
||||
|
||||
## Type of the timestamp column
|
||||
## Currently, "timestamp without time zone" and "timestamp with time zone"
|
||||
## are supported
|
||||
# timestamp_column_type = "timestamp without time zone"
|
||||
|
||||
## Templated statements to execute when creating a new table.
|
||||
# create_templates = [
|
||||
# '''CREATE TABLE {{ .table }} ({{ .columns }})''',
|
||||
|
|
|
|||
|
|
@ -42,6 +42,7 @@ type Postgresql struct {
|
|||
TagsAsJsonb bool `toml:"tags_as_jsonb"`
|
||||
FieldsAsJsonb bool `toml:"fields_as_jsonb"`
|
||||
TimestampColumnName string `toml:"timestamp_column_name"`
|
||||
TimestampColumnType string `toml:"timestamp_column_type"`
|
||||
CreateTemplates []*sqltemplate.Template `toml:"create_templates"`
|
||||
AddColumnTemplates []*sqltemplate.Template `toml:"add_column_templates"`
|
||||
TagTableCreateTemplates []*sqltemplate.Template `toml:"tag_table_create_templates"`
|
||||
|
|
@ -110,10 +111,19 @@ func (p *Postgresql) Init() error {
|
|||
p.TimestampColumnName = "time"
|
||||
}
|
||||
|
||||
switch p.TimestampColumnType {
|
||||
case "":
|
||||
p.TimestampColumnType = PgTimestampWithoutTimeZone
|
||||
case PgTimestampWithoutTimeZone, PgTimestampWithTimeZone:
|
||||
// do nothing for the valid choices
|
||||
default:
|
||||
return fmt.Errorf("unknown timestamp column type %q", p.TimestampColumnType)
|
||||
}
|
||||
|
||||
// Initialize the column prototypes
|
||||
p.timeColumn = utils.Column{
|
||||
Name: p.TimestampColumnName,
|
||||
Type: PgTimestampWithoutTimeZone,
|
||||
Type: p.TimestampColumnType,
|
||||
Role: utils.TimeColType,
|
||||
}
|
||||
p.tagIDColumn = utils.Column{Name: "tag_id", Type: PgBigInt, Role: utils.TagsIDColType}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,11 @@
|
|||
## NOTE: Some tools (e.g. Grafana) require the default name so be careful!
|
||||
# timestamp_column_name = "time"
|
||||
|
||||
## Type of the timestamp column
|
||||
## Currently, "timestamp without time zone" and "timestamp with time zone"
|
||||
## are supported
|
||||
# timestamp_column_type = "timestamp without time zone"
|
||||
|
||||
## Templated statements to execute when creating a new table.
|
||||
# create_templates = [
|
||||
# '''CREATE TABLE {{ .table }} ({{ .columns }})''',
|
||||
|
|
|
|||
|
|
@ -470,3 +470,28 @@ func TestTableManager_addColumnTemplates(t *testing.T) {
|
|||
|
||||
assert.Equal(t, 1, stmtCount)
|
||||
}
|
||||
|
||||
func TestTableManager_TimeWithTimezone(t *testing.T) {
|
||||
p := newPostgresqlTest(t)
|
||||
p.TagsAsForeignKeys = true
|
||||
p.TimestampColumnType = "timestamp with time zone"
|
||||
require.NoError(t, p.Init())
|
||||
require.NoError(t, p.Connect())
|
||||
|
||||
metrics := []telegraf.Metric{
|
||||
newMetric(t, "", MSS{"pop": "tart"}, MSI{"a": 1, "b": 2}),
|
||||
}
|
||||
tsrc := NewTableSources(p.Postgresql, metrics)[t.Name()]
|
||||
require.NoError(t, p.tableManager.MatchSource(ctx, p.db, tsrc))
|
||||
p.Logger.Info("ok")
|
||||
|
||||
expected := `CREATE TABLE "public"."TestTableManager_TimeWithTimezone" ("time" timestamp with time zone, "tag_id" bigint, "a" bigint, "b" bigint)`
|
||||
stmtCount := 0
|
||||
for _, log := range p.Logger.Logs() {
|
||||
if strings.Contains(log.String(), expected) {
|
||||
stmtCount++
|
||||
}
|
||||
}
|
||||
|
||||
require.Equal(t, 1, stmtCount)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue