lmzaphook

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

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

Go to latest
Published: Jul 14, 2022 License: MPL-2.0 Imports: 6 Imported by: 0

README

lm-zap-hook

codecov build_and_test Go Reference Go Report Card

lm-zap-hook is a zapcore.Core implementation to integrate with the Logicmonitor Platform. It sends the log messages generated by the application, to the Logicmonitor platform.

Installation

go get -u github.com/logicmonitor/lm-zap-hook

Quick Start

Authentication:

Set the LM_ACCESS_ID and LM_ACCESS_KEY for using the LMv1 authentication. The company name or account name must be set to LM_ACCOUNT property. All properties can be set using environment variable.

Environment variable Description
LM_ACCOUNT Account name (Company Name) is your organization name
LM_ACCESS_ID Access id while using LMv1 authentication.
LM_ACCESS_KEY Access key while using LMv1 authentication.
Getting Started

Here's an example code snippet for configuring the lm-zap-hook with the application code.

package main

import (
	"context"
	"time"

	lmzaphook "github.com/logicmonitor/lm-zap-hook"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	// create a new Zap logger
	logger, _ := zap.NewProduction()
	defer logger.Sync() // flushes buffer, if any

	// create resource tags for mapping the log messages to a unique LogicMonitor resource
	resourceTags := map[string]string{"system.displayname": "test-device"}

	// create a new core that sends zapcore.WarnLevel and above messages to Logicmonitor Platform
	lmCore, err := lmzaphook.NewLMCore(context.Background(),
		lmzaphook.Params{ResourceMapperTags: resourceTags},
		lmzaphook.WithLogLevel(zapcore.WarnLevel),
	)
	if err != nil {
		logger.Fatal(err.Error())
	}

	// Wrap a NewTee to send log messages to both your main logger and to Logicmonitor
	logger = logger.WithOptions(zap.WrapCore(func(core zapcore.Core) zapcore.Core {
		return zapcore.NewTee(core, lmCore)
	}))

	// This info message will only go to the main logger
	logger.Info("Test log message for main logger", zap.String("foo", "bar"))

	// This warning message will go to both the main logger and to Logicmonitor.
	logger.Warn("Warning message with fields", zap.String("foo", "bar"))

	// By default, log send operations happens async way, so blocking the execution
	time.Sleep(15 * time.Second)
}

Options

Following are the options that can be passed to NewLMCore() to configure the lmCore.

Option Description
WithLogLevel(logLevel zapcore.Level) Configures lmCore to send the logs having level equal or above the level specified by logLevel. Default logLevel is Warning.
WithClientBatchingInterval(batchInterval time.Duration) Configures interval for batching of the log messages.
WithClientBatchingDisabled() Disables the batching of log messages. By default, batching is enabled.
WithMetadata(metadata map[string]string) Metadata to be sent with the every log message.
WithNopLogIngesterClient() Configures lmCore to use the nopLogIngesterClient which discards the log messages. It can be used for testing.
WithBlocking() It makes the call to the send log operation blocking. Default value of Async Mode is true.

Copyright, 2022, LogicMonitor, Inc.

This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLMCore

func NewLMCore(ctx context.Context, params Params, opts ...Option) (*lmCore, error)

NewCore creates a zap core that sends out the logs using logNotifer

Types

type AuthProvider

type AuthProvider interface {
	GetCredentials(method string, uri string, body []byte) string
}

AuthProvider is used by the lmotel collector for providing auth token from derived in collector

type LogIngesterClient

type LogIngesterClient interface {
	SendLogs(ctx context.Context, logMessage string, resourceidMap, metadata map[string]string) error
}

LogIngesterClient represents interface for Log Ingest client

type LogIngesterSetting

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

LogIngesterSetting holds the properties required for configuring the LMLogIngest instance

type LogNotifier

type LogNotifier struct {
	LogIngesterClient
	// contains filtered or unexported fields
}

LogNotifier holds the log ingester config

func (*LogNotifier) Notify

func (logNotifier *LogNotifier) Notify(ctx context.Context, data []byte, metadata map[string]string) error

type Option

type Option func(*lmCore) error

func WithAuthProvider

func WithAuthProvider(authProvider AuthProvider) Option

WithAuthProvider is used by the lmotel collector for passing the auth provider

func WithBlocking

func WithBlocking() Option

WithBlocking blocks for the calls to the send log operation

func WithClientBatchingDisabled

func WithClientBatchingDisabled() Option

WithClientBatchingDisabled disables the batching of logs

func WithClientBatchingInterval

func WithClientBatchingInterval(batchingInterval time.Duration) Option

WithClientBatchingInterval configures the batching interval

func WithLogLevel

func WithLogLevel(level zapcore.Level) Option

WithLogLevel configures the log level such that only log messages with given level or above that level will be sent to the Logicmonitor platform

func WithMetadata

func WithMetadata(metadataTags map[string]string) Option

WithMetadata configures the metadata tags for the log messages

func WithNopLogIngesterClient

func WithNopLogIngesterClient() Option

WithNopLogIngesterClient will use no-op log ingester which will not send any logs. Can be used for testing

type Params

type Params struct {
	// Resource tags for mapping the log messages to a unique LogicMonitor resource
	ResourceMapperTags map[string]string
}

Params holds the required configurations for the hook

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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