mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-28 13:48:49 +08:00

* Add open tracing dependencies Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add open tracing to users service Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add open tracing to the things service Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add open tracing to the http adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add open tracing to the ws adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add open tracing to the CoAP adapter Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update LoRa adapter in accordance with changes Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update SDK tests in accordance with changes Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update bootstrap service in accordance with changes Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update reader services with accordance with changes Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update .env and docker-compose file Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add jaeger and timeout env vars Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Fix broken test for can access by id endpoint Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update deps with proto empty package Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com>
66 lines
1.6 KiB
Go
66 lines
1.6 KiB
Go
//
|
|
// Copyright (c) 2019
|
|
// Mainflux
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
|
|
// Package tracing contains middlewares that will add spans
|
|
// to existing traces.
|
|
package tracing
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/mainflux/mainflux/users"
|
|
opentracing "github.com/opentracing/opentracing-go"
|
|
)
|
|
|
|
const (
|
|
saveOp = "save_op"
|
|
retrieveByIDOp = "retrieve_by_id"
|
|
)
|
|
|
|
var _ users.UserRepository = (*userRepositoryMiddleware)(nil)
|
|
|
|
type userRepositoryMiddleware struct {
|
|
tracer opentracing.Tracer
|
|
repo users.UserRepository
|
|
}
|
|
|
|
// UserRepositoryMiddleware tracks request and their latency, and adds spans
|
|
// to context.
|
|
func UserRepositoryMiddleware(repo users.UserRepository, tracer opentracing.Tracer) users.UserRepository {
|
|
return userRepositoryMiddleware{
|
|
tracer: tracer,
|
|
repo: repo,
|
|
}
|
|
}
|
|
|
|
func (urm userRepositoryMiddleware) Save(ctx context.Context, user users.User) error {
|
|
span := createSpan(ctx, urm.tracer, saveOp)
|
|
defer span.Finish()
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
return urm.repo.Save(ctx, user)
|
|
}
|
|
|
|
func (urm userRepositoryMiddleware) RetrieveByID(ctx context.Context, id string) (users.User, error) {
|
|
span := createSpan(ctx, urm.tracer, retrieveByIDOp)
|
|
defer span.Finish()
|
|
ctx = opentracing.ContextWithSpan(ctx, span)
|
|
|
|
return urm.repo.RetrieveByID(ctx, id)
|
|
}
|
|
|
|
func createSpan(ctx context.Context, tracer opentracing.Tracer, opName string) opentracing.Span {
|
|
if parentSpan := opentracing.SpanFromContext(ctx); parentSpan != nil {
|
|
return tracer.StartSpan(
|
|
opName,
|
|
opentracing.ChildOf(parentSpan.Context()),
|
|
)
|
|
}
|
|
|
|
return tracer.StartSpan(opName)
|
|
}
|