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

Add OpenMetrics support #538

Merged
merged 1 commit into from
Aug 11, 2020
Merged

Conversation

grobie
Copy link
Contributor

@grobie grobie commented Aug 6, 2020

Instrument Yopass' HTTP handler and record served HTTP requests by
method, path and reponse code, and request duration by method and path.

Metrics are optionally served in the OpenMetrics text format on a
separate HTTP server under /metrics. The metrics server can be enabled
by setting the --metrics-port flag. For now listen IP address and TLS
settings are used for both HTTP server instances.

Resolves #536. Depends on #537.

Example /metrics output:
# 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"} 0
go_gc_duration_seconds{quantile="0.25"} 0
go_gc_duration_seconds{quantile="0.5"} 0
go_gc_duration_seconds{quantile="0.75"} 0
go_gc_duration_seconds{quantile="1"} 0
go_gc_duration_seconds_sum 0
go_gc_duration_seconds_count 0
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 7
# HELP go_info Information about the Go environment.
# TYPE go_info gauge
go_info{version="go1.14.6"} 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.214048e+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 2.214048e+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.444929e+06
# HELP go_memstats_frees_total Total number of frees.
# TYPE go_memstats_frees_total counter
go_memstats_frees_total 1350
# 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
# 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 3.436808e+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.214048e+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.3340544e+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.211264e+06
# HELP go_memstats_heap_objects Number of allocated objects.
# TYPE go_memstats_heap_objects gauge
go_memstats_heap_objects 9672
# 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.3340544e+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.6551808e+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 0
# 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 11022
# 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 13888
# 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 63784
# 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 65536
# 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.473924e+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.278903e+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 557056
# 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 557056
# HELP go_memstats_sys_bytes Number of bytes obtained from system.
# TYPE go_memstats_sys_bytes gauge
go_memstats_sys_bytes 7.3351424e+07
# HELP go_threads Number of OS threads created.
# TYPE go_threads gauge
go_threads 10
# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.
# TYPE process_cpu_seconds_total counter
process_cpu_seconds_total 0.05
# HELP process_max_fds Maximum number of open file descriptors.
# TYPE process_max_fds gauge
process_max_fds 8192
# HELP process_open_fds Number of open file descriptors.
# TYPE process_open_fds gauge
process_open_fds 79
# HELP process_resident_memory_bytes Resident memory size in bytes.
# TYPE process_resident_memory_bytes gauge
process_resident_memory_bytes 1.3897728e+07
# HELP process_start_time_seconds Start time of the process since unix epoch in seconds.
# TYPE process_start_time_seconds gauge
process_start_time_seconds 1.59674120894e+09
# HELP process_virtual_memory_bytes Virtual memory size in bytes.
# TYPE process_virtual_memory_bytes gauge
process_virtual_memory_bytes 1.340944384e+09
# HELP process_virtual_memory_max_bytes Maximum amount of virtual memory available in bytes.
# TYPE process_virtual_memory_max_bytes gauge
process_virtual_memory_max_bytes -1
# HELP yopass_request_duration_seconds Histogram of HTTP request latencies by method and path.
# TYPE yopass_request_duration_seconds histogram
yopass_request_duration_seconds_bucket{method="GET",path="/",le="0.005"} 1
yopass_request_duration_seconds_bucket{method="GET",path="/",le="0.01"} 1
yopass_request_duration_seconds_bucket{method="GET",path="/",le="0.025"} 1
yopass_request_duration_seconds_bucket{method="GET",path="/",le="0.05"} 1
yopass_request_duration_seconds_bucket{method="GET",path="/",le="0.1"} 1
yopass_request_duration_seconds_bucket{method="GET",path="/",le="0.25"} 1
yopass_request_duration_seconds_bucket{method="GET",path="/",le="0.5"} 1
yopass_request_duration_seconds_bucket{method="GET",path="/",le="1"} 1
yopass_request_duration_seconds_bucket{method="GET",path="/",le="2.5"} 1
yopass_request_duration_seconds_bucket{method="GET",path="/",le="5"} 1
yopass_request_duration_seconds_bucket{method="GET",path="/",le="10"} 1
yopass_request_duration_seconds_bucket{method="GET",path="/",le="+Inf"} 1
yopass_request_duration_seconds_sum{method="GET",path="/"} 0.000121674
yopass_request_duration_seconds_count{method="GET",path="/"} 1
yopass_request_duration_seconds_bucket{method="GET",path="/metrics",le="0.005"} 5
yopass_request_duration_seconds_bucket{method="GET",path="/metrics",le="0.01"} 5
yopass_request_duration_seconds_bucket{method="GET",path="/metrics",le="0.025"} 5
yopass_request_duration_seconds_bucket{method="GET",path="/metrics",le="0.05"} 5
yopass_request_duration_seconds_bucket{method="GET",path="/metrics",le="0.1"} 5
yopass_request_duration_seconds_bucket{method="GET",path="/metrics",le="0.25"} 5
yopass_request_duration_seconds_bucket{method="GET",path="/metrics",le="0.5"} 5
yopass_request_duration_seconds_bucket{method="GET",path="/metrics",le="1"} 5
yopass_request_duration_seconds_bucket{method="GET",path="/metrics",le="2.5"} 5
yopass_request_duration_seconds_bucket{method="GET",path="/metrics",le="5"} 5
yopass_request_duration_seconds_bucket{method="GET",path="/metrics",le="10"} 5
yopass_request_duration_seconds_bucket{method="GET",path="/metrics",le="+Inf"} 5
yopass_request_duration_seconds_sum{method="GET",path="/metrics"} 0.012709560000000002
yopass_request_duration_seconds_count{method="GET",path="/metrics"} 5
yopass_request_duration_seconds_bucket{method="GET",path="/secret/:key",le="0.005"} 11
yopass_request_duration_seconds_bucket{method="GET",path="/secret/:key",le="0.01"} 11
yopass_request_duration_seconds_bucket{method="GET",path="/secret/:key",le="0.025"} 11
yopass_request_duration_seconds_bucket{method="GET",path="/secret/:key",le="0.05"} 11
yopass_request_duration_seconds_bucket{method="GET",path="/secret/:key",le="0.1"} 11
yopass_request_duration_seconds_bucket{method="GET",path="/secret/:key",le="0.25"} 11
yopass_request_duration_seconds_bucket{method="GET",path="/secret/:key",le="0.5"} 11
yopass_request_duration_seconds_bucket{method="GET",path="/secret/:key",le="1"} 11
yopass_request_duration_seconds_bucket{method="GET",path="/secret/:key",le="2.5"} 11
yopass_request_duration_seconds_bucket{method="GET",path="/secret/:key",le="5"} 11
yopass_request_duration_seconds_bucket{method="GET",path="/secret/:key",le="10"} 11
yopass_request_duration_seconds_bucket{method="GET",path="/secret/:key",le="+Inf"} 11
yopass_request_duration_seconds_sum{method="GET",path="/secret/:key"} 0.007584642
yopass_request_duration_seconds_count{method="GET",path="/secret/:key"} 11
yopass_request_duration_seconds_bucket{method="POST",path="/secret",le="0.005"} 2
yopass_request_duration_seconds_bucket{method="POST",path="/secret",le="0.01"} 2
yopass_request_duration_seconds_bucket{method="POST",path="/secret",le="0.025"} 2
yopass_request_duration_seconds_bucket{method="POST",path="/secret",le="0.05"} 2
yopass_request_duration_seconds_bucket{method="POST",path="/secret",le="0.1"} 2
yopass_request_duration_seconds_bucket{method="POST",path="/secret",le="0.25"} 2
yopass_request_duration_seconds_bucket{method="POST",path="/secret",le="0.5"} 2
yopass_request_duration_seconds_bucket{method="POST",path="/secret",le="1"} 2
yopass_request_duration_seconds_bucket{method="POST",path="/secret",le="2.5"} 2
yopass_request_duration_seconds_bucket{method="POST",path="/secret",le="5"} 2
yopass_request_duration_seconds_bucket{method="POST",path="/secret",le="10"} 2
yopass_request_duration_seconds_bucket{method="POST",path="/secret",le="+Inf"} 2
yopass_request_duration_seconds_sum{method="POST",path="/secret"} 0.002809964
yopass_request_duration_seconds_count{method="POST",path="/secret"} 2
# HELP yopass_requests_total Total number of requests served by HTTP method, path and response code.
# TYPE yopass_requests_total counter
yopass_requests_total{code="200",method="GET",path="/metrics"} 5
yopass_requests_total{code="200",method="GET",path="/secret/:key"} 10
yopass_requests_total{code="200",method="POST",path="/secret"} 1
yopass_requests_total{code="400",method="POST",path="/secret"} 1
yopass_requests_total{code="404",method="GET",path="/"} 1
yopass_requests_total{code="404",method="GET",path="/secret/:key"} 1

@codecov
Copy link

codecov bot commented Aug 6, 2020

Codecov Report

Merging #538 into master will increase coverage by 12.44%.
The diff coverage is 97.50%.

Impacted file tree graph

@@             Coverage Diff             @@
##           master     #538       +/-   ##
===========================================
+ Coverage   74.50%   86.95%   +12.44%     
===========================================
  Files           4        4               
  Lines         102      138       +36     
===========================================
+ Hits           76      120       +44     
+ Misses         18       10        -8     
  Partials        8        8               
Impacted Files Coverage Δ
pkg/yopass/yopass.go 95.29% <97.50%> (+19.78%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 87e316d...aa9f8e5. Read the comment docs.

@grobie grobie force-pushed the add-openmetrics-support branch 3 times, most recently from f415755 to 3e31165 Compare August 7, 2020 09:31
@grobie
Copy link
Contributor Author

grobie commented Aug 7, 2020

Hej @jhaals. This is ready for review, let me know what you'd like to see changed / where you'd prefer a different style or such. Happy to make any changes.

@jhaals
Copy link
Owner

jhaals commented Aug 7, 2020

Thanks a lot for the PR @grobie, I will happily accept these changes. Would you also be able to add some notes about metrics in the readme?

@grobie grobie force-pushed the add-openmetrics-support branch 2 times, most recently from bc1198d to b7415f0 Compare August 7, 2020 14:14
@grobie
Copy link
Contributor Author

grobie commented Aug 7, 2020

@jhaals
Copy link
Owner

jhaals commented Aug 7, 2020

Fantastic 👍

@@ -18,6 +20,7 @@ var (
maxLength = pflag.Int("max-length", 10000, "max length of encrypted secret")
memcached = pflag.String("memcached", "localhost:11211", "Memcached address")
port = pflag.Int("port", 1337, "listen port")
_ = pflag.Int("metrics-port", -1, "metrics server listen port")
Copy link
Owner

Choose a reason for hiding this comment

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

is this supposed to be underscore?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hehe. So I realized that all these global variables are unused, as you use viper to access the flag values. I'll change it for consistency.

Copy link
Contributor Author

@grobie grobie Aug 10, 2020

Choose a reason for hiding this comment

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

Done. It might make sense to remove all these variables and only leave the pflag function invocations to make it clear that the global variables aren't used.

Copy link
Owner

Choose a reason for hiding this comment

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

@grobie I did not know that the variables where unused, it must have been a leftover from the time when I used flag. It make sense to clean them up in a separate PR. I'm going to merge this one. Thanks a lot for adding metrics to yopass ❤️

Instrument Yopass' HTTP handler and record served HTTP requests by
method, path and reponse code, and request duration by method and path.

Metrics are optionally served in the [OpenMetrics][] text format on a
separate HTTP server under /metrics. The metrics server can be enabled
by setting the --metrics-port flag. For now listen IP address and TLS
settings are used for both HTTP server instances.

[OpenMetrics]: https://openmetrics.io/

Signed-off-by: Tobias Schmidt <[email protected]>
@jhaals jhaals merged commit 7e04bb0 into jhaals:master Aug 11, 2020
@grobie grobie deleted the add-openmetrics-support branch August 18, 2020 10:21
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.

Prometheus support
2 participants