telemetry

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

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

Go to latest
Published: Nov 14, 2022 License: Apache-2.0 Imports: 18 Imported by: 0

README

KrakenD Elastic Common Schema Logger - Access and Application Logs

The module enables getting GIN router logs in JSON format.

Setting Up

Clone the KrakenD-CE repository and update the following changes

  1. In the router_engine.go file:

import (
        "encoding/json"
 
        "github.com/gin-gonic/gin"
+       "go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin"
 
        botdetector "github.com/krakendio/krakend-botdetector/v2/gin"
        httpsecure "github.com/krakendio/krakend-httpsecure/v2/gin"

@@ import (
        opencensus "github.com/krakendio/krakend-opencensus/v2/router/gin"
        "github.com/luraproject/lura/v2/config"
        "github.com/luraproject/lura/v2/core"
+       "github.com/luraproject/lura/v2/proxy"
        luragin "github.com/luraproject/lura/v2/router/gin"
        "github.com/luraproject/lura/v2/transport/http/server"
+
+       optiva_telemetry "github.com/darren-bell-optiva/optiva-product-shared-krakend-telemetry"
 )
 
 // NewEngine creates a new gin engine with some default values and a secure middleware
 @@ func NewEngine(cfg config.ServiceConfig, opt luragin.EngineOptions) *gin.Engine {
        engine := luragin.NewEngine(cfg, opt)
 
+       engine.Use(otelgin.Middleware("krakend"))
+
+       engine.Use(optiva_telemetry.NewGinLogger(cfg.ExtraConfig, gin.LoggerConfig{}))
+
+       jsonWithResponseStatusCodeRender := func(c *gin.Context, response *proxy.Response) {
+               if response == nil {
+                       c.JSON(500, gin.H{})
+                       return
+               }
+               status := response.Metadata.StatusCode
+               c.JSON(status, response.Data)
+       }
+
+       // register the render at the router level
+       luragin.RegisterRender("JsonWithStatusCodeRender", jsonWithResponseStatusCodeRender)
  1. executor.go
@@ import (
 
        "github.com/gin-gonic/gin"
        "github.com/go-contrib/uuid"
        "golang.org/x/sync/errgroup"
 
+       optiva_telemetry "github.com/darren-bell-optiva/optiva-product-shared-krakend-telemetry"
        krakendbf "github.com/krakendio/bloomfilter/v2/krakend"
        asyncamqp "github.com/krakendio/krakend-amqp/v2/async"


@@ type LoggerBuilder struct{}
 // NewLogger sets up the logging components as defined at the configuration.
 func (LoggerBuilder) NewLogger(cfg config.ServiceConfig) (logging.Logger, io.Writer, error) {
        var writers []io.Writer
+
+       telemetryConfig, _ := optiva_telemetry.ConfigGetter(cfg.ExtraConfig)
+       if telemetryConfig != nil {
+               logger, _ := optiva_telemetry.NewApplicationLogger(cfg.ExtraConfig)
+
+               return logger, nil, nil
+       }
+
        gelfWriter, gelfErr := gelf.NewWriter(cfg.ExtraConfig)
        if gelfErr == nil {
                writers = append(writers, gelfWriterWrapper{gelfWriter})

                
@@ func (MetricsAndTraces) Register(ctx context.Context, cfg config.ServiceConfig,
                l.Debug("[SERVICE: OpenCensus] Service correctly registered")
        }
 
+       if err := optiva_telemetry.RegisterOpenTelemetry(ctx, cfg, l); err != nil {
+               if err != optiva_telemetry.ErrNoConfig {
+                       l.Warning("[SERVICE: OpenTelemetry]", err.Error())
+               }
+       } else {
+               l.Debug("[SERVICE: OpenTelemetry] Service correctly registered")
+       }
+
        return metricCollector
 }

In KrakenD's configuration.json file, add the following to the service extra_config:

  "extra_config": {
    "github_com/darren-bell-optiva/optiva-product-shared-krakend-telemetry": {
        "logging": {
            "level": "INFO",
            "module": "[OPTIVA-TMF-APIGATEWAY]",
            "skip_paths": ["/__health"],
            "json": {
                "disable_html_escape": false,
                "pretty_print": false
            }
            
        },
        "tracing": {
            "exporter_url": "http://{{ env "HOST_IP" }}:14268/api/traces",
            "attributes": {
                "service": "krakend"
            } 
        }

    },
}
Available Config Options
Logging

level: The desired log level
skip_paths: List of endpoint paths which should not be logged. In the above example configuration, any request to /__health will not be logged.
json.disable_html_escape: Allows disabling html escaping in output. See https://pkg.go.dev/github.com/sirupsen/logrus#JSONFormatter
json.pretty_print: Will indent all json logs. See https://pkg.go.dev/github.com/sirupsen/logrus#JSONFormatter

Tracing

exporter_url Location to export traces in jaeger format
attributes - Additional attributes to apply to the trace

Metrics

TODO - Next steps

Use the default OTEL exported for the traces instead of the Jaeger exporter. e.g. https://docs.honeycomb.io/getting-data-in/opentelemetry/go-distro/

Documentation

Index

Constants

View Source
const (
	Namespace = "github_com/darren-bell-optiva/optiva-product-shared-krakend-telemetry"
)

Variables

View Source
var ErrNoConfig = errors.New("unable to load custom config")
View Source
var ErrWrongConfig = errors.New("getting the extra config for the krakend-logrus module")

ErrWrongConfig is the error returned when there is no config under the namespace

Functions

func ConfigGetter

func ConfigGetter(e config.ExtraConfig) (interface{}, error)

func NewGinLogger

func NewGinLogger(cfg config.ExtraConfig, loggerConfig gin.LoggerConfig) gin.HandlerFunc

func RegisterOpenTelemetry

func RegisterOpenTelemetry(ctx context.Context, cfg config.ServiceConfig, log logging.Logger) error

Types

type ElasticCommonSchemaFormatter

type ElasticCommonSchemaFormatter struct {
	DisableHTMLEscape bool   `json:"disable_html_escape"`
	DataKey           string `json:"data_key"`
	PrettyPrint       bool   `json:"pretty_print"`
}

type Logger

type Logger struct {
	Logger *logrus.Logger
	// contains filtered or unexported fields
}

Logger is a wrapper over a github.com/sirupsen/logrus logger

func NewApplicationLogger

func NewApplicationLogger(cfg config.ExtraConfig) (*Logger, error)

NewLogger returns a krakend logger wrapping a logrus logger

func (*Logger) Critical

func (l *Logger) Critical(v ...interface{})

Critical implements the logger interface but demotes to the error level

func (*Logger) Debug

func (l *Logger) Debug(v ...interface{})

Debug implements the logger interface

func (*Logger) Error

func (l *Logger) Error(v ...interface{})

Error implements the logger interface

func (*Logger) Fatal

func (l *Logger) Fatal(v ...interface{})

Fatal implements the logger interface

func (*Logger) Info

func (l *Logger) Info(v ...interface{})

Info implements the logger interface

func (*Logger) Warning

func (l *Logger) Warning(v ...interface{})

Warning implements the logger interface

type LoggingConfig

type LoggingConfig struct {
	SkipPaths    []string                      `json:"skip_paths`
	Level        string                        `json:"level"`
	Module       string                        `json:"module"`
	ECSFormatter *ElasticCommonSchemaFormatter `json:"json"`
}

type LogrusFormatter

type LogrusFormatter struct {
	// contains filtered or unexported fields
}

func (LogrusFormatter) AccessLogFormatter

func (lf LogrusFormatter) AccessLogFormatter(params gin.LogFormatterParams) string

type TelemetryConfig

type TelemetryConfig struct {
	Logging LoggingConfig `json:"logging`
	Tracing TracingConfig `json:"tracing`
}

type TracingConfig

type TracingConfig struct {
	ExportUrl  string                    `json:"exporter_url`
	Attributes TracingResourceAttributes `json:"attributes"`
}

type TracingResourceAttributes

type TracingResourceAttributes struct {
	// contains filtered or unexported fields
}

Jump to

Keyboard shortcuts

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