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): Auto fan out array nodes into individual fields #16359

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

inhinias
Copy link

Summary

Reading an entire OPC-UA array results in the metric creation to fail silently and reading an individual array element result in a bad node ID.

This PR detects an array and fans it out. So a read array results in each index being collected as a separate metric e.g: MetricName + Index.

This is just a simple way to get it to work. Maybe there is a way to store entire array as a metric, but I'm not all too familiar with the codebase as a whole. Options welcome.

Also Note:

  • Reading an array structure e.g: NodeId.Foo[1].bar works
  • Reading a single index does not work as mentioned above. (This PR does not fix this) For that OpcUa indexRange must be used together with some parsing of the node id so classical [index] notation can be used. See: https://reference.opcfoundation.org/Core/Part4/v104/docs/7.24

Checklist

  • No AI generated code was used in this PR

Related issues

resolves #11898

@telegraf-tiger telegraf-tiger bot added area/opcua fix pr to fix corresponding bug plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins labels Dec 31, 2024
@telegraf-tiger
Copy link
Contributor

Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your fix @inhinias! I do have one comment in the code. Furthermore, I wonder if we can get rid of the reflection in here and do explicit type assertions?

Could you also please provide a unit-test so we don't run into this again?

Comment on lines +434 to +442
valueArray := unpackArray(o.LastReceivedData[nodeIdx].Value)
if len(valueArray) == int(o.LastReceivedData[nodeIdx].ArrayLength) {
for index, value := range valueArray {
var sb strings.Builder
sb.WriteString(nmm.Tag.FieldName)
sb.WriteString(strconv.Itoa(int(index)))
fields[sb.String()] = value
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about folding the unpackArray function into here and reuse its for loop? This saves both allocations and loop iterations...

@srebhan srebhan self-assigned this Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/opcua fix pr to fix corresponding bug plugin/input 1. Request for new input plugins 2. Issues/PRs that are related to input plugins
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OPCUA - Read the data of an array
2 participants