GithubHelp home page GithubHelp logo

vladfr / fiber-servertiming Goto Github PK

View Code? Open in Web Editor NEW
2.0 1.0 0.0 13 KB

Fiber middleware for server-timing (https://w3c.github.io/server-timing/)

License: MIT License

Go 97.30% Makefile 2.70%

fiber-servertiming's Introduction

Go Report Card

Fiber Server Timing

This is a Fiber middleware for the [W3C Server-Timing API](see https://w3c.github.io/server-timing/) based on mitchellh/go-server-timing

Getting started

The Server-Timing API adds HTTP Headers for server-side performance metrics, such as time to render, DB calls latency, etc. You can view this data in the browser, and it can be collected by analytics providers via JavaScript.

This middleware adds a route timer by default, which should measure all the time spent in a request. You can use it to add your own custom timers via Fiber's user context.

Usage

Import the middleware package that is part of the Fiber web framework

import (
  "github.com/gofiber/fiber/v2"
  "github.com/vladfr/fiber-servertiming/v2"
)

After you initiate your Fiber app, you can attach the middleware like this:

app := fiber.New()

# add the middleware; you should add it as the first middleware
# so you'll include the other middlewares in the final timing
app.Use(fiberservertiming.New())

To add metrics to this header, use Context:

app.Get("/server", func(c *fiber.Ctx) error {
  timing := fiberservertiming.FromContext(c)

  # time-consuming tasks

  # you should defer the .Stop() call so it triggers at the very end of this function
  defer timing.NewMetric("backendcall").WithDesc("My Description").Start().Stop()
}

Metrics naming

In the Server-Timing spec, metric names cannot have any special characters, so use just letters and numbers. Also, since this is a http header, values for name and description should be kept as concise as possible.

Security

Timing information is sent by the server in the form of a header. You should add additional protection if you don't want this data exposed in production, e.g. only enable this middleware on-demand or in dev or stage environments.

On the client-side, you can control which origins have access to the information via Javascript by setting the Timing-Allow-Origin header. This works exactly like a CORS header. See /examples for a full server/client example. This allows you to control which 3rd party scripts can access your timing data.

app := fiber.New()
app.Use(fiberservertiming.New(fiberservertiming.Config{
  AllowOrigins: "http://127.0.0.1:3000, http://127.0.0.1:3000/",
}))

fiber-servertiming's People

Contributors

vladfr avatar

Stargazers

 avatar  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.