hnygingonic

package
v1.18.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2024 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package hnygingonic has Middleware to use with the gin-gonic muxer.

Summary

hnygingonic has Middleware for use in the gin.Use function call wrapping all requests that come into the gin muxer

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(queryParams map[string]struct{}) gin.HandlerFunc

Middleware wraps httprouter handlers. Since it wraps handlers with explicit parameters, it can add those values to the event it generates.

Example
package main

import (
	"context"
	"log"
	"net/http"

	"github.com/gin-gonic/gin"
	"github.com/honeycombio/beeline-go"
)

func main() {
	// Setup a new gin Router, not using the Default here so that we can put the
	// Beeline middleware in before the middleware provided by Gin
	router := gin.New()
	router.Use(
		// Add the beeline middleware to the chain
		Middleware(nil),
		// Doing something like the following would have the Middleware grab specifc
		// GET query params that you deal with in your gin application.
		//Middleware(map[string]struct{}{
		//"parts":  {},
		//"limit":  {},
		//"offset": {},
		//})
		// The Logger and Recovery middleware which are setup in the Default gin router
		gin.Logger(),
		gin.Recovery(),
		// Our example middleware that does extra work
		exampleMiddleware(),
	)

	// Setup the routes we want to use
	router.GET("/", home)
	router.GET("/alive", alive)
	router.GET("/ready", ready)

	// Start the server
	log.Fatal(router.Run("127.0.0.1:8080"))
}

func home(c *gin.Context) {
	hnyctx, span := StartSpan(c, "main.home")
	defer span.Send()
	span.AddField("Welcome", "Home")
	childFunction(hnyctx)
	c.Data(http.StatusOK, "text/plain", []byte(`Welcome Home`))
}

func alive(c *gin.Context) {
	c.Data(http.StatusOK, "text/plain", []byte(`OK`))
}

func ready(c *gin.Context) {
	_, span := StartSpan(c, "main.ready")
	defer span.Send()
	// Do some work here
	span.AddField("Ready", true)
	c.Data(http.StatusOK, "text/plain", []byte(`OK`))
}

func exampleMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		hnyctx, span := StartSpan(c, "main.exampleMiddleware")
		defer span.Send()
		SetContext(c, hnyctx)
		// Do some work
		c.Next()
		childFunction(hnyctx)
	}
}

func childFunction(ctx context.Context) {
	_, span := beeline.StartSpan(ctx, "main.childFunction")
	defer span.Send()
	// Do some work here
}
Output:

func SetContext

func SetContext(c *gin.Context, newMiddleWareContext context.Context)

SetContext should be used to replace the context.Context in the gin.Context in the case of having multiple custom middleware in the codebase

func StartSpan

func StartSpan(c *gin.Context, name string) (context.Context, *trace.Span)

StartSpan is a helper function to start a new span in a gin-gonic context This is required because the gin-gonic handler function expects to receive *gin.Context rather than context.Context

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL