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

* NOISSUE- Add OPC-UA adapter Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * NOISSUE - Add opc-adapter PoC, docker and vendor Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Convert OPC messages to SenML Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add gopcua package Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * lora-adapter typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add OPC Reader Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Typo fix Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Typo fix Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Update copyright headers Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix reviews Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix reviews Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add opc config Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add all opc envars in the config Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Config typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add route map Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Use opcua package instead of opc Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix OPCUA typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Rm MQTT sub Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Move interefaces to root Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Fix revieews and typo Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Update Gopkg.toml Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com> * Add all envars into .env Signed-off-by: Manuel Imperiale <manuel.imperiale@gmail.com>
93 lines
2.7 KiB
Go
93 lines
2.7 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package api
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/go-kit/kit/metrics"
|
|
"github.com/mainflux/mainflux/opcua"
|
|
)
|
|
|
|
var _ opcua.Service = (*metricsMiddleware)(nil)
|
|
|
|
type metricsMiddleware struct {
|
|
counter metrics.Counter
|
|
latency metrics.Histogram
|
|
svc opcua.Service
|
|
}
|
|
|
|
// MetricsMiddleware instruments core service by tracking request count and latency.
|
|
func MetricsMiddleware(svc opcua.Service, counter metrics.Counter, latency metrics.Histogram) opcua.Service {
|
|
return &metricsMiddleware{
|
|
counter: counter,
|
|
latency: latency,
|
|
svc: svc,
|
|
}
|
|
}
|
|
|
|
func (mm *metricsMiddleware) CreateThing(mfxDevID string, opcID string) error {
|
|
defer func(begin time.Time) {
|
|
mm.counter.With("method", "create_thing").Add(1)
|
|
mm.latency.With("method", "create_thing").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return mm.svc.CreateThing(mfxDevID, opcID)
|
|
}
|
|
|
|
func (mm *metricsMiddleware) UpdateThing(mfxDevID string, opcID string) error {
|
|
defer func(begin time.Time) {
|
|
mm.counter.With("method", "update_thing").Add(1)
|
|
mm.latency.With("method", "update_thing").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return mm.svc.UpdateThing(mfxDevID, opcID)
|
|
}
|
|
|
|
func (mm *metricsMiddleware) RemoveThing(mfxDevID string) error {
|
|
defer func(begin time.Time) {
|
|
mm.counter.With("method", "remove_thing").Add(1)
|
|
mm.latency.With("method", "remove_thing").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return mm.svc.RemoveThing(mfxDevID)
|
|
}
|
|
|
|
func (mm *metricsMiddleware) CreateChannel(mfxChanID string, opcNamespace string) error {
|
|
defer func(begin time.Time) {
|
|
mm.counter.With("method", "create_channel").Add(1)
|
|
mm.latency.With("method", "create_channel").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return mm.svc.CreateChannel(mfxChanID, opcNamespace)
|
|
}
|
|
|
|
func (mm *metricsMiddleware) UpdateChannel(mfxChanID string, opcNamespace string) error {
|
|
defer func(begin time.Time) {
|
|
mm.counter.With("method", "update_channel").Add(1)
|
|
mm.latency.With("method", "update_channel").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return mm.svc.UpdateChannel(mfxChanID, opcNamespace)
|
|
}
|
|
|
|
func (mm *metricsMiddleware) RemoveChannel(mfxChanID string) error {
|
|
defer func(begin time.Time) {
|
|
mm.counter.With("method", "remove_channel").Add(1)
|
|
mm.latency.With("method", "remove_channel").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return mm.svc.RemoveChannel(mfxChanID)
|
|
}
|
|
|
|
func (mm *metricsMiddleware) Publish(ctx context.Context, token string, m opcua.Message) error {
|
|
defer func(begin time.Time) {
|
|
mm.counter.With("method", "publish").Add(1)
|
|
mm.latency.With("method", "publish").Observe(time.Since(begin).Seconds())
|
|
}(time.Now())
|
|
|
|
return mm.svc.Publish(ctx, token, m)
|
|
}
|