Skip to content

Conversation

@jajeffries
Copy link
Contributor

@jajeffries jajeffries commented Nov 18, 2025

This pull request refactors the SNMP client factory and related code to allow passing a logger for improved logging and debugging. It introduces a new SlogAdapter to integrate structured logging with the underlying SNMP library, and updates all usages and tests to support the new logger-aware client factory signature.

Logging integration and client factory refactor

  • Refactored the ClientFactory type and all usages to accept a *slog.Logger parameter, enabling logger-aware SNMP client creation throughout the codebase. (snmp-discovery/snmp/snmp.go, snmp-discovery/policy/manager.go, snmp-discovery/snmp/mocks.go, snmp-discovery/snmp/snmp_test.go) [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12]
  • Added the SlogAdapter type to bridge slog.Logger with the gosnmp.LoggerInterface, ensuring SNMP debug output is captured in structured logs when debug is enabled. (snmp-discovery/snmp/snmp.go)
  • Updated the NewClient function to attach a gosnmp.Logger when debug logging is enabled, and propagate the logger to SNMP client instances for all supported protocol versions. (snmp-discovery/snmp/snmp.go) [1] [2] [3] [4]

Test and mock updates

  • Updated all SNMP client factory mocks and test cases to use the new logger-aware signature, ensuring tests remain compatible with the refactored interface. (snmp-discovery/snmp/snmp_test.go, snmp-discovery/snmp/mocks.go, snmp-discovery/policy/runner_test.go) [1] [2] [3] [4] [5] [6] [7] [8] [9]

Minor improvements

  • Added missing imports (time, log/slog, context) to support new functionality. (snmp-discovery/policy/manager.go, snmp-discovery/snmp/mocks.go, snmp-discovery/snmp/snmp.go) [1] [2] [3]

This refactor improves observability and debugging for SNMP operations, making it easier to trace issues and monitor SNMP interactions in production environments.

Example output

{"time":"2025-11-19T15:05:57.005187711Z","level":"DEBUG","msg":"getResponseLength: 36","backend":"snmp_discovery"}
{"time":"2025-11-19T15:05:57.005189606Z","level":"DEBUG","msg":"parseRawField: request id","backend":"snmp_discovery"}
{"time":"2025-11-19T15:05:57.00522451Z","level":"DEBUG","msg":"requestID: 1610386809","backend":"snmp_discovery"}
{"time":"2025-11-19T15:05:57.005227198Z","level":"DEBUG","msg":"parseRawField: error-status","backend":"snmp_discovery"}
{"time":"2025-11-19T15:05:57.005229406Z","level":"DEBUG","msg":"errorStatus: 0","backend":"snmp_discovery"}
{"time":"2025-11-19T15:05:57.005230908Z","level":"DEBUG","msg":"parseRawField: error index","backend":"snmp_discovery"}
{"time":"2025-11-19T15:05:57.00523241Z","level":"DEBUG","msg":"error-index: 0","backend":"snmp_discovery"}
{"time":"2025-11-19T15:05:57.005235546Z","level":"DEBUG","msg":"vblLength: 22","backend":"snmp_discovery"}
{"time":"2025-11-19T15:05:57.005239569Z","level":"DEBUG","msg":"parseRawField: OID","backend":"snmp_discovery"}
{"time":"2025-11-19T15:05:57.005242016Z","level":"DEBUG","msg":"OID: .1.3.6.1.2.1.2.2.1.5.1","backend":"snmp_discovery"}
{"time":"2025-11-19T15:05:57.005244157Z","level":"DEBUG","msg":"decodeValue: type is Gauge32","backend":"snmp_discovery"}
{"time":"2025-11-19T15:05:57.005246679Z","level":"DEBUG","msg":"decodeValue: value is 0x989680","backend":"snmp_discovery"}
{"time":"2025-11-19T15:05:57.005248993Z","level":"DEBUG","msg":"Walk completed with NoError","backend":"snmp_discovery"}
{"time":"2025-11-19T15:05:57.005251966Z","level":"DEBUG","msg":"SEND INIT","backend":"snmp_discovery"}

@github-actions
Copy link

Go test coverage

Status Elapsed Package Cover Pass Fail Skip
🟢 PASS 1.01s github.com/netboxlabs/orb-discovery/snmp-discovery/config 100.0% 8 0 0
🟢 PASS 4.81s github.com/netboxlabs/orb-discovery/snmp-discovery/data 84.0% 55 0 0
🟢 PASS 1.02s github.com/netboxlabs/orb-discovery/snmp-discovery/env 85.7% 15 0 0
🟢 PASS 1.11s github.com/netboxlabs/orb-discovery/snmp-discovery/mapping 90.8% 240 0 0
🟢 PASS 1.02s github.com/netboxlabs/orb-discovery/snmp-discovery/metrics 83.6% 24 0 0
🟢 PASS 15.14s github.com/netboxlabs/orb-discovery/snmp-discovery/policy 87.5% 35 0 0
🟢 PASS 1.94s github.com/netboxlabs/orb-discovery/snmp-discovery/server 85.5% 19 0 0
🟢 PASS 1.03s github.com/netboxlabs/orb-discovery/snmp-discovery/snmp 79.4% 37 0 0
🟢 PASS 1.02s github.com/netboxlabs/orb-discovery/snmp-discovery/targets 100.0% 23 0 0
🟢 PASS 1.01s github.com/netboxlabs/orb-discovery/snmp-discovery/version 100.0% 1 0 0
Total coverage: 88.0%

@jajeffries jajeffries changed the title turn on gosnmp logging in debug mode feat: turn on gosnmp logging in debug mode Nov 19, 2025
@jajeffries jajeffries marked this pull request as ready for review November 19, 2025 15:11
@jajeffries jajeffries merged commit 3d5e3b3 into develop Nov 19, 2025
6 checks passed
@jajeffries jajeffries deleted the feat/OBS-1623-debug-logging branch November 19, 2025 15:29
@github-actions
Copy link

🎉 This PR is included in version 1.10.1 🎉

The release is available on GitHub release

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants