Git Product home page Git Product logo

aws-embedded-metrics-golang's Introduction

aws-embedded-metrics-golang

test golangci-lint

Go implementation of AWS CloudWatch Embedded Metric Format

It's aim is to simplify reporting metrics to CloudWatch:

  • using EMF avoids additional HTTP API calls to CloudWatch as metrics are logged in JSON format to stdout
  • no need for additional dependencies in your services (or mocks in tests) to report metrics from inside your code
  • built in support for default dimensions and properties for Lambda functions

Supports namespaces, setting dimensions and properties as well as different contexts (at least partially).

Installation

go get github.com/prozz/aws-embedded-metrics-golang

Usage

emf.New().Namespace("mtg").Metric("totalWins", 1500).Log()

emf.New().Dimension("colour", "red").
    MetricAs("gameLength", 2, emf.Seconds).Log()

emf.New().DimensionSet(
        emf.NewDimension("format", "edh"),
        emf.NewDimension("commander", "Muldrotha")).
    MetricAs("wins", 1499, emf.Count).Log()

You may also use the lib together with defer.

m := emf.New() // sets up whatever you fancy here
defer m.Log()

// any reporting metrics calls

Customizing the logger:

emf.New(
    emf.WithWriter(os.Stderr), // Log to stderr.
    emf.WithTimestamp(time.Now().Add(-time.Hour)), // Record past metrics.
    emf.WithoutDimensions(), // Do not include useful Lambda related dimensions.
    emf.WithLogGroup("my-logs") // Add specific log group.
)

Functions for reporting metrics:

func Metric(name string, value int)
func Metrics(m map[string]int)
func MetricAs(name string, value int, unit MetricUnit)
func MetricsAs(m map[string]int, unit MetricUnit)

func MetricFloat(name string, value float64)
func MetricsFloat(m map[string]float64)
func MetricFloatAs(name string, value float64, unit MetricUnit)
func MetricsFloatAs(m map[string]float64, unit MetricUnit)

Functions for setting up dimensions:

func Dimension(key, value string)
func DimensionSet(dimensions ...Dimension) // use `func NewDimension` for creating one

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests.

License

MIT

aws-embedded-metrics-golang's People

Contributors

daszwarc avatar dependabot[bot] avatar harrisonhjones avatar prozz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

aws-embedded-metrics-golang's Issues

Log additional information but not as dimensions

Hello.

How do I log additional information useful for debugging without adding it as a dimension in the metric itself. Im looking for something equivalent to set_property from aws-embedded-metrics-python

set_property(key: str, value: Any) -> MetricsLogger

Adds or updates the value for a given property on this context. This value is not submitted to CloudWatch Metrics but is searchable by CloudWatch Logs Insights. This is useful for contextual and potentially high-cardinality data that is not appropriate for CloudWatch Metrics dimensions.

Thanks

Allow clients to set the internal timestamp

First off: thanks for this great little library. It's saved me some time writing it myself.

I'm happy to send in a PR for this but wanted to make sure it would be accepted before sending.

Problem

I'd like to be able to set the timestamp when creating a new emf. It's currently initalized to time.Now().UnixNano() / int64(time.Millisecond). This complicates two different situations:

  1. When I want to emit logs for past/future events.
  2. When I want to test log entries against static values.

In the mean time I've been doing the extremely hacky:

rf := reflect.ValueOf(metrics).Elem().FieldByName("timestamp")
reflect.NewAt(rf.Type(), unsafe.Pointer(rf.UnsafeAddr())).Elem().Set(reflect.ValueOf(int64(0)))

to manually set the timestamp to 0.

Solution

The cleanest solution would be to do the following:

  1. Bump the MV to 2
  2. Update New to use optional functional options to allow users to customize the output writer and timestamp (and others in the future).
  3. Remove NewFor as it is no longer needed.

A less clean but not requiring a MV bump option would be to add a SetTimestamp method on the *Logger that sets the timestamp.

Thoughts?

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.