Skip to content

Commit

Permalink
fixups
Browse files Browse the repository at this point in the history
  • Loading branch information
bbergshaven committed May 15, 2023
1 parent 75d0e70 commit 0883521
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 28 deletions.
2 changes: 1 addition & 1 deletion docs/LICENSE_OF_DEPENDENCIES.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ following works:
- github.com/cenkalti/backoff [MIT License](https://github.com/cenkalti/backoff/blob/master/LICENSE)
- github.com/cespare/xxhash [MIT License](https://github.com/cespare/xxhash/blob/master/LICENSE.txt)
- github.com/cisco-ie/nx-telemetry-proto [Apache License 2.0](https://github.com/cisco-ie/nx-telemetry-proto/blob/master/LICENSE)
- github.com/cloudevents/sdk-go [Apache License 2.0](https://github.com/cloudevents/sdk-go/blob/main/LICENSE)
- github.com/clarify/clarify-go [Apache License 2.0](https://github.com/clarify/clarify-go/blob/master/LICENSE)
- github.com/cloudevents/sdk-go [Apache License 2.0](https://github.com/cloudevents/sdk-go/blob/main/LICENSE)
- github.com/containerd/containerd [Apache License 2.0](https://github.com/containerd/containerd/blob/master/LICENSE)
- github.com/coocood/freecache [MIT License](https://github.com/coocood/freecache/blob/master/LICENSE)
- github.com/coreos/go-semver [Apache License 2.0](https://github.com/coreos/go-semver/blob/main/LICENSE)
Expand Down
8 changes: 3 additions & 5 deletions plugins/outputs/clarify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
```toml @sample.conf
## Configuration to publish Telegraf metrics to Clarify
[[outputs.clarify]]

## Credentials File (Oauth 2.0 from Clarify integration)
credentials_file = "/path/to/clarify/credentials.json"

Expand Down Expand Up @@ -57,10 +56,6 @@ E.g:
Clarify only supports values that can be converted to floating point numbers.
Strings and invalid numbers are ignored.

[clarify]: https://clarify.io
[clarifydoc]: https://docs.clarify.io
[credentials]: https://docs.clarify.io/users/admin/integrations/credentials

## Example

The following input would be stored in Clarify with the values shown below:
Expand All @@ -85,3 +80,6 @@ temperature,host=demo.clarifylocal,sensor=TC0P value=49 1682670910000000000
}
}
```
[clarify]: https://clarify.io
[clarifydoc]: https://docs.clarify.io
[credentials]: https://docs.clarify.io/users/admin/integrations/credentials
23 changes: 13 additions & 10 deletions plugins/outputs/clarify/clarify.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ type Clarify struct {

var errIDTooLong = errors.New("id too long (>128)")

const defaultTimeout = config.Duration(20 * time.Second)
const allowedIDRunes = `abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_:.#+/`

//go:embed sample.conf
var sampleConfig string

func (c *Clarify) Init() error {
// No blocking as it doesn't do any http requests, just sets up the necessarry Oauth2 client.
if c.Timeout <= 0 {
c.Timeout = defaultTimeout
}
// Not blocking as it doesn't do any http requests, just sets up the necessarry Oauth2 client.
ctx := context.Background()
switch {
case c.CredentialsFile != "":
Expand Down Expand Up @@ -80,11 +84,11 @@ func (c *Clarify) Write(metrics []telegraf.Metric) error {
defer cancel()

if _, err := c.client.Insert(frame).Do(ctx); err != nil {
return err
return fmt.Errorf("inserting failed %w", err)
}

if _, err := c.client.SaveSignals(signals).Do(ctx); err != nil {
return err
return fmt.Errorf("saving signals failed %w", err)
}

return nil
Expand Down Expand Up @@ -140,14 +144,13 @@ func normalizeID(id string) string {
}

func (c *Clarify) generateID(m telegraf.Metric, f *telegraf.Field) (string, error) {
var id, cid string
exist := false
var id string
if c.ClarifyIDTag != "" {
cid, exist = m.GetTag(c.ClarifyIDTag)
if cid, exist := m.GetTag(c.ClarifyIDTag); exist && len(m.FieldList()) == 1 {
id = cid
}
}
if exist && len(m.FieldList()) == 1 {
id = cid
} else {
if id == "" {
parts := make([]string, 0, len(c.IDTags)+2)
parts = append(parts, m.Name(), f.Key)

Expand Down Expand Up @@ -177,7 +180,7 @@ func (c *Clarify) Close() error {
func init() {
outputs.Add("clarify", func() telegraf.Output {
return &Clarify{
Timeout: config.Duration(20 * time.Second),
Timeout: defaultTimeout,
}
})
}
16 changes: 6 additions & 10 deletions plugins/outputs/clarify/clarify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"errors"
"math"
"reflect"
"testing"
"time"

Expand Down Expand Up @@ -38,12 +37,13 @@ type MockHandler struct {
func (m *MockHandler) Do(ctx context.Context, _ jsonrpc.Request, result any) error {
err := json.Unmarshal([]byte(m.jsonResult), result)
if m.sleep > 0 {
timer1 := time.NewTimer(m.sleep)
timer := time.NewTimer(m.sleep)
select {
case <-ctx.Done():
timer.Stop()
return errTimeout
case <-timer1.C:
timer1.Stop()
case <-timer.C:
timer.Stop()
return nil
}
}
Expand Down Expand Up @@ -281,12 +281,8 @@ func TestProcessMetrics(t *testing.T) {
}
for _, tt := range idTests {
of, os := clfy.processMetrics([]telegraf.Metric{tt.inMetric})
if !reflect.DeepEqual(of, tt.outFrame) {
t.Errorf("\nexpected %+v\ngot %+v\n", tt.outFrame, of)
}
if !reflect.DeepEqual(os, tt.outSignals) {
t.Errorf("\nexpected %+v\ngot %+v\n", tt.outSignals, os)
}
require.EqualValues(t, tt.outFrame, of)
require.EqualValues(t, tt.outSignals, os)
}
}

Expand Down
3 changes: 1 addition & 2 deletions plugins/outputs/clarify/sample.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
## Configuration to publish Telegraf metrics to Clarify
[[outputs.clarify]]

## Credentials File (Oauth 2.0 from Clarify integration)
credentials_file = "/path/to/clarify/credentials.json"

Expand All @@ -13,4 +12,4 @@ password = "secret-password"

## Optional tags to be included when generating the unique ID for a signal in Clarify
# id_tags = []
# clarify_id_tag = 'clarify_input_id'
# clarify_id_tag = 'clarify_input_id'

0 comments on commit 0883521

Please sign in to comment.