metricsum

package module
v0.0.0-...-297f327 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2021 License: GPL-3.0 Imports: 8 Imported by: 0

README

Go Reference Build Status Go Report Card

Introduction

metricsum is a Golang webserver module built with fasthttp and fasthttp/router that provides a simple API to track a statistic over the past hour. It is thread safe and can handle concurrent requests into the server without issue.

Installation

go get -u github.com/ddrake12/metricsum

Running metricsum

Navigate to the metricsum/cmd folder and run any of the following commands:

  • go run main.go to run it in the current terminal.
  • go build main.go to create a binary in the current directory.
  • go install main.go to create a binary at the appropriate bin folder. See this reference for details.

Using metricsum

Metric sum supports two endpoints, one to to track a value for a given key/statistic using POST /metric/key e.g.:

POST http://localhost:8080/metric/mykey { "value": 3}

And one that returns the sum of all values for that key posted within the last hour using GET /metric/key/sum

GET http://localhost:8080/metric/mykey/sum]

Note: metricsum uses an in memory data store and values will not persist through termination.

Fully tested

The metricsum code is fully tested with appropriate mocks for external dependencies as well as the time dependent code to delete values after an hour. All Pull Requests will run the tests and ensure they pass.

Enjoy!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KeysMap

type KeysMap struct {
	// contains filtered or unexported fields
}

KeysMap has methods Metric and Sum that can be used as a fasthttp.RequestHandler for routing with fasthttp/routing. All access to the internal keys map is mutex protected and thread safe.

func NewKeysMap

func NewKeysMap() *KeysMap

NewKeysMap initializes a new KeysMap

func (*KeysMap) Metric

func (km *KeysMap) Metric(ctx *fasthttp.RequestCtx)

Metric allows the saving of a metric value with the associated key e.g. POST /metric/{key} { "value" = 4 }

func (*KeysMap) Sum

func (km *KeysMap) Sum(ctx *fasthttp.RequestCtx)

Sum allows for the summation of all values for a given key using GET /metric/{key}/sum

type RequestCtx

type RequestCtx interface {
	UserValue(key string) interface{}
	PostBody() []byte
	Error(msg string, statusCode int)
	Time() time.Time
}

RequestCtx contains methods needed by fasthttp.RequestCtx to allow mocks for unit testing

type Value

type Value struct {
	Value float64 `json:"value"`
}

Value allows for easy unmarshalling of POST /metric/{key} body data

Directories

Path Synopsis
Package mocks is a generated GoMock package.
Package mocks is a generated GoMock package.

Jump to

Keyboard shortcuts

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