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.
To effectively use the mc-telemetry package, you first need to do the following:
-
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.
-
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
Run:
go get github.com/plentymarkets/mc-telemetry
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")
}
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:
|
|
telemetry.traceeDriver |
TELEMETRY_TRACE_DRIVER |
Creates a traceID through which you can follow all the flow between microservices
Available drivers:
|
|
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 |
telemetry.newrelic.licenseKey |
NEW_RELIC_LICENSE_KEY |
The licence key from newrelic |
// 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
- go version >= 1.21
- mc-telemetry-driver