Mainflux.mainflux/ws/api/metrics.go

49 lines
1.0 KiB
Go
Raw Normal View History

//
// Copyright (c) 2018
// Mainflux
//
// SPDX-License-Identifier: Apache-2.0
//
// +build !test
MF-171 - Extract websocket adapter as separate service (#188) * Add websocket adapter Add websocket adapter with basic logging and metrics middleware. Add publish and subscribe to NATS subjects using websocket. Add websocket handshake authorization over mainflux manager. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add test for websocket publish Add test for websocket adapter's publish method. Add dependecy injected logger to adapter. Remove unnecessary manager client dependency from adapter. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add websocket library in dependencies Add gorilla/websocket dependency in dep toml and lock file. Add dependency in vendor dir. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add direct websocket connection Add direct websocket connection. While messages are still beeing published over NATS, they are not received over src.ws subject in WebSocket adapter. Instead messages are sent directly over websocket connection. Add swagger file for WebSocket handshake endpoint. Update ReadMe to reference new swagger file. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Refactore websocket adapter code Extract listen part from handshake. Update WebSocket adapter code. Fix subscribe to NATS subject, so that it is independent from other adapters. Remove message base64 encoding from response.. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove connection management from websocket adapter Align notion of channel with NATS topic. Remove connection management from adapter logic. Add log messages to adapter's transport layer. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update NATS subjects and add subscriber interface Update NATS subject name to channel.<channel_id>. Add and implement subscriber interface. Implement subscriber interface in adapter. Update readme to use new make <service_name> command. Refactor code. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add basic tests for broadcast and subscribe Improve mock implementation of NATS pubsub. Add multiple basic test cases for broadcast and subscribe methods. Add logging for subscribe. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add listen method test Add listen method test and refactor existing tests. Refactor listen method in adapter. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Move broadcast method to message broker interface Refactor broadcast method to receive send message callback and message that needs to be sent. Update tests accordingly. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update pubsub API Remove listen method from public API. Move listen call to subscribe implementation. Update domain pubsub API in project root. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add connection error handlers to adapter API Update publish and subscribe API to receive connection error handler. Update tests accordingly. Handle NATS connection error. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update logs Replace go-kit logger with custom mainflux logger. Update log messages where needed.. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Refactor web socket adapter Remove MessagePubSub interface. Remove unnecessary callbacks. Add channels to web socket adapter implementation. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add API layer tests Update existing tests and add API layer tests. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update docs with web socket related data Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Refactor web socket adapter Update subscription struct, and refactor listen and broadcast methods. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add version and metrics endpoint to ws adapter Update version to 0.2.0. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Fix race condition bug in ws adapter test Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Rename listen to broadcast and broadcast to listen Switch names between listen and broadcast methods. Move channel structure to service.go. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com>
2018-04-18 19:09:01 +08:00
package api
import (
"time"
"github.com/go-kit/kit/metrics"
"github.com/mainflux/mainflux"
"github.com/mainflux/mainflux/ws"
)
var _ ws.Service = (*metricsMiddleware)(nil)
type metricsMiddleware struct {
counter metrics.Counter
latency metrics.Histogram
svc ws.Service
}
// MetricsMiddleware instruments adapter by tracking request count and latency.
func MetricsMiddleware(svc ws.Service, counter metrics.Counter, latency metrics.Histogram) ws.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
svc: svc,
}
}
func (mm *metricsMiddleware) Publish(msg mainflux.RawMessage) 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(msg)
}
MF-475 - Replace increment ID with UUID (#490) * Update increment ID to UUID in things service Update increment ID to UUID for things and channels in things service and proto files. Also, update ID type from uint to string. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in http adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in ws adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in CoAP adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in normalizer service Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in writer services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in reader services Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in SDK Update increment ID to UUID in SDK. Update id type to string. Update tests. Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update increment ID to UUID in mqtt adapter Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Remove unnecessary case from influxdb reader Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update tests in order to increase code coverage Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com> * Update lora adapter to use string ID instead of unsigned int Signed-off-by: Aleksandar Novakovic <aleksandar.novakovic@mainflux.com>
2018-12-05 20:09:25 +08:00
func (mm *metricsMiddleware) Subscribe(chanID string, channel *ws.Channel) error {
MF-171 - Extract websocket adapter as separate service (#188) * Add websocket adapter Add websocket adapter with basic logging and metrics middleware. Add publish and subscribe to NATS subjects using websocket. Add websocket handshake authorization over mainflux manager. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add test for websocket publish Add test for websocket adapter's publish method. Add dependecy injected logger to adapter. Remove unnecessary manager client dependency from adapter. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add websocket library in dependencies Add gorilla/websocket dependency in dep toml and lock file. Add dependency in vendor dir. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add direct websocket connection Add direct websocket connection. While messages are still beeing published over NATS, they are not received over src.ws subject in WebSocket adapter. Instead messages are sent directly over websocket connection. Add swagger file for WebSocket handshake endpoint. Update ReadMe to reference new swagger file. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Refactore websocket adapter code Extract listen part from handshake. Update WebSocket adapter code. Fix subscribe to NATS subject, so that it is independent from other adapters. Remove message base64 encoding from response.. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Remove connection management from websocket adapter Align notion of channel with NATS topic. Remove connection management from adapter logic. Add log messages to adapter's transport layer. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update NATS subjects and add subscriber interface Update NATS subject name to channel.<channel_id>. Add and implement subscriber interface. Implement subscriber interface in adapter. Update readme to use new make <service_name> command. Refactor code. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add basic tests for broadcast and subscribe Improve mock implementation of NATS pubsub. Add multiple basic test cases for broadcast and subscribe methods. Add logging for subscribe. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add listen method test Add listen method test and refactor existing tests. Refactor listen method in adapter. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Move broadcast method to message broker interface Refactor broadcast method to receive send message callback and message that needs to be sent. Update tests accordingly. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update pubsub API Remove listen method from public API. Move listen call to subscribe implementation. Update domain pubsub API in project root. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add connection error handlers to adapter API Update publish and subscribe API to receive connection error handler. Update tests accordingly. Handle NATS connection error. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update logs Replace go-kit logger with custom mainflux logger. Update log messages where needed.. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Refactor web socket adapter Remove MessagePubSub interface. Remove unnecessary callbacks. Add channels to web socket adapter implementation. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add API layer tests Update existing tests and add API layer tests. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Update docs with web socket related data Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Refactor web socket adapter Update subscription struct, and refactor listen and broadcast methods. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Add version and metrics endpoint to ws adapter Update version to 0.2.0. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Fix race condition bug in ws adapter test Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com> * Rename listen to broadcast and broadcast to listen Switch names between listen and broadcast methods. Move channel structure to service.go. Signed-off-by: Aleksandar Novakovic <anovakovic01@gmail.com>
2018-04-18 19:09:01 +08:00
return mm.svc.Subscribe(chanID, channel)
}