Skip to content

Commit

Permalink
feat(prometheus): Add suggestion notes for more static metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
kereis committed Feb 25, 2021
1 parent c1c78c1 commit 12da79d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions meters/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func NewManager(conn Connection) *Manager {
devices: make([]device, 0),
Conn: conn,
}
// TODO prometheus: ConnectionManagerCreated
return &m
}

Expand Down
12 changes: 11 additions & 1 deletion meters/sunspec/sunspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package sunspec
import (
"errors"
"fmt"
prometheusManager "github.com/volkszaehler/mbmd/prometheus_metrics"
"math"
"sort"
"strconv"
"strings"
"time"

prometheusManager "github.com/volkszaehler/mbmd/prometheus_metrics"

sunspec "github.com/andig/gosunspec"
sunspecbus "github.com/andig/gosunspec/modbus"
"github.com/grid-x/modbus"
Expand Down Expand Up @@ -65,17 +66,22 @@ func (d *SunSpec) Initialize(client modbus.Client) error {
var partiallyOpen bool
in, err := sunspecbus.Open(client)

// TODO prometheus: DeviceModbusConnectionAttemptTotal
prometheusManager.ConnectionAttemptTotal.WithLabelValues(d.descriptor.Model, strconv.Itoa(d.descriptor.SubDevice)).Inc()

if err != nil {
if in == nil {
// TODO prometheus: DeviceModbusConnectionFailedTotal
prometheusManager.ConnectionAttemptFailedTotal.WithLabelValues(d.descriptor.Model, strconv.Itoa(d.descriptor.SubDevice)).Inc()
return err
}

partiallyOpen = true
// TODO prometheus: DeviceModbusConnectionPartialSuccess
}

// TODO prometheus: else DeviceModbusConnectionSuccess

devices := in.Collect(sunspec.AllDevices)
if len(devices) == 0 {
return errors.New("sunspec: device not found")
Expand All @@ -88,13 +94,17 @@ func (d *SunSpec) Initialize(client modbus.Client) error {

// read common block
if err := d.readCommonBlock(device); err != nil {
// TODO prometheus: DeviceModbusCommonBlockReadFailure
return err
}
// TODO prometheus: DeviceModbusCommonBlockReadSuccess

// collect relevant models
if err := d.collectModels(device); err != nil {
// TODO prometheus: DeviceModbusModelCollectionFailure
return err
}
// TODO prometheus: DeviceModbusModelCollectionSuccess

// return partial open error if everything else went fine
if partiallyOpen {
Expand Down
3 changes: 3 additions & 0 deletions meters/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func NewTCP(address string) Connection {
Handler: handler,
}

// TODO prometheus: TCPConnectionCreated

return b
}

Expand Down Expand Up @@ -71,4 +73,5 @@ func (b *TCP) Timeout(timeout time.Duration) time.Duration {
// This forces the modbus client to reopen the connection before the next bus operations.
func (b *TCP) Close() {
b.Handler.Close()
// TODO prometheus: TCPConnectionClosed
}
6 changes: 6 additions & 0 deletions server/socket.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,31 @@ type SocketClient struct {
func (c *SocketClient) writePump() {
defer func() {
c.conn.Close()
// TODO prometheus: WebsocketClientConnectionClosed
}()
for {
msg := <-c.send
if err := c.conn.SetWriteDeadline(time.Now().Add(socketWriteWait)); err != nil {
return
}
if err := c.conn.WriteMessage(websocket.TextMessage, msg); err != nil {
// TODO prometheus: WebsocketClientMessagesSentFailed
return
}
// TODO prometheus: WebsocketClientMessagesSentSuccessfully
}
}

// ServeWebsocket handles websocket requests from the peer.
func ServeWebsocket(hub *SocketHub, w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil)
// TODO prometheus: WebsocketClientCreated
if err != nil {
log.Println(err)
// TODO prometheus: WebsocketClientCreationFailed
return
}
// TODO prometheus: WebsocketClientCreatedSucessfully
client := &SocketClient{hub: hub, conn: conn, send: make(chan []byte, 256)}
client.hub.register <- client

Expand Down

0 comments on commit 12da79d

Please sign in to comment.