Giter Club home page Giter Club logo

mc4go's Introduction

mCounters for golang

Counters for midi- and microservices.

Motivation and goals

It's a common problem to collect and expose metrics from your midi- and microservice applications. This library tryes to help you whith it.

The library is aimed to be:

  • as fast as possible. It uses memory mapped files and direct memory access to store the counters.
  • service implementation agnostic. For example, you can expose JMX metrics from your Java application and read them from a Golang sidecar application or vise versa (see https://github.com/anatolygudkov/mc4j).
  • usable to expose static information about the application as well as dynamic counters.
  • a 0-dependency project. Just copy-paste the sources into your project.

Usage

The library isn't tested for Windows yet

How to write counters

//Here is some statics we'd like to expose
statics := make(map[string]string)
statics["my.prop.1"] = "value 1"
statics["my.prop.2"] = "value 2"

w, err := NewWriterForName("mycounters.dat", statics, 100)
if err != nil {
	return err
}
defer w.Close()

//First counter
myCounter1, err := w.AddCounter("my.cnt.1")
if err != nil {
	return err
}
myCounter1.Set(100)
myCounter1.Close() //Free the memory allocated for the counter

//Second counter
myCounter2, err := w.AddCounter("my.cnt.2")
if err != nil {
	return err
}
myCounter2.Increment()
 

How to read counters

r, err := mc4go.NewReaderForName("mycounters.dat")
if err != nil {
	return err
}
defer r.Close()

fmt.Printf("version: %d\n", r.Version())
fmt.Printf("pid: %d\n", r.Pid())
fmt.Printf("started: %d\n", r.StartTime())

r.ForEachStatic(func(label, value string) bool {
	fmt.Printf("static: %s=%s\n", label, value)
	return true
})

r.ForEachCounter(func(id, value int64, label string) bool {
	fmt.Printf("counter: %s[%d]=%d\n", label, id, value)
	return true
}) 

There are two reades included into the library:

Concurrency issues

  • Counters are thread safe and one counter can be modified in different goroutines.
  • After a counter is closed, it must be not used, since its memory slot can be occupied by a new counter and the value of that new counter will be modified unexpectedtly.
  • Counters must not be accessed after the writer is closed, because such modification leads to a segmentation fault.

License

The code is available under the terms of the MIT License.

mc4go's People

Contributors

anatolygudkov avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

patrixgdd

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.