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

[exporter/influxdbexporter] Add support for exporting to InfluxDB v1.x API #16042

Merged
merged 2 commits into from
Dec 2, 2022
Merged
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
16 changes: 16 additions & 0 deletions .chloggen/feat_influxdb-1x.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: influxdbexporter

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add support for exporting to InfluxDB v1.X API

# One or more tracking issues related to the change
issues: [16042]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
5 changes: 5 additions & 0 deletions exporter/influxdbexporter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ The following configuration options are supported:
* `org` (required) Name of InfluxDB organization that owns the destination bucket
* `bucket` (required) name of InfluxDB bucket to which signals will be written
* `token` (optional) The authentication token for InfluxDB
* `v1_compatibility` (optional) Options for exporting to InfluxDB v1.x
* `enabled` (optional) Use InfluxDB v1.x API if enabled
* `db` (required if enabled) Name of the InfluxDB database to which signals will be written
* `username` (optional) Basic auth username for authenticating with InfluxDB v1.x
* `password` (optional) Basic auth password for authenticating with InfluxDB v1.x
* `metrics_schema` (default = telegraf-prometheus-v1) The chosen metrics schema to write; must be one of:
* `telegraf-prometheus-v1`
* `telegraf-prometheus-v2`
Expand Down
14 changes: 14 additions & 0 deletions exporter/influxdbexporter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ const (
stability = component.StabilityLevelBeta
)

// V1Compatibility is used to specify if the exporter should use the v1.X InfluxDB API schema.
type V1Compatibility struct {
// Enabled is used to specify if the exporter should use the v1.X InfluxDB API schema
Enabled bool `mapstructure:"enabled"`
// DB is used to specify the name of the V1 InfluxDB database that telemetry will be written to.
DB string `mapstructure:"db"`
// Username is used to optionally specify the basic auth username
Username string `mapstructure:"username"`
// Password is used to optionally specify the basic auth password
Password string `mapstructure:"password"`
}

// Config defines configuration for the InfluxDB exporter.
type Config struct {
config.ExporterSettings `mapstructure:",squash"`
Expand All @@ -43,6 +55,8 @@ type Config struct {
Bucket string `mapstructure:"bucket"`
// Token is used to identify InfluxDB permissions within the organization.
Token string `mapstructure:"token"`
// V1Compatibility is used to specify if the exporter should use the v1.X InfluxDB API schema.
V1Compatibility V1Compatibility `mapstructure:"v1_compatibility"`

// MetricsSchema indicates the metrics schema to emit to line protocol.
// Options:
Expand Down
35 changes: 27 additions & 8 deletions exporter/influxdbexporter/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package influxdbexporter // import "github.com/open-telemetry/opentelemetry-coll
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -45,21 +46,39 @@ func newInfluxHTTPWriter(logger common.Logger, config *Config, host component.Ho
return nil, err
}
if writeURL.Path == "" || writeURL.Path == "/" {
writeURL, err = writeURL.Parse("api/v2/write")
if err != nil {
return nil, err
if config.V1Compatibility.Enabled {
writeURL, err = writeURL.Parse("write")
if err != nil {
return nil, err
}
} else {
writeURL, err = writeURL.Parse("api/v2/write")
if err != nil {
return nil, err
}
}
}
queryValues := writeURL.Query()
queryValues.Set("org", config.Org)
queryValues.Set("bucket", config.Bucket)
queryValues.Set("precision", "ns")
writeURL.RawQuery = queryValues.Encode()

if config.Token != "" {
config.HTTPClientSettings.Headers["Authorization"] = "Token " + config.Token
if config.V1Compatibility.Enabled {
queryValues.Set("db", config.V1Compatibility.DB)

if config.V1Compatibility.Username != "" && config.V1Compatibility.Password != "" {
var basicAuth []byte
base64.StdEncoding.Encode(basicAuth, []byte(config.V1Compatibility.Username+":"+config.V1Compatibility.Password))
config.HTTPClientSettings.Headers["Authorization"] = "Basic " + string(basicAuth)
}
} else {
queryValues.Set("org", config.Org)
queryValues.Set("bucket", config.Bucket)

if config.Token != "" {
config.HTTPClientSettings.Headers["Authorization"] = "Token " + config.Token
}
}

writeURL.RawQuery = queryValues.Encode()
httpClient, err := config.HTTPClientSettings.ToClient(host, settings)
if err != nil {
return nil, err
Expand Down