Giter Club home page Giter Club logo

peevee's Introduction

PeeVee - Go 1.18+ only (requires Go generics)

Goreportcard Build Status Coveralls Go Reference

PEEVEE lets you peek into what is happening in real time through Golang channels. It can automatically generate Prometheus metrics about the throughput of channels. Think of it like Unix's "pv" (https://linux.die.net/man/1/pv) but in Golang.

Examples

Using PeeVee to create both reader and writer channels

package main

import (
	"fmt"
	"time"
	"log"

	"github.com/migueleliasweb/peevee"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

func runMetricsEndpoint() {
	http.Handle("/metrics", promhttp.HandlerFor(
		prometheus.DefaultGatherer,
		promhttp.HandlerOpts{},
	))
	log.Fatal(http.ListenAndServe(":8080", nil))
}

func main() {
	pv := peevee.New("myWorkChannel", WithPromMetrics[int]())

	// mimic an asynchronous channel writer
	go func() {
		for {
			pv.GetWritableChan() <- rand.Int()
			time.Sleep(time.Millisecond * 250)
		}
	}()

	// mimic an asynchronous channel reader
	go func() {
		for {
			fmt.Printnl("int:", <-pv.GetReadableChan())
		}
	}()

	runMetricsEndpoint()
	
	// Leave the example running and open "localhost:8080/metrics" on your browser.
	// You will see a metric called "peevee" that is generated in real time
	// when the channel is being read/written.
}

Using PeeVee to wrap an existing channel for reading

package main

import (
	"fmt"
	"time"
	"log"

	"github.com/migueleliasweb/peevee"
	"github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/client_golang/prometheus/promhttp"
)

func runMetricsEndpoint() {
	http.Handle("/metrics", promhttp.HandlerFor(
		prometheus.DefaultGatherer,
		promhttp.HandlerOpts{},
	))
	log.Fatal(http.ListenAndServe(":8080", nil))
}

func main() {
	// this is a channel that gets returned
	// from an SDK or a third party lib, for example
	channelYouDontControl := make(chan bool)

	go func() {
		for {
			// this would be something happening inside the SDK or lib
			channelYouDontControl <- true
			time.Sleep(time.Millisecond * 250)
		}
	}()

	pv := peevee.NewReaderWrap(
		"boolwrap",
		channelYouDontControl, // this time you can pass the existing channel
	)

	// mimic an asynchronous channel reader
	go func() {
		for {
			fmt.Printnl("got:", <-pv.GetReadableChan())
		}
	}()

	runMetricsEndpoint()
	
	// Leave the example running and open "localhost:8080/metrics" on your browser.
	// You will see a metric called "peevee" that is generated in real time
	// when the channel is being read/written.
}

peevee's People

Contributors

migueleliasweb avatar

Watchers

 avatar

peevee's Issues

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.