You can find a getting started guide on opentelemetry.io.
OpenTelemetry's goal is to provide a single set of APIs to capture distributed traces and metrics from your application and send them to an observability platform. This project allows you to do just that for applications written in Go. There are two steps to this process: instrument your application, and configure an exporter.
Go Version 1.16+
go get github.com/erajayatech/go-opentelemetry
- Set the following environment variables:
MODE=<your_application_mode>
- Example :
prod
- Example :
APP_VERSION=<your_application_version>
APP_NAME=<your_application_name>
OTEL_EXPORTER_OTLP_ENDPOINT=https://otlp.nr-data.net:4317
OTEL_EXPORTER_OTLP_HEADERS="api-key=<your_license_key>"
- Replace
<your_license_key>
with your Account License Key.
- Replace
OTEL_SAMPLED=true
- Be careful about using this sampler in a production application with significant traffic: a new trace will be started and exported for every request. If you won't sampling for every request just set to
false
- Be careful about using this sampler in a production application with significant traffic: a new trace will be started and exported for every request. If you won't sampling for every request just set to
- Import package on main.go
otel "github.com/erajayatech/go-opentelemetry"
- Add below code to
main.go
otelTracerService := otel.ConstructOtelTracer()
otelTracerServiceErr := otelTracerService.SetTraceProviderNewRelic(ctx)
if otelTracerServiceErr != nil {
panic(otelTracerServiceErr)
}
-
Tracing
Controller
- Import package
otel "github.com/erajayatech/go-opentelemetry"
- Start Span Tracer
ctx, span := otel.Start(context) defer span.End()
- Use code below to add span's tags
otel.AddSpanTags(span, map[string]string{"traceId": singleton.Trace().ID, "RegisterRequest": string(requestMarshalled)})
- Use code below to add span error
if err != nil { otel.AddSpanError(span, err) otel.FailSpan(span, "Request not valid") httpresponse.BadRequest(context, "Request not valid") return }
-
Tracing
Service
- Import package
otel "github.com/erajayatech/go-opentelemetry"
- Add context argument to function that we want to trace, example :
func (service *RegisterService) Register(context context.Context, request RegisterRequest, platform string) *httpresponse.HTTPError {}
- Use code below for add span
ctx, span := otel.NewSpan(context, "authregister.service.Register", "") defer span.End()
- Use code below to add span error
if registeredPlatforms.Id == 0 { httpError.Code = http.StatusBadRequest httpError.Message = "Platform not valid" otel.AddSpanError(span, valErr) otel.FailSpan(span, httpError.Message) return &httpError }
-
Tracing
Query
- Add code below on
database.go
- Import package
"gorm.io/gorm/logger" "gorm.io/plugin/opentelemetry/logging/logrus" "gorm.io/plugin/opentelemetry/tracing"
- Set
gorm logger
logger := logger.New( logrus.NewWriter(), logger.Config{ SlowThreshold: time.Millisecond, LogLevel: logger.Warn, Colorful: false, }, ) db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{Logger: logger}) if err != nil { log.Println("Connected to database Failed:", err) } if err := db.Use(tracing.NewPlugin()); err != nil { panic(err) }
- Add code below on
repository
- Import package
otel "github.com/erajayatech/go-opentelemetry"
- Add context argument to function that we want to trace, example :
func (repo *RegisterRepository) getCustomerByEmail(context context.Context, customer *model.Customer, email string) {}
- Use code below for add span
ctx, span := otel.NewSpan(context, "authregister.RegisterRepository.getCustomerByEmail", "") defer span.End()
- Use code below for trace a query
repo.db.WithContext(ctx).First(customer, "email = ?", strings.ToLower(email))
- Add code below on
-
Tracing
External Service
- Import package
otel "github.com/erajayatech/go-opentelemetry"
- Add context argument to function that we want to trace, example :
func (capillary *Capillary) sendToStagingCustomer(context context.Context, url string, payload interface{}, stagingResponse *StagingCustomerResponse) (*StagingCustomerResponse, error) {}
- Use code below for add span
httpSpanAttribute := otel.HttpSpanAttribute{} httpSpanAttribute.Method = request.Method httpSpanAttribute.Url = request.RequestURI httpSpanAttribute.IP = request.Host _, span := otel.NewHttpSpan(context, "capillary.sendToStagingCustomer", url, httpSpanAttribute) defer span.End()
- Use code below for add event span
otel.AddSpanEvents(span, "capillary.sendToStagingCustomer", map[string]string{"traceId": capillary.traceID, "StagCust Endpoint": url, "StagCust Request": string(payloadByte), "StagCustResponse": compactedBuffer.String(), "http.status_code": response.Status})