Skip to content

Commit

Permalink
Fixups
Browse files Browse the repository at this point in the history
- Add missing header to sample.conf and README
- Add user configurable tag to specify tag to use for inputID
- Remove ambiguous default value for optional config option
  • Loading branch information
bbergshaven committed May 9, 2023
1 parent 751daa3 commit 5238d76
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
13 changes: 10 additions & 3 deletions plugins/outputs/clarify/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ See the [CONFIGURATION.md][CONFIGURATION.md] for more details.
## Configuration

```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 All @@ -26,7 +29,8 @@ password = "secret-password"
# timeout = "20s"

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

You can use either a credentials file or username/password.
Expand All @@ -38,8 +42,11 @@ credentials file will be used.
Clarify signal names are formed by joining the Telegraf metric name and the
field key with a `.` character. Telegraf tags are added to signal labels.

If a tag named `clarify_input_id` is present as a tag and there is only one
field present in the metric, this tag will be used as the inputID in Clarify.
If you wish to specify a specific tag to use as the input id, set the config
option `clarify_id_tag` to the tag containing the id to be used.
If `clarify_id_tag = 'clarify_input_id'` and this tag is present and there
is only one field present in the metric, this tag will be used as the inputID
in Clarify.

If information from one or several tags is needed to uniquely identify a metric
field, the id_tags array can be added to the config with the needed tag names.
Expand Down
8 changes: 6 additions & 2 deletions plugins/outputs/clarify/clarify.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Clarify struct {
CredentialsFile string `toml:"credentials_file"`
Timeout config.Duration `toml:"timeout"`
IDTags []string `toml:"id_tags"`
ClarifyIDTag string `toml:"clarify_id_tag"`
Log telegraf.Logger `toml:"-"`

client *clarify.Client
Expand Down Expand Up @@ -149,8 +150,11 @@ func normalizeID(id string) string {
}

func (c *Clarify) generateID(m telegraf.Metric, f *telegraf.Field) (string, error) {
var id string
cid, exist := m.GetTag("clarify_input_id")
var id, cid string
exist := false
if c.ClarifyIDTag != "" {
cid, exist = m.GetTag(c.ClarifyIDTag)
}
if exist && len(m.FieldList()) == 1 {
id = cid
} else {
Expand Down
10 changes: 6 additions & 4 deletions plugins/outputs/clarify/clarify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ func (m *MockHandler) Do(ctx context.Context, _ jsonrpc.Request, result any) err

func TestGenerateID(t *testing.T) {
clfy := &Clarify{
Log: testutil.Logger{},
IDTags: []string{"tag1", "tag2"},
Log: testutil.Logger{},
IDTags: []string{"tag1", "tag2"},
ClarifyIDTag: "clarify_input_id",
}
var idTests = []struct {
inMetric telegraf.Metric
Expand Down Expand Up @@ -129,8 +130,9 @@ func TestGenerateID(t *testing.T) {

func TestProcessMetrics(t *testing.T) {
clfy := &Clarify{
Log: testutil.Logger{},
IDTags: []string{"tag1", "tag2", "node_id"},
Log: testutil.Logger{},
IDTags: []string{"tag1", "tag2", "node_id"},
ClarifyIDTag: "clarify_input_id",
}
var idTests = []struct {
inMetric telegraf.Metric
Expand Down
6 changes: 5 additions & 1 deletion plugins/outputs/clarify/sample.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 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 @@ -9,4 +12,5 @@ password = "secret-password"
# timeout = "20s"

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

0 comments on commit 5238d76

Please sign in to comment.