apmiris

package module
v0.0.0-...-b863e37 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2023 License: MPL-2.0 Imports: 8 Imported by: 0

README

APM IRIS

This package is for middleware kataras iris, so you can add this to your application and logging to APM.

Reference : issue

How to use

    go get -u github.com/fari-99/apmiris
Setup ENV

first you need too setup basic environment variable, for more advance setup please refer to apm configuration.

export ELASTIC_APM_SERVICE_NAME=your-app-name
export ELASTIC_APM_SERVER_URL=http://your.apm.server:port
export ELASTIC_APM_SECRET_TOKEN=apm-token-app
Code
package main

import (
    "fmt"
	"github.com/fari-99/apmiris"
	"github.com/kataras/iris/v12"
)

func main() {
    app := iris.New()
    
    // setup middleware + user data so your logs have user data
    // in this example my application using redis session, but you can change it
    app.Use(apmiris.Middleware(app, func(ctx iris.Context) (userData *apmiris.GetUserData) {
        sessionRedis := GetRedisSessionConnection()
        s := sessionRedis.Start(ctx)
        
        userModel := s.GetString("auth")
        var dataUser map[string]interface{}
        _ = json.Unmarshal([]byte(userModel), &dataUser)
        
        user := &apmiris.UserData{
            UserID:    fmt.Sprintf("%v", dataUser["id"]),
            UserName:  fmt.Sprintf("%v", dataUser["username"]),
            UserEmail: fmt.Sprintf("%v", dataUser["email"]),
        }
        
        return user
    }))
        
    app.Get("/test", func(ctx iris.Context) {
        ctx.StatusCode(iris.StatusOK)
        _, _ = ctx.JSON(iris.Map{
            "message": "You've been mixing with the wrong crowd.",
        })
        return
    })
        
    _ = app.Listen(":8080")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Middleware

func Middleware(engine *iris.Application, getUserData GetUserData, opts ...Option) iris.Handler

Middleware returns a new Kataras/Iris middleware handler for tracing requests and reporting errors.

This middleware will recover and report panics, so it can be used instead of the standard gin.Recovery middleware.

By default, the middleware will use apm.DefaultTracer. Use WithTracer to specify an alternative tracer.

Types

type GetUserData

type GetUserData func(ctx iris.Context) (userData *UserData)

type Option

type Option func(*middleware)

Option sets options for tracing.

func WithTracer

func WithTracer(t *apm.Tracer) Option

WithTracer returns an Option which sets t as the tracer to use for tracing server requests.

type UserData

type UserData struct {
	UserID    string
	UserEmail string
	UserName  string
}

Jump to

Keyboard shortcuts

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