Skip to content

Conversation

@kongfei605
Copy link
Collaborator

No description provided.

Copilot AI review requested due to automatic review settings December 23, 2025 08:25
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors the snmp_zabbix input plugin to leverage native JavaScript JSON support from the goja library, simplifying the implementation by removing custom JSON handling code.

Key Changes:

  • Removes custom JSON.parse and JSON.stringify implementations that were manually injected into the JavaScript VM
  • Removes automatic unwrapping of Zabbix LLD JSON {"data": [...]} format, simplifying value injection logic
  • Adds comprehensive test coverage for JavaScript preprocessing with JSON operations

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 5 comments.

File Description
inputs/snmp_zabbix/preprocessing.go Removed ~20 lines of custom JSON handling and LLD auto-unwrapping logic; simplified value injection to pass values directly to the JavaScript VM
inputs/snmp_zabbix/preprocessing_test.go Added new test file with test cases for JSON.parse, JSON.stringify, and invalid JSON handling to verify native JavaScript JSON support works correctly

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 32 to 44
scriptStringify := `
var obj = {a: 1, b: "test"};
return JSON.stringify(obj);
`
resStr, err := applyJavaScript(nil, []string{scriptStringify})
if err != nil {
t.Errorf("JSON.stringify failed: %v", err)
}
// Verify naive string contains keys
s := resStr.(string)
if !reflect.DeepEqual(s, `{"a":1,"b":"test"}`) && !reflect.DeepEqual(s, `{"b":"test","a":1}`) {
t.Errorf("Unexpected JSON string: %s", s)
}
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

Consider adding a test case for JSON.stringify with edge cases like arrays, nested objects, null values, and special characters to ensure the native JSON implementation handles all scenarios correctly. This would help verify that the transition from custom JSON implementation to native support is complete.

Copilot uses AI. Check for mistakes.
t.Errorf("JSON.stringify failed: %v", err)
}
// Verify naive string contains keys
s := resStr.(string)
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

The type assertion on line 41 is unsafe and will panic if resStr is not a string. Consider adding a safety check similar to the one used for numeric types in the test above (lines 19-28), or use the comma-ok idiom to verify the type before accessing it as a string.

Suggested change
s := resStr.(string)
s, ok := resStr.(string)
if !ok {
t.Errorf("Unexpected result type for JSON.stringify: %T %v", resStr, resStr)
return
}

Copilot uses AI. Check for mistakes.
if err != nil {
t.Errorf("JSON.stringify failed: %v", err)
}
// Verify naive string contains keys
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

The comment says "Verify naive string contains keys" but the actual check uses reflect.DeepEqual to verify exact JSON string equality (with two possible key orderings). The comment should be updated to accurately describe what the test is doing, such as "Verify JSON string matches expected format with proper key-value serialization".

Suggested change
// Verify naive string contains keys
// Verify JSON string matches expected format with proper key-value serialization (allowing for key order differences)

Copilot uses AI. Check for mistakes.
}
// Verify naive string contains keys
s := resStr.(string)
if !reflect.DeepEqual(s, `{"a":1,"b":"test"}`) && !reflect.DeepEqual(s, `{"b":"test","a":1}`) {
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

Using reflect.DeepEqual to compare strings is unnecessarily complex and less efficient than a simple string equality check. Consider replacing this with a direct comparison: if s != {"a":1,"b":"test"}&& s !={"b":"test","a":1} { for better readability and performance.

Copilot uses AI. Check for mistakes.
Copilot AI review requested due to automatic review settings December 23, 2025 08:34
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

if !ok {
t.Fatalf("Expected string result, got %T %v", res, res)
}
// Verify naive string contains keys (order may vary)
Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

The comment says "Verify naive string contains keys" but the verification is checking for exact JSON string matches. This is misleading since the code is actually performing exact string comparison, not a "naive" check. Consider updating the comment to be more accurate, such as "Check both possible JSON key orderings".

Suggested change
// Verify naive string contains keys (order may vary)
// Check both possible JSON key orderings

Copilot uses AI. Check for mistakes.
// 如果解析失败或 'data' 键不存在, valueToInject 将保持为原始字符串,这是安全的
}
}

Copy link

Copilot AI Dec 23, 2025

Choose a reason for hiding this comment

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

This PR removes the automatic LLD JSON unwrapping logic that previously extracted the 'data' field from Zabbix LLD JSON format. Users who have JavaScript preprocessing steps that rely on this auto-unwrapping behavior will need to manually parse the JSON and access the 'data' field. Consider documenting this breaking change in the commit message or README, and providing migration guidance for existing users.

Suggested change
// 兼容逻辑:如果传入的是 Zabbix LLD JSON 字符串,自动展开其中的 "data" 字段
if strVal, ok := value.(string); ok {
var decoded map[string]interface{}
if uerr := json.Unmarshal([]byte(strVal), &decoded); uerr == nil {
if data, ok := decoded["data"]; ok {
value = data
}
}
}

Copilot uses AI. Check for mistakes.
@kongfei605 kongfei605 merged commit e15ad96 into flashcatcloud:main Dec 23, 2025
9 checks passed
@kongfei605 kongfei605 deleted the snmp_zbx_js branch December 23, 2025 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant