Observability - Installation
Esta página aún no está disponible en tu idioma.
The einar-cli allows you to automatically set up the observability trace, metric and log providers for your application, ensuring that all necessary files and imports are properly configured to manage both tracing and logging throughout your entire application.
einar install observabilityThis command will generate the following files and directories within your project, setting up the necessary infrastructure for tracing and logging operations:
/app /shared /infrastructure /observability - observability.go - trace_provider.go - meter_provider.go - log_provider.go👨💻 Tutorial: Traces and Logs Correlation
In this tutorial, we will generate a new onload-job, inject the observability instance, create a new span, and correlate traces with logs.
1.- Inside your project directory, run the following command to create a new custom onload job:
einar generate onload-job my-job-nameHere’s an example of how the generated code will look:
func init() { ioc.Registry(myJobName)}func myJobName() { //execute your onload job here}2.- Inject the tracer and logger into your onload job, and perform a correlated trace-logging operation.
func init() { ioc.Registry( myJobName, observability.NewObservability)}
func myJobName(obs observability.Observability) { spanCtx, span := obs.Tracer.Start(context.Background(), "my-onload-job-parent-trace") defer span.End() fmt.Println("PARENT OUTPUT :") obs.Logger.InfoContext(spanCtx, "Hello from log + trace structured logs :D", "customKey", "customValue")
fmt.Println("CORRELATED CHILDREN OUTPUT :") executeCorrelatedLogOperation(spanCtx, obs)}
func executeCorrelatedLogOperation(ctx context.Context, obs observability.Observability) { spanCtx, span := obs.Tracer.Start(ctx, "my-onload-job-child-trace") defer span.End() obs.Logger.InfoContext(spanCtx, "Log and trace correlation successful.", "traceIdMatch", "The traceId for this span is the same as the parent.")}When executed, the log will appear in the console output as follows:
PARENT OUTPUT : { "time": "2024-09-16T12:15:31.8786429-03:00", "level": "INFO", "msg": "Hello from log + trace structured logs :D", "customKey": "customValue", "trace_id": "d25d1f2fddbe684cd4ebb9a8b863e00f", "span_id": "63fe0f14eb035d76"}CORRELATED CHILDREN OUTPUT : { "time": "2024-09-16T12:15:31.8798036-03:00", "level": "INFO", "msg": "Log and trace correlation successful.", "traceIdMatch": "The traceId for this span is the same as the parent.", "trace_id": "d25d1f2fddbe684cd4ebb9a8b863e00f", "span_id": "85791c46cb2c8854"}