Skip to content
This repository has been archived by the owner on Dec 14, 2021. It is now read-only.

Commit

Permalink
Merge branch 'feature/logging' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
htdvisser committed Feb 3, 2017
2 parents 3f86382 + 4bc2ffe commit fc4f9bd
Show file tree
Hide file tree
Showing 21 changed files with 486 additions and 60 deletions.
14 changes: 14 additions & 0 deletions api/broker/client_streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ func NewMonitoredRouterStream(client BrokerClient, getContextFunc func() context
message, err := client.Recv()
if message != nil {
s.ctx.Debug("Receiving Downlink message")
if err := message.Validate(); err != nil {
s.ctx.WithError(err).Warn("Invalid Downlink")
continue
}
if err := message.UnmarshalPayload(); err != nil {
s.ctx.Warn("Could not unmarshal Downlink payload")
}
select {
case s.down <- message:
default:
Expand Down Expand Up @@ -345,6 +352,13 @@ func NewMonitoredHandlerSubscribeStream(client BrokerClient, getContextFunc func
message, err = client.Recv()
if message != nil {
s.ctx.Debug("Receiving Uplink message")
if err := message.Validate(); err != nil {
s.ctx.WithError(err).Warn("Invalid Uplink")
continue
}
if err := message.UnmarshalPayload(); err != nil {
s.ctx.Warn("Could not unmarshal Uplink payload")
}
select {
case s.ch <- message:
default:
Expand Down
90 changes: 90 additions & 0 deletions api/broker/missing_utils.pb.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.

package broker

import "github.com/TheThingsNetwork/ttn/core/types"

func (m *UplinkMessage) GetAppEui() *types.AppEUI {
if m != nil {
return m.AppEui
}
return nil
}

func (m *UplinkMessage) GetDevEui() *types.DevEUI {
if m != nil {
return m.DevEui
}
return nil
}

func (m *DownlinkMessage) GetAppEui() *types.AppEUI {
if m != nil {
return m.AppEui
}
return nil
}

func (m *DownlinkMessage) GetDevEui() *types.DevEUI {
if m != nil {
return m.DevEui
}
return nil
}

func (m *DeviceActivationRequest) GetAppEui() *types.AppEUI {
if m != nil {
return m.AppEui
}
return nil
}

func (m *DeviceActivationRequest) GetDevEui() *types.DevEUI {
if m != nil {
return m.DevEui
}
return nil
}

func (m *DeduplicatedUplinkMessage) GetAppEui() *types.AppEUI {
if m != nil {
return m.AppEui
}
return nil
}

func (m *DeduplicatedUplinkMessage) GetDevEui() *types.DevEUI {
if m != nil {
return m.DevEui
}
return nil
}

func (m *DeduplicatedDeviceActivationRequest) GetAppEui() *types.AppEUI {
if m != nil {
return m.AppEui
}
return nil
}

func (m *DeduplicatedDeviceActivationRequest) GetDevEui() *types.DevEUI {
if m != nil {
return m.DevEui
}
return nil
}

func (m *ActivationChallengeRequest) GetAppEui() *types.AppEUI {
if m != nil {
return m.AppEui
}
return nil
}

func (m *ActivationChallengeRequest) GetDevEui() *types.DevEUI {
if m != nil {
return m.DevEui
}
return nil
}
13 changes: 10 additions & 3 deletions api/broker/server_streams.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/api"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/golang/protobuf/ptypes/empty"
"google.golang.org/grpc/metadata"
)
Expand Down Expand Up @@ -70,7 +69,11 @@ func (s *BrokerStreamServer) Associate(stream Broker_AssociateServer) (err error
return err
}
if err := uplink.Validate(); err != nil {
return errors.Wrap(err, "Invalid Uplink")
s.ctx.WithError(err).Warn("Invalid Uplink")
continue
}
if err := uplink.UnmarshalPayload(); err != nil {
s.ctx.Warn("Could not unmarshal uplink payload")
}
upChan <- uplink
}
Expand Down Expand Up @@ -139,7 +142,11 @@ func (s *BrokerStreamServer) Publish(stream Broker_PublishServer) error {
return err
}
if err := downlink.Validate(); err != nil {
return errors.Wrap(err, "Invalid Downlink")
s.ctx.WithError(err).Warn("Invalid Downlink")
continue
}
if err := downlink.UnmarshalPayload(); err != nil {
s.ctx.Warn("Could not unmarshal downlink payload")
}
ch <- downlink
}
Expand Down
240 changes: 240 additions & 0 deletions api/fields/log_fields.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.

package fields

import (
"github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/api/gateway"
"github.com/TheThingsNetwork/ttn/api/protocol"
"github.com/TheThingsNetwork/ttn/core/types"
)

type hasId interface {
GetId() string
}

type hasServiceName interface {
GetServiceName() string
}

func fillDiscoveryFields(m interface{}, f log.Fields) {
if m, ok := m.(hasId); ok {
if v := m.GetId(); v != "" {
f["ID"] = v
}
}
if m, ok := m.(hasServiceName); ok {
if v := m.GetServiceName(); v != "" {
f["ServiceName"] = v
}
}
}

type hasDevId interface {
GetDevId() string
}

type hasAppId interface {
GetAppId() string
}

type hasDevEui interface {
GetDevEui() *types.DevEUI
}

type hasAppEui interface {
GetAppEui() *types.AppEUI
}

type hasDevAddr interface {
GetDevAddr() *types.DevAddr
}

func fillIdentifiers(m interface{}, f log.Fields) {
if m, ok := m.(hasDevEui); ok {
if v := m.GetDevEui(); v != nil {
f["DevEUI"] = v
}
}
if m, ok := m.(hasAppEui); ok {
if v := m.GetAppEui(); v != nil {
f["AppEUI"] = v
}
}
if m, ok := m.(hasDevId); ok {
if v := m.GetDevId(); v != "" {
f["DevID"] = v
}
}
if m, ok := m.(hasAppId); ok {
if v := m.GetAppId(); v != "" {
f["AppID"] = v
}
}
if m, ok := m.(hasDevAddr); ok {
if v := m.GetDevAddr(); v != nil {
f["DevAddr"] = v
}
}
}

type hasProtocolMetadata interface {
GetProtocolMetadata() *protocol.RxMetadata
}

type hasProtocolConfiguration interface {
GetProtocolConfiguration() *protocol.TxConfiguration
}

type hasProtocolConfig interface {
GetProtocolConfig() *protocol.TxConfiguration
}

func fillProtocolConfig(cfg *protocol.TxConfiguration, f log.Fields) {
if lorawan := cfg.GetLorawan(); lorawan != nil {
if v := lorawan.Modulation.String(); v != "" {
f["Modulation"] = v
}
if v := lorawan.DataRate; v != "" {
f["DataRate"] = v
}
if v := lorawan.BitRate; v != 0 {
f["BitRate"] = v
}
if v := lorawan.CodingRate; v != "" {
f["CodingRate"] = v
}
}
}

func fillProtocol(m interface{}, f log.Fields) {
if m, ok := m.(hasProtocolMetadata); ok {
if meta := m.GetProtocolMetadata(); meta != nil {
if lorawan := meta.GetLorawan(); lorawan != nil {
if v := lorawan.Modulation.String(); v != "" {
f["Modulation"] = v
}
if v := lorawan.DataRate; v != "" {
f["DataRate"] = v
}
if v := lorawan.BitRate; v != 0 {
f["BitRate"] = v
}
if v := lorawan.CodingRate; v != "" {
f["CodingRate"] = v
}
}
}
}
if m, ok := m.(hasProtocolConfiguration); ok {
fillProtocolConfig(m.GetProtocolConfiguration(), f)
}
if m, ok := m.(hasProtocolConfig); ok {
fillProtocolConfig(m.GetProtocolConfig(), f)
}
}

type hasGatewayConfiguration interface {
GetGatewayConfiguration() *gateway.TxConfiguration
}

type hasGatewayConfig interface {
GetGatewayConfig() *gateway.TxConfiguration
}

type hasGatewayMetadata interface {
GetGatewayMetadata() *gateway.RxMetadata
}

type hasMoreGatewayMetadata interface {
GetGatewayMetadata() []*gateway.RxMetadata
}

func fillGatewayConfig(cfg *gateway.TxConfiguration, f log.Fields) {
if v := cfg.Frequency; v != 0 {
f["Frequency"] = v
}
if v := cfg.Power; v != 0 {
f["Power"] = v
}
}

func fillGateway(m interface{}, f log.Fields) {
if m, ok := m.(hasGatewayMetadata); ok {
if meta := m.GetGatewayMetadata(); meta != nil {
if v := meta.GatewayId; v != "" {
f["GatewayID"] = v
}
if v := meta.Frequency; v != 0 {
f["Frequency"] = v
}
if v := meta.Rssi; v != 0 {
f["RSSI"] = v
}
if v := meta.Snr; v != 0 {
f["SNR"] = v
}
}
}
if m, ok := m.(hasMoreGatewayMetadata); ok {
if meta := m.GetGatewayMetadata(); meta != nil {
f["NumGateways"] = len(meta)
}
}
if m, ok := m.(hasGatewayConfiguration); ok {
fillGatewayConfig(m.GetGatewayConfiguration(), f)
}
if m, ok := m.(hasGatewayConfig); ok {
fillGatewayConfig(m.GetGatewayConfig(), f)
}
}

type hasMessage interface {
GetMessage() *protocol.Message
}

type hasPayload interface {
GetPayload() []byte
}

func fillMessage(m interface{}, f log.Fields) {
if m, ok := m.(hasPayload); ok {
f["PayloadSize"] = len(m.GetPayload())
}
if m, ok := m.(hasMessage); ok {
if m := m.GetMessage(); m != nil {
if lorawan := m.GetLorawan(); lorawan != nil {
if mac := lorawan.GetMacPayload(); mac != nil {
if v := mac.DevAddr; !v.IsEmpty() {
f["DevAddr"] = v
}
if v := len(mac.FrmPayload); v != 0 {
f["AppPayloadSize"] = v
}
if v := mac.FPort; v != 0 {
f["Port"] = v
}
if v := mac.FCnt; v != 0 {
f["Counter"] = v
}
}
if join := lorawan.GetJoinRequestPayload(); join != nil {
f["AppEUI"] = join.AppEui
f["DevEUI"] = join.DevEui
}
}
}
}
}

// Get a number of log fields for a message, if we're able to extract them
func Get(m interface{}) log.Fields {
fields := log.Fields{}
fillDiscoveryFields(m, fields)
fillIdentifiers(m, fields)
fillGateway(m, fields)
fillProtocol(m, fields)
fillMessage(m, fields)
return fields
}
Loading

0 comments on commit fc4f9bd

Please sign in to comment.