Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(inputs.opcua): add more data to error log #10465

Merged
merged 1 commit into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion plugins/inputs/opcua/opcua_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,9 @@ func (o *OpcUA) getData() error {
for i, d := range resp.Results {
o.nodeData[i].Quality = d.Status
if !o.checkStatusCode(d.Status) {
o.Log.Errorf("status not OK for node %v: %v", o.nodes[i].tag.FieldName, d.Status)
mp := newMP(&o.nodes[i])
o.Log.Errorf("status not OK for node '%s'(metric name '%s', tags '%s')",
mp.fieldName, mp.metricName, mp.tags)
continue
}
o.nodeData[i].TagName = o.nodes[i].tag.FieldName
Expand Down
68 changes: 68 additions & 0 deletions plugins/inputs/opcua/opcua_client_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package opcua_client

import (
"context"
"fmt"
"reflect"
"testing"
"time"

"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"

"github.com/gopcua/opcua/ua"
"github.com/influxdata/telegraf/config"
Expand All @@ -21,6 +24,71 @@ type OPCTags struct {
Want interface{}
}

func TestGetDataBadNodeContainerIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}

// Spin-up the container
ctx := context.Background()
req := testcontainers.GenericContainerRequest{
ContainerRequest: testcontainers.ContainerRequest{
Image: "open62541/open62541:1.0",
ExposedPorts: []string{"4840/tcp"},
WaitingFor: wait.ForListeningPort("4840/tcp"),
},
Started: true,
}
container, err := testcontainers.GenericContainer(ctx, req)
require.NoError(t, err, "starting container failed")
defer func() {
require.NoError(t, container.Terminate(ctx), "terminating container failed")
}()

// Get the connection details from the container
addr, err := container.Host(ctx)
require.NoError(t, err, "getting container host address failed")
p, err := container.MappedPort(ctx, "4840/tcp")
require.NoError(t, err, "getting container host port failed")
port := p.Port()

var testopctags = []OPCTags{
{"ProductName", "1", "i", "2261", "open62541 OPC UA Server"},
{"ProductUri", "0", "i", "2262", "http://open62541.org"},
{"ManufacturerName", "0", "i", "2263", "open62541"},
}

var o OpcUA
o.MetricName = "testing"
o.Endpoint = fmt.Sprintf("opc.tcp://%s:%s", addr, port)
fmt.Println(o.Endpoint)
o.AuthMethod = "Anonymous"
o.ConnectTimeout = config.Duration(10 * time.Second)
o.RequestTimeout = config.Duration(1 * time.Second)
o.SecurityPolicy = "None"
o.SecurityMode = "None"
o.codes = []ua.StatusCode{ua.StatusOK}
logger := &testutil.CaptureLogger{}
o.Log = logger

g := GroupSettings{
MetricName: "anodic_current",
TagsSlice: [][]string{
{"pot", "2002"},
},
}

for _, tags := range testopctags {
g.Nodes = append(g.Nodes, MapOPCTag(tags))
}
o.Groups = append(o.Groups, g)
err = o.Init()
require.NoError(t, err)
err = Connect(&o)
require.NoError(t, err)
require.Contains(t, logger.LastError, "E! [] status not OK for node 'ProductName'(metric name 'anodic_current', tags 'pot=2002')")
}

func TestClient1Integration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
Expand Down