Giter Site home page Giter Site logo

tkliner / stats Goto Github PK

View Code? Open in Web Editor NEW

This project forked from thoas/stats

0.0 2.0 0.0 3.46 MB

A Go middleware that stores various information about your web application (response time, status code count, etc.) compatible with fasthttp and fasthttprouter

License: MIT License

Makefile 0.40% Go 99.60%

stats's Introduction

Go stats handler

Build Status

stats is a valyala/fasthttp handler in golang reporting various metrics about your web application.

This middleware has been developed and required for the need of picfit, an image resizing server written in Go.

Compatibility

This handler supports the following frameworks at the moment:

We don't support your favorite Go framework? Send me a PR or create a new issue and I will implement it :)

Installation

  1. Make sure you have a Go language compiler >= 1.3 (required) and git installed.
  2. Make sure you have the following go system dependencies in your $PATH: bzr, svn, hg, git
  3. Ensure your GOPATH is properly set.
  4. Download it:
go get github.com/tkliner/stats

Usage

Fasthttp and Fasthttprouter

If you are using fasthttp with buaazp/fasthttprouter you need to call the middleware with the handler itself:

package main

import (
        "encoding/json"
        "github.com/buaazp/fasthttprouter"
            "github.com/valyala/fasthttp"
        "github.com/tkliner/stats"
)

func main() {
    router := fasthttprouter.New()
        s := stats.New()
        router.GET("/stats", func(ctx *fasthttp.RequestCtx) {
                    ctx.Response.Header.Set("Content-Type", "application-json")
                    s, err := json.Marshal(s.Data())
                    if err != nil {
                            log.Fatal("Stats error")
                    }
                    ctx.Write(s)
                    ctx.SetStatusCode(200)
        })
        log.Fatal(fasthttp.ListenAndServe("localhost:8000", s.Handler(router.Handler)))
}

Run it in a shell:

$ go run server.go

Then in another shell run:

$ curl http://localhost:3000/stats | python -m "json.tool"

Expect the following result:

{
    "total_response_time": "1.907382ms",
    "average_response_time": "86.699\u00b5s",
    "average_response_time_sec": 8.6699e-05,
    "count": 1,
    "pid": 99894,
    "status_code_count": {
        "200": 1
    },
    "time": "2015-03-06 17:23:27.000677896 +0100 CET",
    "total_count": 22,
    "total_response_time_sec": 0.0019073820000000002,
    "total_status_code_count": {
        "200": 22
    },
    "unixtime": 1425659007,
    "uptime": "4m14.502271612s",
    "uptime_sec": 254.502271612
}

See examples to test them.

Inspiration

Antoine Imbert is the original author of this middleware.

Originally developed for go-json-rest, it had been ported as a simple Golang handler by Florent Messa to be used in various frameworks.

This middleware implements a ticker which is launched every seconds to reset requests/sec and will implement new features in a near future :)

Original package

This is fork of the original package thoas/stats, which was created as part of modification to be used with fasthttp and fasthttprouter

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.