Skip to content

Add SQL output plugin#9280

Merged
reimda merged 24 commits intomasterfrom
illuusio-sqloutput
Jun 4, 2021
Merged

Add SQL output plugin#9280
reimda merged 24 commits intomasterfrom
illuusio-sqloutput

Conversation

@reimda
Copy link
Copy Markdown
Contributor

@reimda reimda commented May 19, 2021

Add a SQL Output plugin that supports multiple DBMSes. This PR is an update of #4205 by @illuusio, adding integration tests and the ability to store the metric timestamp.

This plugin uses a simple, hard-coded database schema. The table name is the metric name. There is a column per field and a column per tag. It writes a row for every input metric. This means multiple metrics are never merged into a single row, even if they have the same metric name, tags, and timestamp.

This plugin generates simple SQL that is likely to work on any DBMS. It doesn't use any feature that is specific to a particular DBMS. If you want to use a feature that is specific to a particular DBMS, you may be able to set it up manually outside of this plugin or you may need to use a different plugin altogether. This means this plugin doesn't intend to remove the need for other SQL output plugins that are specialized for a single DBMS, for example #8651 for Postgres.

There is basic support to automatically create tables. Automatic table creation assumes input metrics always have the same tags and fields, and that the type of each field never changes. (The plugin doesn't attempt to alter existing tables to add or change columns based on input metrics). Use cases that don't have metrics with static schemas may require the user to create tables manually or to use a different plugin.

The integration tests run each DBMS in a docker container. Since these tests are relatively slow they are skipped in go's short mode unit tests. This is to allow short mode to complete quickly so it can be run as part of in CI builds for PRs. I intend to add a new nightly CI job that runs non short mode tests so they will be exercised regularly.

Draft PR to do list:

  • Currently the only integration test is for mariadb but I plan to add similar tests for sqlite and postgres.
  • The metric timestamp is always stored in a column called "timestamp" but I plan to make the column name configurable. I'll also try to make timestamp writing optional to allow use of db insertion timestamp like Added Generic SQL output plugin #4205 does.
  • Add nightly CI job for integration tests

@telegraf-tiger telegraf-tiger bot added feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin new plugin plugin/output 1. Request for new output plugins 2. Issues/PRs that are related to out plugins labels May 19, 2021
Copy link
Copy Markdown
Contributor

@ssoroka ssoroka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

quoted = append(quoted, quoteIdent(column))
}

sql := fmt.Sprintf("INSERT INTO %s(%s) VALUES(%s)", quoteIdent(tablename), strings.Join(quoted, ","), strings.Join(placeholder, ","))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

column names might need to be escaped for some weirder column names.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, there's also opportunity for a sql injection attack through the names of metrics, fields, and tags currently. I'll have to escape them to get them to work and sanitize them so they can't be misused.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added identifier quoting and a check for invalid characters that should cover this.

@reimda reimda force-pushed the illuusio-sqloutput branch from 7a021a8 to 268ea71 Compare May 25, 2021 22:09
@illuusio
Copy link
Copy Markdown

Is there any change to get this in?

@reimda
Copy link
Copy Markdown
Contributor Author

reimda commented May 26, 2021

Is there any change to get this in?

Hi @illuusio, I'd like to complete the to-do list in the PR description and add escaping like ssoroka mentioned in review. I also want to add to the readme, probably reusing some of this PR's description and adding a couple config examples.

I hope to get these things done in the next few days and have the PR reviewed and merged in time for the 1.19.0 release candidate on June 2.

@illuusio
Copy link
Copy Markdown

@reimda Should I close my PR to favor this? If everything goes like this I'll be happy camper with 1.19.

@reimda
Copy link
Copy Markdown
Contributor Author

reimda commented May 27, 2021

@illuusio This PR is your PR with tests and a couple fixes added, so it's safe to close your PR. The tests and fixes will make it easier to pass code review and hopefully a little easier to use.

I'm glad to hear you're happy about the changes I've made and the plan to get it into 1.19. I should have contacted you before updating your PR, but I was in a hurry because I have a need for this too. Thanks for your work writing #4205!

@reimda reimda force-pushed the illuusio-sqloutput branch from e676e32 to 5197a05 Compare June 3, 2021 04:02
illuusio and others added 16 commits June 3, 2021 09:56
… a struct instead of a vector of struct. Remove TagTableSuffix field as it's not used. Switch from metric.Tags and metric.Fields to TagList and FieldList to preserve order of fields and tags so they'll be predictable in unit tests. Don't drop the metric timestamp. Reformat README.md to pass markdown linter.
…quote strings, double quotes identifiers). Add a test metric with spaces in names. Add a setting for initialization sql that runs right after connection to the db. Use the initialization sql in the mysql integration test to set ANSI_QUOTES mode.
@reimda reimda force-pushed the illuusio-sqloutput branch from 5197a05 to 32b7e88 Compare June 3, 2021 16:19
@reimda reimda marked this pull request as ready for review June 4, 2021 04:07
@reimda reimda merged commit e289612 into master Jun 4, 2021
@reimda reimda deleted the illuusio-sqloutput branch June 4, 2021 04:49
@sjwang90 sjwang90 mentioned this pull request Jul 1, 2021
3 tasks
arstercz pushed a commit to arstercz/telegraf that referenced this pull request Aug 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin new plugin plugin/output 1. Request for new output plugins 2. Issues/PRs that are related to out plugins

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants