step9

command
v0.0.0-...-9e008f2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 21, 2021 License: MIT Imports: 7 Imported by: 0

README

Demo Flow - Step 9

prev |

Expose operational values

There is an option to expose operational values publicly.

Import the expvar package

import _ "expvar"

Restart the demo server and browse to http://localhost:8000/debug/vars.

Add custom exposed variable

Adding custom operational values is quite easy.

  1. Import the expvar package, not anonymously
  2. Define variables for the operational values
  3. Add the custom variables to the exposed values

In this demo we will monitor the following information:

  • Number of concurrent requests
  • Total number of requests
  • Total bytes read

Define the exposed variables:

var concurrentRequests expvar.Int
var totalRequests expvar.Int
var totalBytes expvar.Int

Publish a map variable with the metrics values:

metrics := expvar.NewMap("metrics")
metrics.Set("concurrentRequests", &concurrentRequests)
metrics.Set("totalRequests", &totalRequests)
metrics.Set("totalBytes", &totalBytes)

On every request, add to request metrics counters:

totalRequests.Add(1)
concurrentRequests.Add(1)
defer concurrentRequests.Add(-1)

Every time bytes are read - count the bytee

totalBytes.Add(int64(n))

(see modified source code in server.go)


Run the demo server again, run the benchmark and refresh the /debug/vars page - the custom operational information is available.


And we're done!

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL