apmiris

package module
v0.0.0-...-826b4d7 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2023 License: MPL-2.0 Imports: 9 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/mataharibiz/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
Example Default Recover from PANIC

this example doesn't need context

package main

import (
    "fmt"
	"github.com/mataharibiz/apmiris"
	"github.com/kataras/iris/v12"
)

func main() {
    app := iris.New()

    app.Get("/test-panic", func(context iris.Context) {
        defer func() {
            apmiris.RecoverApmDefault("test panic")

            if err := recover(); err != nil {
                context.StatusCode(iris.StatusOK)
                _, _ = context.JSON(iris.Map{
                    "message": "You just recovered",
                })
            }
        }()

        panic("panic test")
        return
    })
        
    _ = app.Listen(":8080")
}
Example Default Send Error
package main

import (
    "fmt"
	"github.com/mataharibiz/apmiris"
	"github.com/kataras/iris/v12"
)

func main() {
    app := iris.New()

    app.Get("/test-error", func(ctx iris.Context) {
        apmiris.SendErrorApmDefault(fmt.Errorf("this is error"))

        ctx.StatusCode(iris.StatusNotFound)
        _, _ = ctx.JSON(iris.Map{
            "message": "Testing error",
        })
        return
    })
        
    _ = app.Listen(":8080")
}
Example Code Middleware
package main

import (
    "fmt"
	"github.com/mataharibiz/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")
}
Example Code Error
package main

import (
    "fmt"
	"github.com/mataharibiz/apmiris"
	"github.com/kataras/iris/v12"
)

func main() {
    app := iris.New()
    
    app.Get("/test", func(ctx iris.Context) {
        apmiris.NewApmError(nil, ctx).
            SetAction("test error", "controller.role"). 
            SetLevelError("lowest"). // you can use custom level error
            SetTitle("Error when get input json"). // title for your error
            SetAdditionalData("additional data").
            SendError(fmt.Errorf("this is the error"))

        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.

func NewApmError

func NewApmError(userData *UserData, ctx iris.Context, opts ...OptionError) *apmError

func RecoverApmDefault

func RecoverApmDefault(recoverType string)

func SendErrorApmDefault

func SendErrorApmDefault(err error)

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 OptionError

type OptionError func(*apmError)

Option sets options for tracing.

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