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): Make sure to always create a request #15178

Merged
merged 1 commit into from
Apr 22, 2024
Merged
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
24 changes: 12 additions & 12 deletions plugins/inputs/opcua/read_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ type ReadClient struct {
Workarounds ReadClientWorkarounds

// internal values
req *ua.ReadRequest
ctx context.Context
reqIDs []*ua.ReadValueID
ctx context.Context
}

func (rc *ReadClientConfig) CreateReadClient(log telegraf.Logger) (*ReadClient, error) {
Expand Down Expand Up @@ -65,10 +65,10 @@ func (o *ReadClient) Connect() error {
return fmt.Errorf("initializing node IDs failed: %w", err)
}

readValueIDs := make([]*ua.ReadValueID, 0, len(o.NodeIDs))
o.reqIDs = make([]*ua.ReadValueID, 0, len(o.NodeIDs))
if o.Workarounds.UseUnregisteredReads {
for _, nid := range o.NodeIDs {
readValueIDs = append(readValueIDs, &ua.ReadValueID{NodeID: nid})
o.reqIDs = append(o.reqIDs, &ua.ReadValueID{NodeID: nid})
}
} else {
regResp, err := o.Client.RegisterNodes(o.ctx, &ua.RegisterNodesRequest{
Expand All @@ -79,16 +79,10 @@ func (o *ReadClient) Connect() error {
}

for _, v := range regResp.RegisteredNodeIDs {
readValueIDs = append(readValueIDs, &ua.ReadValueID{NodeID: v})
o.reqIDs = append(o.reqIDs, &ua.ReadValueID{NodeID: v})
}
}

o.req = &ua.ReadRequest{
MaxAge: 2000,
TimestampsToReturn: ua.TimestampsToReturnBoth,
NodesToRead: readValueIDs,
}

if err := o.read(); err != nil {
return fmt.Errorf("get data failed: %w", err)
}
Expand Down Expand Up @@ -136,7 +130,13 @@ func (o *ReadClient) CurrentValues() ([]telegraf.Metric, error) {
}

func (o *ReadClient) read() error {
resp, err := o.Client.Read(o.ctx, o.req)
req := &ua.ReadRequest{
MaxAge: 2000,
TimestampsToReturn: ua.TimestampsToReturnBoth,
NodesToRead: o.reqIDs,
}

resp, err := o.Client.Read(o.ctx, req)
if err != nil {
o.ReadError.Incr(1)
return fmt.Errorf("RegisterNodes Read failed: %w", err)
Expand Down
Loading