mirror of
https://github.com/SAP/jenkins-library.git
synced 2024-12-16 11:09:33 +02:00
54f2a0d471
* Added go-based influxWriteData step * Wrote tests & fixed issues * Fixed issues * Created go-based step tests. Fixed issues * Fixed issues * Integration test was added Co-authored-by: Oliver Nocon <33484802+OliverNocon@users.noreply.github.com> Co-authored-by: Sven Merk <33895725+nevskrem@users.noreply.github.com>
46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
package influx
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
|
|
)
|
|
|
|
// Client handles communication with InfluxDB
|
|
type Client struct {
|
|
client influxdb2.Client
|
|
ctx context.Context
|
|
organization string
|
|
bucket string
|
|
}
|
|
|
|
// NewClient instantiates a Client
|
|
func NewClient(influxClient influxdb2.Client, organization string, bucket string) *Client {
|
|
ctx := context.Background()
|
|
client := Client{
|
|
client: influxClient,
|
|
ctx: ctx,
|
|
organization: organization,
|
|
bucket: bucket,
|
|
}
|
|
return &client
|
|
}
|
|
|
|
// WriteMetrics writes metrics to InfluxDB
|
|
func (c *Client) WriteMetrics(dataMap map[string]map[string]interface{}, dataMapTags map[string]map[string]string) error {
|
|
writeAPI := c.client.WriteAPIBlocking(c.organization, c.bucket)
|
|
|
|
for measurement, fields := range dataMap {
|
|
tags := dataMapTags[measurement]
|
|
point := influxdb2.NewPoint(measurement,
|
|
tags,
|
|
fields,
|
|
time.Now())
|
|
if err := writeAPI.WritePoint(c.ctx, point); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|