joe-elliott / cert-exporter

A Prometheus exporter that publishes cert expirations on disk and in Kubernetes secrets
Apache License 2.0
316 stars 87 forks source link

Support disabling metrics about the exporter itself through -prometheus-disable-exporter-metrics #104

Closed praymann closed 2 years ago

praymann commented 2 years ago

👋🏻 Figured I'd open this to see if you're interested.

node_exporter and other exporters using the Prometheus Golang client support disabling the metrics of the exporter itself via some form of --web.disable-exporter-metrics, and I noticed the lack of that behavior with this exporter.

Something of note and please let me know if you want it done differently is that currently the different packages use the init() function but in order to influence the behavior of Metrics before it registered new metrics and after knowing the value of the flag I needed to move to an Init() function and call it explicitly after the flags are parsed.

Running without the flag ``` # > go run main.go > curl localhost:8080/metrics # HELP cert_exporter_error_total Cert Exporter Errors # TYPE cert_exporter_error_total counter cert_exporter_error_total 0 # HELP go_gc_duration_seconds A summary of the pause duration of garbage collection cycles. # TYPE go_gc_duration_seconds summary go_gc_duration_seconds{quantile="0"} 4.9366e-05 go_gc_duration_seconds{quantile="0.25"} 4.9366e-05 go_gc_duration_seconds{quantile="0.5"} 4.9366e-05 go_gc_duration_seconds{quantile="0.75"} 4.9366e-05 go_gc_duration_seconds{quantile="1"} 4.9366e-05 go_gc_duration_seconds_sum 4.9366e-05 go_gc_duration_seconds_count 1 # HELP go_goroutines Number of goroutines that currently exist. # TYPE go_goroutines gauge go_goroutines 9 # HELP go_info Information about the Go environment. # TYPE go_info gauge go_info{version="go1.16.15"} 1 # HELP go_memstats_alloc_bytes Number of bytes allocated and still in use. # TYPE go_memstats_alloc_bytes gauge go_memstats_alloc_bytes 2.134776e+06 # HELP go_memstats_alloc_bytes_total Total number of bytes allocated, even if freed. # TYPE go_memstats_alloc_bytes_total counter go_memstats_alloc_bytes_total 3.223408e+06 # HELP go_memstats_buck_hash_sys_bytes Number of bytes used by the profiling bucket hash table. # TYPE go_memstats_buck_hash_sys_bytes gauge go_memstats_buck_hash_sys_bytes 1.444839e+06 # HELP go_memstats_frees_total Total number of frees. # TYPE go_memstats_frees_total counter go_memstats_frees_total 6901 # HELP go_memstats_gc_cpu_fraction The fraction of this program's available CPU time used by the GC since the program started. # TYPE go_memstats_gc_cpu_fraction gauge go_memstats_gc_cpu_fraction 0.013472361006169897 # HELP go_memstats_gc_sys_bytes Number of bytes used for garbage collection system metadata. # TYPE go_memstats_gc_sys_bytes gauge go_memstats_gc_sys_bytes 4.317576e+06 # HELP go_memstats_heap_alloc_bytes Number of heap bytes allocated and still in use. # TYPE go_memstats_heap_alloc_bytes gauge go_memstats_heap_alloc_bytes 2.134776e+06 # HELP go_memstats_heap_idle_bytes Number of heap bytes waiting to be used. # TYPE go_memstats_heap_idle_bytes gauge go_memstats_heap_idle_bytes 6.2939136e+07 # HELP go_memstats_heap_inuse_bytes Number of heap bytes that are in use. # TYPE go_memstats_heap_inuse_bytes gauge go_memstats_heap_inuse_bytes 3.514368e+06 # HELP go_memstats_heap_objects Number of allocated objects. # TYPE go_memstats_heap_objects gauge go_memstats_heap_objects 8860 # HELP go_memstats_heap_released_bytes Number of heap bytes released to OS. # TYPE go_memstats_heap_released_bytes gauge go_memstats_heap_released_bytes 6.2603264e+07 # HELP go_memstats_heap_sys_bytes Number of heap bytes obtained from system. # TYPE go_memstats_heap_sys_bytes gauge go_memstats_heap_sys_bytes 6.6453504e+07 # HELP go_memstats_last_gc_time_seconds Number of seconds since 1970 of last garbage collection. # TYPE go_memstats_last_gc_time_seconds gauge go_memstats_last_gc_time_seconds 1.648230308181782e+09 # HELP go_memstats_lookups_total Total number of pointer lookups. # TYPE go_memstats_lookups_total counter go_memstats_lookups_total 0 # HELP go_memstats_mallocs_total Total number of mallocs. # TYPE go_memstats_mallocs_total counter go_memstats_mallocs_total 15761 # HELP go_memstats_mcache_inuse_bytes Number of bytes in use by mcache structures. # TYPE go_memstats_mcache_inuse_bytes gauge go_memstats_mcache_inuse_bytes 14400 # HELP go_memstats_mcache_sys_bytes Number of bytes used for mcache structures obtained from system. # TYPE go_memstats_mcache_sys_bytes gauge go_memstats_mcache_sys_bytes 16384 # HELP go_memstats_mspan_inuse_bytes Number of bytes in use by mspan structures. # TYPE go_memstats_mspan_inuse_bytes gauge go_memstats_mspan_inuse_bytes 125120 # HELP go_memstats_mspan_sys_bytes Number of bytes used for mspan structures obtained from system. # TYPE go_memstats_mspan_sys_bytes gauge go_memstats_mspan_sys_bytes 131072 # HELP go_memstats_next_gc_bytes Number of heap bytes when next garbage collection will take place. # TYPE go_memstats_next_gc_bytes gauge go_memstats_next_gc_bytes 4.194304e+06 # HELP go_memstats_other_sys_bytes Number of bytes used for other system allocations. # TYPE go_memstats_other_sys_bytes gauge go_memstats_other_sys_bytes 1.709721e+06 # HELP go_memstats_stack_inuse_bytes Number of bytes in use by the stack allocator. # TYPE go_memstats_stack_inuse_bytes gauge go_memstats_stack_inuse_bytes 655360 # HELP go_memstats_stack_sys_bytes Number of bytes obtained from system for stack allocator. # TYPE go_memstats_stack_sys_bytes gauge go_memstats_stack_sys_bytes 655360 # HELP go_memstats_sys_bytes Number of bytes obtained from system. # TYPE go_memstats_sys_bytes gauge go_memstats_sys_bytes 7.4728456e+07 # HELP go_threads Number of OS threads created. # TYPE go_threads gauge go_threads 14 # HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served. # TYPE promhttp_metric_handler_requests_in_flight gauge promhttp_metric_handler_requests_in_flight 1 # HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code. # TYPE promhttp_metric_handler_requests_total counter promhttp_metric_handler_requests_total{code="200"} 0 promhttp_metric_handler_requests_total{code="500"} 0 promhttp_metric_handler_requests_total{code="503"} 0 ```
Running with the flag ``` # go run main.go -prometheus-disable-exporter-metrics > curl localhost:8080/metrics # HELP cert_exporter_error_total Cert Exporter Errors # TYPE cert_exporter_error_total counter cert_exporter_error_total 0 ```
joe-elliott commented 2 years ago

@praymann I apologize for my absence. I've been caught up with work, but I finally got back to this. CI is fixed in master and it's been updated to test an actual, real version of cert-manager instead of the alpha versions.

If you rebase or merge then tests should pass and we can get this change in. Thank you for taking the time to put this together. I think it's a good feature and I'd love to get it into the codebase.

praymann commented 2 years ago

@praymann I apologize for my absence. I've been caught up with work, but I finally got back to this. CI is fixed in master and it's been updated to test an actual, real version of cert-manager instead of the alpha versions.

If you rebase or merge then tests should pass and we can get this change in. Thank you for taking the time to put this together. I think it's a good feature and I'd love to get it into the codebase.

No worries! I've merged mainline in so the tests pass, and added a comment about what I found around using Unregister().

I'd love to see it added to the codebase, let me know if there is anything else I can do!