Coder Social home page Coder Social logo

metrics's Introduction

Installation

go get -u github.com/andrew528i/[email protected]

Usage

package main

import (
	"math/rand"
	"sync"
	"time"

	"github.com/andrew528i/metrics"
	"github.com/go-redis/redis/v7"
)

// some heavy func, i.e. photo processing or smth else
func heavy() {
	dur := time.Millisecond * time.Duration(rand.Intn(1000))
	time.Sleep(dur)
}

func main() {
	// initialize redis client
	client := redis.NewClient(&redis.Options{
		Addr:     "127.0.0.1:6379",
		DB:       0,
		Password: "",
	})

	interval := 3 // in minutes
	m := metrics.NewMetrics(client, interval)

	// counter metric
	for i := 0; i < 10; i += 1 {
		m.Internal.Counter.Increase("my_value")
	}

	// average counter metric
	for i := 0; i < 10; i += 1 {
		v := int64(rand.Intn(100))
		m.Internal.AvgCounter.Add("my_avg_value", v)
	}

	// execution time metric
	var wg sync.WaitGroup
	for i := 0; i < 25; i += 1 {
		wg.Add(1)

		go func() {
			timer := m.Time.Start("heavy_avg_ts")
			heavy()
			timer.Stop()
			wg.Done()
		}()
	}

	wg.Wait()

	// run metrics http server
	srv := metrics.NewServer(m)
	srv.Run()
}

After running this code you can access metrics above with:

http://localhost:6767/internal/my_value
http://localhost:6767/internal/my_avg_value
http://localhost:6767/internal/heavy_avg_ts

Also there are some system metrics:

http://localhost:6767/system/cpu  // in percents
http://localhost:6767/system/ram  // in megabytes
http://localhost:6767/system/disk // disk usage in percents

There are 3 types of internal metrics for now:

Counter

You can count new users, orders, etc in last N minutes.

m.Internal.Counter.Increase("new_users")

http://localhost:6767/internal/new_users

Average Counter

Can be used for measuring average order sum in last N minutes

m.Internal.AvgCounter.Add("order_sum", order.sum)

http://localhost:6767/internal/order_sum

Average execution time

An average HTTP/GRPC/NATS request execution can be measured:

timer := m.Time.Start("process_photo_ts")
process_photo()
timer.Stop()

http://localhost:6767/internal/process_photo_ts

metrics's People

Contributors

afansv avatar andrew528i avatar

Stargazers

 avatar  avatar

Watchers

 avatar

Forkers

afansv massalex

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.