Skip to content

Commit

Permalink
chore(inputs.opcua): Add option to collect client debug logging (infl…
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj authored Jan 22, 2024
1 parent 8cd487d commit f7208e5
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 2 deletions.
12 changes: 10 additions & 2 deletions plugins/common/opcua/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package opcua
import (
"context"
"fmt"
"log" //nolint:depguard // just for debug
"net/url"
"strconv"
"time"

"github.com/gopcua/opcua"
"github.com/gopcua/opcua/debug"
"github.com/gopcua/opcua/ua"

"github.com/influxdata/telegraf"
Expand Down Expand Up @@ -44,6 +46,7 @@ type OpcUAClientConfig struct {
AuthMethod string `toml:"auth_method"`
ConnectTimeout config.Duration `toml:"connect_timeout"`
RequestTimeout config.Duration `toml:"request_timeout"`
ClientTrace bool `toml:"client_trace"`

OptionalFields []string `toml:"optional_fields"`
Workarounds OpcUAWorkarounds `toml:"workarounds"`
Expand Down Expand Up @@ -87,15 +90,20 @@ func (o *OpcUAClientConfig) validateEndpoint() error {
return nil
}

func (o *OpcUAClientConfig) CreateClient(log telegraf.Logger) (*OpcUAClient, error) {
func (o *OpcUAClientConfig) CreateClient(telegrafLogger telegraf.Logger) (*OpcUAClient, error) {
err := o.Validate()
if err != nil {
return nil, err
}

if o.ClientTrace {
debug.Enable = true
debug.Logger = log.New(&DebugLogger{Log: telegrafLogger}, "", 0)
}

c := &OpcUAClient{
Config: o,
Log: log,
Log: telegrafLogger,
}
c.Log.Debug("Initialising OpcUAClient")

Expand Down
15 changes: 15 additions & 0 deletions plugins/common/opcua/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package opcua

import (
"github.com/influxdata/telegraf"
)

// DebugLogger logs messages from opcua at the debug level.
type DebugLogger struct {
Log telegraf.Logger
}

func (l *DebugLogger) Write(p []byte) (n int, err error) {
l.Log.Debug(string(p))
return len(p), nil
}
6 changes: 6 additions & 0 deletions plugins/inputs/opcua/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ to use them.
## "source" -- uses the timestamp provided by the source
# timestamp = "gather"
#
## Client trace messages
## When set to true, and debug mode enabled in the agent settings, the OPCUA
## client's messages are included in telegraf logs. These messages are very
## noisey, but essential for debugging issues.
# client_trace = false
#
## Include additional Fields in each metric
## Available options are:
## DataType -- OPC-UA Data Type (string)
Expand Down
6 changes: 6 additions & 0 deletions plugins/inputs/opcua/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
## "source" -- uses the timestamp provided by the source
# timestamp = "gather"
#
## Client trace messages
## When set to true, and debug mode enabled in the agent settings, the OPCUA
## client's messages are included in telegraf logs. These messages are very
## noisey, but essential for debugging issues.
# client_trace = false
#
## Include additional Fields in each metric
## Available options are:
## DataType -- OPC-UA Data Type (string)
Expand Down
7 changes: 7 additions & 0 deletions plugins/inputs/opcua_listener/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,13 @@ to use them.
# e.g.: json_timestamp_format = "2006-01-02T15:04:05Z07:00"
#timestamp_format = ""
#
#
## Client trace messages
## When set to true, and debug mode enabled in the agent settings, the OPCUA
## client's messages are included in telegraf logs. These messages are very
## noisey, but essential for debugging issues.
# client_trace = false
#
## Include additional Fields in each metric
## Available options are:
## DataType -- OPC-UA Data Type (string)
Expand Down
7 changes: 7 additions & 0 deletions plugins/inputs/opcua_listener/sample.conf
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@
# e.g.: json_timestamp_format = "2006-01-02T15:04:05Z07:00"
#timestamp_format = ""
#
#
## Client trace messages
## When set to true, and debug mode enabled in the agent settings, the OPCUA
## client's messages are included in telegraf logs. These messages are very
## noisey, but essential for debugging issues.
# client_trace = false
#
## Include additional Fields in each metric
## Available options are:
## DataType -- OPC-UA Data Type (string)
Expand Down

0 comments on commit f7208e5

Please sign in to comment.