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

170 lines
5.4 KiB
Go
Raw Normal View History

// Copyright (c) Mainflux
2019-07-18 15:01:09 +02:00
// SPDX-License-Identifier: Apache-2.0
package tracing
import (
"context"
"github.com/mainflux/mainflux/things"
opentracing "github.com/opentracing/opentracing-go"
)
const (
saveChannelsOp = "save_channels"
2019-07-18 15:01:09 +02:00
updateChannelOp = "update_channel"
retrieveChannelByIDOp = "retrieve_channel_by_id"
retrieveAllChannelsOp = "retrieve_all_channels"
retrieveChannelsByThingOp = "retrieve_channels_by_thing"
removeChannelOp = "retrieve_channel"
connectOp = "connect"
disconnectOp = "disconnect"
hasThingOp = "has_thing"
hasThingByIDOp = "has_thing_by_id"
)
var (
_ things.ChannelRepository = (*channelRepositoryMiddleware)(nil)
_ things.ChannelCache = (*channelCacheMiddleware)(nil)
)
type channelRepositoryMiddleware struct {
tracer opentracing.Tracer
repo things.ChannelRepository
}
// ChannelRepositoryMiddleware tracks request and their latency, and adds spans
// to context.
func ChannelRepositoryMiddleware(tracer opentracing.Tracer, repo things.ChannelRepository) things.ChannelRepository {
return channelRepositoryMiddleware{
tracer: tracer,
repo: repo,
}
}
func (crm channelRepositoryMiddleware) Save(ctx context.Context, channels ...things.Channel) ([]things.Channel, error) {
span := createSpan(ctx, crm.tracer, saveChannelsOp)
2019-07-18 15:01:09 +02:00
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return crm.repo.Save(ctx, channels...)
}
2019-07-18 15:01:09 +02:00
func (crm channelRepositoryMiddleware) Update(ctx context.Context, ch things.Channel) error {
span := createSpan(ctx, crm.tracer, updateChannelOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return crm.repo.Update(ctx, ch)
}
func (crm channelRepositoryMiddleware) RetrieveByID(ctx context.Context, owner, id string) (things.Channel, error) {
span := createSpan(ctx, crm.tracer, retrieveChannelByIDOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return crm.repo.RetrieveByID(ctx, owner, id)
}
MF-1290 - Sort Things and Channels by name (#1293) * MF-1290 - Sort Things and Channels by name Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add sort HTTP parameter and use PageMetadata to pass filters Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix channels conn list Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix logs Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Replace sort naming by order Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add order in responses Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Improve tests Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Improve order tests Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Improve getNameQuery func Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix commits Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add name-asc and name-desc Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add dir query parameter Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix openapi Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix openapi format Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix test messages and openapi.yml Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix openapi uuid format Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Check query parameters in endpoint layer Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add asc endpoint tests Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Rename pageMeta into pageMetadata Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
2020-12-08 21:30:47 +01:00
func (crm channelRepositoryMiddleware) RetrieveAll(ctx context.Context, owner string, pm things.PageMetadata) (things.ChannelsPage, error) {
2019-07-18 15:01:09 +02:00
span := createSpan(ctx, crm.tracer, retrieveAllChannelsOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
MF-1290 - Sort Things and Channels by name (#1293) * MF-1290 - Sort Things and Channels by name Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add sort HTTP parameter and use PageMetadata to pass filters Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix channels conn list Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix logs Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Replace sort naming by order Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add order in responses Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Improve tests Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Improve order tests Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Improve getNameQuery func Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix commits Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add name-asc and name-desc Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add dir query parameter Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix openapi Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix openapi format Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix test messages and openapi.yml Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix openapi uuid format Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Check query parameters in endpoint layer Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add asc endpoint tests Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Rename pageMeta into pageMetadata Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
2020-12-08 21:30:47 +01:00
return crm.repo.RetrieveAll(ctx, owner, pm)
2019-07-18 15:01:09 +02:00
}
func (crm channelRepositoryMiddleware) RetrieveByThing(ctx context.Context, owner, thID string, pm things.PageMetadata) (things.ChannelsPage, error) {
2019-07-18 15:01:09 +02:00
span := createSpan(ctx, crm.tracer, retrieveChannelsByThingOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return crm.repo.RetrieveByThing(ctx, owner, thID, pm)
2019-07-18 15:01:09 +02:00
}
func (crm channelRepositoryMiddleware) Remove(ctx context.Context, owner, id string) error {
span := createSpan(ctx, crm.tracer, removeChannelOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return crm.repo.Remove(ctx, owner, id)
}
func (crm channelRepositoryMiddleware) Connect(ctx context.Context, owner string, chIDs, thIDs []string) error {
2019-07-18 15:01:09 +02:00
span := createSpan(ctx, crm.tracer, connectOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return crm.repo.Connect(ctx, owner, chIDs, thIDs)
2019-07-18 15:01:09 +02:00
}
func (crm channelRepositoryMiddleware) Disconnect(ctx context.Context, owner string, chIDs, thIDs []string) error {
2019-07-18 15:01:09 +02:00
span := createSpan(ctx, crm.tracer, disconnectOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return crm.repo.Disconnect(ctx, owner, chIDs, thIDs)
2019-07-18 15:01:09 +02:00
}
func (crm channelRepositoryMiddleware) HasThing(ctx context.Context, chanID, key string) (string, error) {
span := createSpan(ctx, crm.tracer, hasThingOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return crm.repo.HasThing(ctx, chanID, key)
}
func (crm channelRepositoryMiddleware) HasThingByID(ctx context.Context, chanID, thingID string) error {
span := createSpan(ctx, crm.tracer, hasThingByIDOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return crm.repo.HasThingByID(ctx, chanID, thingID)
}
type channelCacheMiddleware struct {
tracer opentracing.Tracer
cache things.ChannelCache
}
// ChannelCacheMiddleware tracks request and their latency, and adds spans
// to context.
func ChannelCacheMiddleware(tracer opentracing.Tracer, cache things.ChannelCache) things.ChannelCache {
return channelCacheMiddleware{
tracer: tracer,
cache: cache,
}
}
func (ccm channelCacheMiddleware) Connect(ctx context.Context, chanID, thingID string) error {
span := createSpan(ctx, ccm.tracer, connectOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return ccm.cache.Connect(ctx, chanID, thingID)
}
func (ccm channelCacheMiddleware) HasThing(ctx context.Context, chanID, thingID string) bool {
span := createSpan(ctx, ccm.tracer, hasThingOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return ccm.cache.HasThing(ctx, chanID, thingID)
}
func (ccm channelCacheMiddleware) Disconnect(ctx context.Context, chanID, thingID string) error {
span := createSpan(ctx, ccm.tracer, disconnectOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return ccm.cache.Disconnect(ctx, chanID, thingID)
}
func (ccm channelCacheMiddleware) Remove(ctx context.Context, chanID string) error {
span := createSpan(ctx, ccm.tracer, removeChannelOp)
defer span.Finish()
ctx = opentracing.ContextWithSpan(ctx, span)
return ccm.cache.Remove(ctx, chanID)
}