chore(inputs.opcua): Add option to collect client debug logging (#14595)

This commit is contained in:
Joshua Powers 2024-01-22 01:20:27 -07:00 committed by GitHub
parent 8cd487d052
commit f7208e5339
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 51 additions and 2 deletions

View File

@ -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"
@ -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"`
@ -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")

View File

@ -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
}

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)