Skip to content

Builtin checks expose context logger with string key - #430

Merged
sni merged 2 commits into
mainfrom
builtin-checks-expose-context-logger-with-string-key
Jul 28, 2026
Merged

Builtin checks expose context logger with string key#430
sni merged 2 commits into
mainfrom
builtin-checks-expose-context-logger-with-string-key

Conversation

@inqrphl

@inqrphl inqrphl commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

logger context: expose snclient logger instance with a second key of string type

loggerCtxKey is a struct{} type used to pack logger inside. this can be accessed if snclient package is imported

loggerCtxKey2 is a new string type key with value "github.com/consol-monitoring/snclient/pkg/utils.Logger" , specific to the snclient logger.

adding objects to the context with string keys is not recommended in general, but if the pkg/snclient is not imported in other package (likely due to circular dependency problem) , they can use the string value to get the logger instance. the string is very specific to snclient already.

this is better than writing to the output buffer and contaminating it. output buffer should only be used to communicate check results

this is intended to be used with check_http, which has its own package

tested and works, even though the filename is wrong as it uses the filename in the check_http_go package

but need to merge check_http_go PR first:

sni/check_http_go#5

./snclient -vvv --verbose --logfile stdout run check_http --hostname google.com --certificate=1000,500
...
[16:27:05.896][T][check:117] extracting logger from context using snclient logger specific key result: true
[16:27:05.936][T][check:117] certificate check: cert '*.google.com' from 'WR2' expires in 55 days (critical)
[16:27:05.936][T][check:117] certificate check: cert 'WR2' from 'GTS Root R1' expires in 938 days (warning)
[16:27:05.936][T][check:117] certificate check: cert 'GTS Root R1' from 'GlobalSign Root CA' expires in 549 days (warning)
[16:27:05.936][T][check:117] subcheck 0 | code: 2 | importance: 151 | msg: HTTP CRITICAL - x509 certificate '*.google.com' from 'WR2' is valid until Mon 21 Sep 2026 08:37:24 AM UTC +0000 (expires in 55 days)
[16:27:05.936][T][check:117] subcheck 1 | code: 1 | importance: 251 | msg: HTTP WARNING - x509 certificate 'WR2' from 'GTS Root R1' is valid until Tue 20 Feb 2029 02:00:00 PM UTC +0000 (expires in 938 days)
[16:27:05.936][T][check:117] subcheck 2 | code: 1 | importance: 351 | msg: HTTP WARNING - x509 certificate 'GTS Root R1' from 'GlobalSign Root CA' is valid until Fri 28 Jan 2028 12:00:42 AM UTC +0000 (expires in 549 days)
[16:27:05.936][T][check:117] subcheck 3 | code: 0 | importance: 152 | msg: HTTP OK - x509 certificate '*.google.com' from 'WR2' has its validity start time in the past (valid from Mon 29 Jun 2026 08:37:25 AM UTC +0000)
[16:27:05.936][T][check:117] subcheck 4 | code: 0 | importance: 153 | msg: HTTP OK - x509 certificate '*.google.com' from 'WR2' uses strong signature algorithm SHA256-RSA
[16:27:05.936][T][check:117] subcheck 5 | code: 0 | importance: 252 | msg: HTTP OK - x509 certificate 'WR2' from 'GTS Root R1' has its validity start time in the past (valid from Wed 13 Dec 2023 09:00:00 AM UTC +0000)
[16:27:05.936][T][check:117] subcheck 6 | code: 0 | importance: 253 | msg: HTTP OK - x509 certificate 'WR2' from 'GTS Root R1' uses strong signature algorithm SHA256-RSA
[16:27:05.936][T][check:117] subcheck 7 | code: 0 | importance: 352 | msg: HTTP OK - x509 certificate 'GTS Root R1' from 'GlobalSign Root CA' has its validity start time in the past (valid from Fri 19 Jun 2020 12:00:42 AM UTC +0000)
[16:27:05.936][T][check:117] subcheck 8 | code: 0 | importance: 353 | msg: HTTP OK - x509 certificate 'GTS Root R1' from 'GlobalSign Root CA' uses strong signature algorithm SHA256-RSA

Ahmet Oeztuerk added 2 commits July 27, 2026 16:29
…string type

loggerCtxKey is a struct{} type used to pack logger inside. this can be accessed if snclient package is imported

loggerCtxKey2 is a new string type key with value "github.com/consol-monitoring/snclient/pkg/utils.Logger" , specific to the snclient logger.

adding objects to the context with string keys is not recommended in general, but if the pkg/snclient is not imported in other package (likely due to circular dependency problem) , they can use the string value to get the logger instance. the string is very specific to snclient already.

this is better than writing to the output buffer and contaminating it. output buffer should only be used to communicate check results

this is intended to be used with check_http, which has its own package

tested and works, even though the filename is wrong as it uses the filename in the check_http_go package

```
./snclient -vvv --verbose --logfile stdout run check_http --hostname google.com --certificate=1000,500
...
[16:27:05.896][T][check:117] extracting logger from context using snclient logger specific key result: true
[16:27:05.936][T][check:117] certificate check: cert '*.google.com' from 'WR2' expires in 55 days (critical)
[16:27:05.936][T][check:117] certificate check: cert 'WR2' from 'GTS Root R1' expires in 938 days (warning)
[16:27:05.936][T][check:117] certificate check: cert 'GTS Root R1' from 'GlobalSign Root CA' expires in 549 days (warning)
[16:27:05.936][T][check:117] subcheck 0 | code: 2 | importance: 151 | msg: HTTP CRITICAL - x509 certificate '*.google.com' from 'WR2' is valid until Mon 21 Sep 2026 08:37:24 AM UTC +0000 (expires in 55 days)
[16:27:05.936][T][check:117] subcheck 1 | code: 1 | importance: 251 | msg: HTTP WARNING - x509 certificate 'WR2' from 'GTS Root R1' is valid until Tue 20 Feb 2029 02:00:00 PM UTC +0000 (expires in 938 days)
[16:27:05.936][T][check:117] subcheck 2 | code: 1 | importance: 351 | msg: HTTP WARNING - x509 certificate 'GTS Root R1' from 'GlobalSign Root CA' is valid until Fri 28 Jan 2028 12:00:42 AM UTC +0000 (expires in 549 days)
[16:27:05.936][T][check:117] subcheck 3 | code: 0 | importance: 152 | msg: HTTP OK - x509 certificate '*.google.com' from 'WR2' has its validity start time in the past (valid from Mon 29 Jun 2026 08:37:25 AM UTC +0000)
[16:27:05.936][T][check:117] subcheck 4 | code: 0 | importance: 153 | msg: HTTP OK - x509 certificate '*.google.com' from 'WR2' uses strong signature algorithm SHA256-RSA
[16:27:05.936][T][check:117] subcheck 5 | code: 0 | importance: 252 | msg: HTTP OK - x509 certificate 'WR2' from 'GTS Root R1' has its validity start time in the past (valid from Wed 13 Dec 2023 09:00:00 AM UTC +0000)
[16:27:05.936][T][check:117] subcheck 6 | code: 0 | importance: 253 | msg: HTTP OK - x509 certificate 'WR2' from 'GTS Root R1' uses strong signature algorithm SHA256-RSA
[16:27:05.936][T][check:117] subcheck 7 | code: 0 | importance: 352 | msg: HTTP OK - x509 certificate 'GTS Root R1' from 'GlobalSign Root CA' has its validity start time in the past (valid from Fri 19 Jun 2020 12:00:42 AM UTC +0000)
[16:27:05.936][T][check:117] subcheck 8 | code: 0 | importance: 353 | msg: HTTP OK - x509 certificate 'GTS Root R1' from 'GlobalSign Root CA' uses strong signature algorithm SHA256-RSA
```
the passed logger now prints these messages instead
@sni
sni merged commit f469ad5 into main Jul 28, 2026
149 checks passed
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.

2 participants