Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Commit

Permalink
Add influxdb emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
myoung34 committed Aug 30, 2022
1 parent f748866 commit 0d52b4c
Show file tree
Hide file tree
Showing 93 changed files with 110,399 additions and 670 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test:


build:
GODEBUG=cgocheck=0 go build -o dist/tilty
GOOS=linux GOARCH=arm GODEBUG=cgocheck=0 go build -o dist/tilty

run:
sudo ./dist/tilty -c test.ini
Expand Down
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Tilty
[![Docker Pulls](https://img.shields.io/docker/pulls/myoung34/tilty.svg)](https://hub.docker.com/r/myoung34/tilty)

![](assets/datadog.png)
![](assets/influxdb.png)

A CLI to capture and emit events from your [tilt hydrometer](https://tilthydrometer.com/)

Expand All @@ -25,6 +26,7 @@ The Tilt supports writing to a google doc which you could use with something lik
* Generic (Send to any endpoint with any type)
* Brewstat.us (Example below)
* BrewersFriend (Example below)
* InfluxDB (1.8+)
* Datadog (dogstatsd)
* SQLite

Expand Down Expand Up @@ -87,6 +89,15 @@ enabled = true
statsd_host = "statsdhost.corp.com"
statsd_port = 8125
[influxdb]
url = "http://localhost:8086"
verify_ssl = true
bucket = "tilty"
org = "Mine"
token = "myuser:password"
gravity_payload_template = "gravity,color={{.Color}},mac={{.Mac}} sg={{.Gravity}}"
temperature_payload_template = "temperature,color={{.Color}},mac={{.Mac}} temp={{.Temp}}"
```

### Run ###
Expand Down
Binary file added assets/influxdb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
82 changes: 82 additions & 0 deletions emitters/influxdb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package emitters

import (
"bytes"
"context"
"crypto/tls"
"encoding/json"
"github.com/go-kit/log/level"
influxdb2 "github.com/influxdata/influxdb-client-go/v2"
"github.com/myoung34/tilty/tilt"
"strconv"
"text/template"
)

type InfluxDB struct {
Enabled bool
URL string `json:"url"`
VerifySSL bool `json:"verify_ssl"`
Bucket string `json:"bucket"`
Org string `json:"org"`
Token string `json:"token"`
GravityPayloadTemplate string `json:"gravity_payload_template"`
TemperaturePayloadTemplate string `json:"temperature_payload_template"`
}

//gravity_payload_template = gravity,color={{ color }},mac={{ mac }} sg={{ gravity }}
//temperature_payload_template = temperature,color={{ color }},mac={{ mac }} temp={{ temp }}

func InfluxDBEmit(payload tilt.Payload, emitterConfig interface{}) (string, error) {
influxdb := InfluxDB{}
jsonString, _ := json.Marshal(emitterConfig)
json.Unmarshal(jsonString, &influxdb)

client := influxdb2.NewClientWithOptions(influxdb.URL, influxdb.Token,
influxdb2.DefaultOptions().
SetTLSConfig(&tls.Config{
InsecureSkipVerify: influxdb.VerifySSL,
}))
writeAPI := client.WriteAPIBlocking(influxdb.Org, influxdb.Bucket)

payloadTemplate := Template{
Color: payload.Color,
Gravity: strconv.Itoa(int(payload.Minor)),
Mac: payload.Mac,
Temp: strconv.Itoa(int(payload.Major)),
Timestamp: payload.Timestamp,
}

// Generate the gravity body from a template
gravityTmpl, err := template.New("influxdb").Parse(`"gravity,color={{.Color}},mac={{.Mac}} sg={{.Gravity}}"`)
if len(influxdb.GravityPayloadTemplate) > 0 {
gravityTmpl, err = template.New("influxdb").Parse(influxdb.GravityPayloadTemplate)
}
if err != nil {
level.Error(tilt.Logger).Log("emitters.influxdb", err)
return "", err
}
var gravityTpl bytes.Buffer
if err := gravityTmpl.Execute(&gravityTpl, payloadTemplate); err != nil {
level.Error(tilt.Logger).Log("emitters.influxdb", err)
return "", err
}
writeAPI.WriteRecord(context.Background(), gravityTpl.String())

// Generate the temperature body from a template
temperatureTmpl, err := template.New("influxdb").Parse(`"gravity,color={{.Color}},mac={{.Mac}} sg={{.Gravity}}"`)
if len(influxdb.TemperaturePayloadTemplate) > 0 {
temperatureTmpl, err = template.New("influxdb").Parse(influxdb.TemperaturePayloadTemplate)
}
if err != nil {
level.Error(tilt.Logger).Log("emitters.influxdb", err)
return "", err
}
var temperatureTpl bytes.Buffer
if err := temperatureTmpl.Execute(&temperatureTpl, payloadTemplate); err != nil {
level.Error(tilt.Logger).Log("emitters.influxdb", err)
return "", err
}
writeAPI.WriteRecord(context.Background(), temperatureTpl.String())

return "", nil
}
8 changes: 6 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ go 1.19
require (
github.com/DataDog/datadog-go/v5 v5.1.1
github.com/akamensky/argparse v1.4.0
github.com/go-kit/kit v0.12.0
github.com/go-kit/log v0.2.0
github.com/go-playground/validator/v10 v10.11.0
github.com/influxdata/influxdb-client-go/v2 v2.10.0
github.com/jarcoal/httpmock v1.2.0
github.com/mattn/go-sqlite3 v1.14.15
github.com/myoung34/gatt v0.0.0-20220817003501-ce14497a0f85
Expand All @@ -17,24 +18,27 @@ require (
require (
github.com/Microsoft/go-winio v0.5.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/deepmap/oapi-codegen v1.8.2 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-kit/log v0.2.0 // indirect
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/magiconair/properties v1.8.6 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/pelletier/go-toml/v2 v2.0.1 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/afero v1.8.2 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.3.0 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
Expand Down
Loading

0 comments on commit 0d52b4c

Please sign in to comment.