jbactad / krakend-newrelic-v2

A NewRelic middleware that uses the NewRelic Go Agent v3

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

krakend-newrelic-v2

Go Reference Go codecov Go Report Card

A NewRelic middleware that uses the NewRelic Go Agent v3

Installation

go get -u github.com/jbactad/krakend-newrelic-v2

Quick Start

To enable NewRelic instrumentation in your krakend api gateway, you need to make sure and call metrics.Register function to initialize the newrelic.Application from your gateway.

There are 3 middlewares where you can enable instrumentation in your krakend api gateway, Handler, Proxy and Backend.

package main

import (
	"context"
	"net/http"

	"github.com/gin-gonic/gin"
	metrics "github.com/jbactad/krakend-newrelic-v2"
	"github.com/luraproject/lura/v2/config"
	"github.com/luraproject/lura/v2/logging"
	"github.com/luraproject/lura/v2/proxy"
	router "github.com/luraproject/lura/v2/router/gin"
	serverhttp "github.com/luraproject/lura/v2/transport/http/server"
	server "github.com/luraproject/lura/v2/transport/http/server/plugin"
)

func main() {
	cfg := config.ServiceConfig{}
	logger := logging.NoOp

	// Initializes the metrics collector.
	metrics.Register(context.Background(), cfg.ExtraConfig, logger)

	backendFactory := proxy.HTTPProxyFactory(&http.Client{})

	// Wrap the default BackendFactory with instrumented BackendFactory 
	backendFactory = metrics.BackendFactory("backend", backendFactory)

	pf := proxy.NewDefaultFactory(backendFactory, logger)

	// Wrap the default ProxyFactory with instrumented ProxyFactory
	pf = metrics.ProxyFactory("proxy", pf)

	handlerFactory := router.CustomErrorEndpointHandler(logger, serverhttp.DefaultToHTTPError)

	// Wrap the default HandlerFactory with instrumented HandlerFactory
	handlerFactory = metrics.HandlerFactory(handlerFactory)

	engine := gin.New()

	// setup the krakend router
	routerFactory := router.NewFactory(
		router.Config{
			Engine:         engine,
			ProxyFactory:   pf,
			Logger:         logger,
			RunServer:      router.RunServerFunc(server.New(logger, serverhttp.RunServer)),
			HandlerFactory: handlerFactory,
		},
	)

	// start the engines
	logger.Info("Starting the KrakenD instance")
	routerFactory.NewWithContext(context.Background()).Run(cfg)
}

Then in your gateway's config file make sure to add github_com/jbactad/krakend_newrelic_v2 in the service extra_config section.

{
  "version": 3.0,
  "extra_config": {
    "github_com/jbactad/krakend_newrelic_v2": {
      "rate": 100
    }
  }
}

Configuring

NewRelic related configurations are all read from environment variables. Refer to newrelic go agent package to know more of which NewRelic options can be configure.

From krakend configuration file, these are the following options you can configure.

Name Type Description
rate int The rate the middlewares instrument your application.

Development

Requirements

To start development, make sure you have the following dependencies installed in your development environment.

  • golang >=v1.17

Setup

Run the following to install the necessary tools to run tests.

make setup

Generate mocks for unit tests

make gen

Running unit tests

To run the unit tests, execute the following command.

make test-unit

About

A NewRelic middleware that uses the NewRelic Go Agent v3

License:Apache License 2.0


Languages

Language:Go 99.3%Language:Makefile 0.7%