Mainflux.mainflux/twins/tracing/twins.go

148 lines
4.5 KiB
Go
Raw Normal View History

// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0
package tracing
import (
"context"
"github.com/mainflux/mainflux/twins"
opentracing "github.com/opentracing/opentracing-go"
)
const (
saveTwinOp = "save_twin"
MF-1180 - Add redis based twins and states cache (#1184) * Add twins redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add connectToRedis to twins main and twinCache to twins service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add tracing to twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twins cache mock and test setup for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TestTwinSave to redis twins cache tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add remove twin redis cache test Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add channels param to CreateDefinition helper method in mocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add IDs test to redis twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Simplify senml rec array and attribute creation funcs by removing unnecessary params Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Align cache remove twin method with service remove twin method Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache funcs to twins save, update and remove Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add def SaveIDs to redis cache and ref to service SaveStates Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TwinSaveIDs tests for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache related env vars desc to README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twinid bson key constant Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Update method to cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Integrate uuid unification related changes Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Use named arguments in interface method declarations Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add env vars to docker-compose file Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Make parameter names in interface methods and implementations consistent Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Wrap vars and consts in var and const blocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
2020-06-05 17:42:16 +08:00
saveTwinsOp = "save_twins"
updateTwinOp = "update_twin"
retrieveTwinByIDOp = "retrieve_twin_by_id"
retrieveAllTwinsOp = "retrieve_all_twins"
retrieveTwinsByAttributeOp = "retrieve_twins_by_attribute"
removeTwinOp = "remove_twin"
)
MF-1180 - Add redis based twins and states cache (#1184) * Add twins redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add connectToRedis to twins main and twinCache to twins service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add tracing to twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twins cache mock and test setup for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TestTwinSave to redis twins cache tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add remove twin redis cache test Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add channels param to CreateDefinition helper method in mocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add IDs test to redis twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Simplify senml rec array and attribute creation funcs by removing unnecessary params Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Align cache remove twin method with service remove twin method Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache funcs to twins save, update and remove Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add def SaveIDs to redis cache and ref to service SaveStates Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TwinSaveIDs tests for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache related env vars desc to README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twinid bson key constant Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Update method to cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Integrate uuid unification related changes Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Use named arguments in interface method declarations Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add env vars to docker-compose file Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Make parameter names in interface methods and implementations consistent Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Wrap vars and consts in var and const blocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
2020-06-05 17:42:16 +08:00
var _ twins.TwinRepository = (*twinRepositoryMiddleware)(nil)
type twinRepositoryMiddleware struct {
tracer opentracing.Tracer
repo twins.TwinRepository
}
MF-1180 - Add redis based twins and states cache (#1184) * Add twins redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add connectToRedis to twins main and twinCache to twins service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add tracing to twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twins cache mock and test setup for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TestTwinSave to redis twins cache tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add remove twin redis cache test Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add channels param to CreateDefinition helper method in mocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add IDs test to redis twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Simplify senml rec array and attribute creation funcs by removing unnecessary params Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Align cache remove twin method with service remove twin method Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache funcs to twins save, update and remove Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add def SaveIDs to redis cache and ref to service SaveStates Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TwinSaveIDs tests for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache related env vars desc to README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twinid bson key constant Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Update method to cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Integrate uuid unification related changes Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Use named arguments in interface method declarations Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add env vars to docker-compose file Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Make parameter names in interface methods and implementations consistent Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Wrap vars and consts in var and const blocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
2020-06-05 17:42:16 +08:00
// TwinRepositoryMiddleware tracks request and their latency, and adds spans to context.
func TwinRepositoryMiddleware(tracer opentracing.Tracer, repo twins.TwinRepository) twins.TwinRepository {
return twinRepositoryMiddleware{
tracer: tracer,
repo: repo,
}
}
func (trm twinRepositoryMiddleware) Save(ctx context.Context, tw twins.Twin) (string, error) {
span := createSpan(ctx, trm.tracer, saveTwinOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return trm.repo.Save(ctx, tw)
}
func (trm twinRepositoryMiddleware) Update(ctx context.Context, tw twins.Twin) error {
span := createSpan(ctx, trm.tracer, updateTwinOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return trm.repo.Update(ctx, tw)
}
MF-1180 - Add redis based twins and states cache (#1184) * Add twins redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add connectToRedis to twins main and twinCache to twins service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add tracing to twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twins cache mock and test setup for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TestTwinSave to redis twins cache tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add remove twin redis cache test Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add channels param to CreateDefinition helper method in mocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add IDs test to redis twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Simplify senml rec array and attribute creation funcs by removing unnecessary params Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Align cache remove twin method with service remove twin method Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache funcs to twins save, update and remove Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add def SaveIDs to redis cache and ref to service SaveStates Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TwinSaveIDs tests for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache related env vars desc to README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twinid bson key constant Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Update method to cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Integrate uuid unification related changes Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Use named arguments in interface method declarations Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add env vars to docker-compose file Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Make parameter names in interface methods and implementations consistent Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Wrap vars and consts in var and const blocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
2020-06-05 17:42:16 +08:00
func (trm twinRepositoryMiddleware) RetrieveByID(ctx context.Context, twinID string) (twins.Twin, error) {
span := createSpan(ctx, trm.tracer, retrieveTwinByIDOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
MF-1180 - Add redis based twins and states cache (#1184) * Add twins redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add connectToRedis to twins main and twinCache to twins service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add tracing to twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twins cache mock and test setup for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TestTwinSave to redis twins cache tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add remove twin redis cache test Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add channels param to CreateDefinition helper method in mocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add IDs test to redis twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Simplify senml rec array and attribute creation funcs by removing unnecessary params Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Align cache remove twin method with service remove twin method Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache funcs to twins save, update and remove Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add def SaveIDs to redis cache and ref to service SaveStates Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TwinSaveIDs tests for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache related env vars desc to README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twinid bson key constant Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Update method to cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Integrate uuid unification related changes Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Use named arguments in interface method declarations Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add env vars to docker-compose file Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Make parameter names in interface methods and implementations consistent Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Wrap vars and consts in var and const blocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
2020-06-05 17:42:16 +08:00
return trm.repo.RetrieveByID(ctx, twinID)
}
func (trm twinRepositoryMiddleware) RetrieveAll(ctx context.Context, owner string, offset, limit uint64, name string, metadata twins.Metadata) (twins.Page, error) {
span := createSpan(ctx, trm.tracer, retrieveAllTwinsOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return trm.repo.RetrieveAll(ctx, owner, offset, limit, name, metadata)
}
func (trm twinRepositoryMiddleware) RetrieveByAttribute(ctx context.Context, channel, subtopic string) ([]string, error) {
span := createSpan(ctx, trm.tracer, retrieveAllTwinsOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return trm.repo.RetrieveByAttribute(ctx, channel, subtopic)
}
MF-1180 - Add redis based twins and states cache (#1184) * Add twins redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add connectToRedis to twins main and twinCache to twins service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add tracing to twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twins cache mock and test setup for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TestTwinSave to redis twins cache tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add remove twin redis cache test Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add channels param to CreateDefinition helper method in mocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add IDs test to redis twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Simplify senml rec array and attribute creation funcs by removing unnecessary params Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Align cache remove twin method with service remove twin method Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache funcs to twins save, update and remove Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add def SaveIDs to redis cache and ref to service SaveStates Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TwinSaveIDs tests for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache related env vars desc to README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twinid bson key constant Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Update method to cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Integrate uuid unification related changes Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Use named arguments in interface method declarations Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add env vars to docker-compose file Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Make parameter names in interface methods and implementations consistent Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Wrap vars and consts in var and const blocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
2020-06-05 17:42:16 +08:00
func (trm twinRepositoryMiddleware) Remove(ctx context.Context, twinID string) error {
span := createSpan(ctx, trm.tracer, removeTwinOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
MF-1180 - Add redis based twins and states cache (#1184) * Add twins redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add connectToRedis to twins main and twinCache to twins service Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add tracing to twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twins cache mock and test setup for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TestTwinSave to redis twins cache tests Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add remove twin redis cache test Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add channels param to CreateDefinition helper method in mocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add IDs test to redis twins cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Simplify senml rec array and attribute creation funcs by removing unnecessary params Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Align cache remove twin method with service remove twin method Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache funcs to twins save, update and remove Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add def SaveIDs to redis cache and ref to service SaveStates Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add TwinSaveIDs tests for redis cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add cache related env vars desc to README.md Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add twinid bson key constant Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add Update method to cache Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Integrate uuid unification related changes Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Use named arguments in interface method declarations Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Add env vars to docker-compose file Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Make parameter names in interface methods and implementations consistent Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com> * Wrap vars and consts in var and const blocks Signed-off-by: Darko Draskovic <darko.draskovic@gmail.com>
2020-06-05 17:42:16 +08:00
return trm.repo.Remove(ctx, twinID)
}
type twinCacheMiddleware struct {
tracer opentracing.Tracer
cache twins.TwinCache
}
// TwinCacheMiddleware tracks request and their latency, and adds spans to context.
func TwinCacheMiddleware(tracer opentracing.Tracer, cache twins.TwinCache) twins.TwinCache {
return twinCacheMiddleware{
tracer: tracer,
cache: cache,
}
}
func (tcm twinCacheMiddleware) Save(ctx context.Context, twin twins.Twin) error {
span := createSpan(ctx, tcm.tracer, saveTwinOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return tcm.cache.Save(ctx, twin)
}
func (tcm twinCacheMiddleware) SaveIDs(ctx context.Context, channel, subtopic string, ids []string) error {
span := createSpan(ctx, tcm.tracer, saveTwinsOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return tcm.cache.SaveIDs(ctx, channel, subtopic, ids)
}
func (tcm twinCacheMiddleware) Update(ctx context.Context, twin twins.Twin) error {
span := createSpan(ctx, tcm.tracer, updateTwinOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return tcm.cache.Update(ctx, twin)
}
func (tcm twinCacheMiddleware) IDs(ctx context.Context, channel, subtopic string) ([]string, error) {
span := createSpan(ctx, tcm.tracer, retrieveTwinsByAttributeOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return tcm.cache.IDs(ctx, channel, subtopic)
}
func (tcm twinCacheMiddleware) Remove(ctx context.Context, twinID string) error {
span := createSpan(ctx, tcm.tracer, removeTwinOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return tcm.cache.Remove(ctx, twinID)
}
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)
}