Skip to content

plentymarkets/mc-telemetry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

mc-telemetry: A plentymarkets package for go telemetry

Purpose

By integrating the mc-telemetry package we implement a more sophisticated framework. This enhances the existing logging system and improves the quality and depth of logged data.

The mc-telemetry package is designed to support logging into various third-party systems, including New Relic APM, New Relic ZeroLog, and Nope. Through the local driver the package also allows for straightforward console logging.

The mc-telemetry package includes interfaces for telemetry drivers, allowing users to use abstract interfaces rather than concrete implementations.

For more details about available drivers, please refer to: mc-telemetry-driver.

Getting Started

To effectively use the mc-telemetry package, you first need to do the following:

  1. Install the appropriate package by using the Go installer via the command line interface.

    This ensures that all required components of the mc-telemetry package are correctly set up in your system.

  2. Configure the config.yaml file and environment binder to unlock the full functionality of the mc-telemetry package.

    NOTE: These configurations are crucial to unlock the full functionality of the mc-telemetry package as they define necessary operational parameters and environment settings.

Update below structure as follows:

my-app/
./
├── cmd/
│   ├── config.yaml
│   └── main.go
└── pkg/
    └── config.go

Installing the mc-telemetry package

Run:

go get github.com/plentymarkets/mc-telemetry

Importing the packages and configuring the drivers

1. In main.go import the new packages, like so:

package main

import (
    //...
	// Import the packages as follows.
	_ "github.com/plentymarkets/mc-telemetry-driver/pkg/teldrvr"
	"github.com/plentymarkets/mc-telemetry/pkg/telemetry" 
    //...
)

func main() { 
    //...
	// Configure the drivers 
	telemetry.SetDriver(strings.Split(cfg.GetString("telemetry.driver"), ",")...)
	telemetry.SetTraceDriver(cfg.GetString("telemetry.traceDriver"))
    // ...
}

2. To configure the packages in the config.yaml file, do the following:

Note: This is required only for development.

# cmd/config.yaml
telemetry:
  driver: "local"       # The driver used for logging
  traceDriver: "local"  # The driver used for tracing between microservice
  app: "name-of-the-app"# Name of the microservice
  logLevel: "debug"     # Levels: error / info / debug
  newrelic:
    licenseKey: ""

3. In the default configuration file, bind the environment variables:

// GetConfig returns the configuration
func GetConfig(path string) (Config, error) {
	// telemetry
	viper.BindEnv("telemetry.driver", "TELEMETRY_DRIVER")
	viper.BindEnv("telemetry.traceDriver", "TELEMETRY_TRACE_DRIVER")
}

Dynamic Configuration

For example, you can define the ExampleConfig as follows:

yaml env description default
telemetry.driver TELEMETRY_DRIVER Thee trace driver used for logging:
Available drivers:
  • local - Run locally and log only to console
  • newrelicAPM - Run locally and log only to console
  • nrZerolog - Run locally and log only to console
  • nopDriver - Run locally and log only to console
Note: You can use one or more drivers at once
telemetry.traceeDriver TELEMETRY_TRACE_DRIVER Creates a traceID through which you can follow all the flow between microservices Available drivers:
  • local - Run locally and log only to console
  • newrelicAPM - Run locally and log only to console
  • nrZerolog - Run locally and log only to console
  • nopDriver - Run locally and log only to console
Note: You can use one or more drivers at once
telemetry.app TELEMETRY_APP The name of the microservice
telemetry.logLevel TELEMETRY_LOGLEVEL There are three types of log levels based on their priority
  • error - Highest priority, logs only the errors
  • info - Medium priority, logs both errors and info messages
  • debug - Low priority, logs all levels plus extra info for debugging purposes
error
telemetry.newrelic.licenseKey NEW_RELIC_LICENSE_KEY The licence key from newrelic

How to use telemetry

// Create the transaction object
transaction, err := telemetry.Start("Transaction Message")

if err != nil {
    log.Printf(err.Error())
}

defer transaction.Done() // Close transaction

// Start the Segment
segmentID := transaction.SegmentStart("Start handle user token")
defer transaction.SegmentEnd(segmentID) // Close segment

// Log message based on priority
transaction.Debug(segmentID, &msg)  // Requires segmentID string, msg *string
transaction.Info(segmentID, &msg)   // Requires segmentID string, msg *string
transaction.Error(segmentID, &err)  // Requires segmentID string, msg *error

Dependencies