Git Product home page Git Product logo

metrix's Introduction

Metrix

A tiny library to simplify measuring the execution time of code blocks.

Overview

Metrix is a module you can use in your Elixir applications to simplify measuring and relaying the results to Telemetry.

When incorporated into your modules, you provide measure/2 the event name to send measurements to, and the block of code you want to measure. measure/2 then sends the following data to your Telemetry event handler:

  • measurement: This is a Map with :duration as the key. The value is in microseconds
  • metadata: The value returned from the block as a map in the form %{response: results}. Extra metadata can be provided to measure/3 as a Map. The resulting metadata will be a merging of what's provided and the response/results. Example: measure([:event, :name], %{conn: conn}), do: ...

Example

Here's a simple example of how you might use Metrix. Below, we wrap a call to System.sleep/1 and IO.puts/2 in the #measure/2 macro. We also provide the Telemetry event name against which we want to capture data. Whenever run/0 is executed it measures the elapsed time and reports the duration to any Telemetry event handlers set up to match on [:my_app, :event, :name].

defmodule MyApp do
  use Metrix

  def run do
    measure([:my_app, :event, :name]) do
      System.sleep(1_000)
      IO.puts "Hello, World!"
    end
  end
end

An example event handler might look like this:

def handle_event([:my_app, :event, :name], %{duration: duration}, metadata, _config) do
  IO.puts "#{duration}"
  IO.inspect metadata
end

Installation

If available in Hex, the package can be installed by adding metrix to your list of dependencies in mix.exs:

def deps do
  [
    {:metrix, "~> 0.1.0"}
  ]
end

Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/metrix.

Copyright and License

Metrix is copyright (c) 2020 Samuel Mullen.

Metrix source code is released under Apache License, Version 2.0.

See LICENSE and NOTICE files for more information.

metrix's People

Contributors

samullen avatar

Stargazers

Javier García avatar

Watchers

 avatar James Cloos avatar

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.