Code Monkey home page Code Monkey logo

responselogger's Introduction

responselogger

Server middleware to log HTTP server response status codes and their times.

Usage

package main

import (
	"fmt"
	"net/http"

	"github.com/welldigital/responselogger"
)

func main() {
	// Create a mux to store routes.
	mux := http.NewServeMux()

	mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello world!"))
	})

	mux.HandleFunc("/other", func(w http.ResponseWriter, r *http.Request) {
		http.NotFound(w, r)
	})

	// Wrap the mux inside the responselogger.
	loggedHandler := responselogger.NewHandler(mux)

	// Start serving.
	fmt.Println("Listening on :1234...")
	http.ListenAndServe(":1234", loggedHandler)
	fmt.Println("Exited")
}

Output

Example output from JSON logging

{"time":"2018-02-01T18:41:31Z","src":"rl","status":404,"http_4xx":1,"len":19,"ms":2,"path":"/other"}
{"time":"2018-02-01T18:41:39Z","src":"rl","status":200,"http_2xx":1,"len":12,"ms":4,"path":"/"}

Configuring AWS CloudWatch metrics extraction with Terraform

The JSON logs can be converted into CloudWatch metrics for monitoring with the following Terraform configuration.

resource "aws_cloudwatch_log_metric_filter" "http_2xx" {
  name = "HTTP 2xx status"
  pattern = "{ ($.src = \"rl\") && ($.http_2xx = *) }"
  log_group_name = "logs"

  metric_transformation {
    name = "http_2xx"
    namespace = "HTTPMetrics"
    value = "$.http_2xx"
  }
}

resource "aws_cloudwatch_log_metric_filter" "http_3xx" {
  name = "HTTP 3xx status"
  pattern = "{ ($.src = \"rl\") && ($.http_3xx = *) }"
  log_group_name = "logs"

  metric_transformation {
    name = "http_3xx"
    namespace = "HTTPMetrics"
    value = "$.http_3xx"
  }
}

resource "aws_cloudwatch_log_metric_filter" "http_4xx" {
  name = "HTTP 4xx status"
  pattern = "{ ($.src = \"rl\") && ($.http_4xx = *) }"
  log_group_name = "logs"

  metric_transformation {
    name = "http_4xx"
    namespace = "HTTPMetrics"
    value = "$.http_4xx"
  }
}

resource "aws_cloudwatch_log_metric_filter" "http_5xx" {
  name = "HTTP 5xx status"
  pattern = "{ ($.src = \"rl\") && ($.http_5xx = *) }"
  log_group_name = "logs"

  metric_transformation {
    name = "http_5xx"
    namespace = "HTTPMetrics"
    value = "$.http_5xx"
  }
}

resource "aws_cloudwatch_log_metric_filter" "http_duration_ms" {
  name = "HTTP duration ms"
  pattern = "{ ($.src = \"rl\") && ($.ms = *) }"
  log_group_name = "logs"

  metric_transformation {
    name = "http_duration_ms"
    namespace = "HTTPMetrics"
    value = "$.ms"
  }
}

responselogger's People

Contributors

a-h avatar krasimihaylov avatar

Watchers

 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.