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

Commit

Permalink
Log more metadata everywhere 💪
Browse files Browse the repository at this point in the history
  • Loading branch information
htdvisser committed Feb 3, 2017
1 parent 722d5c0 commit 4bc2ffe
Show file tree
Hide file tree
Showing 12 changed files with 277 additions and 56 deletions.
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
}
7 changes: 2 additions & 5 deletions core/broker/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
ttnlog "github.com/TheThingsNetwork/go-utils/log"
pb "github.com/TheThingsNetwork/ttn/api/broker"
pb_discovery "github.com/TheThingsNetwork/ttn/api/discovery"
"github.com/TheThingsNetwork/ttn/api/fields"
pb_handler "github.com/TheThingsNetwork/ttn/api/handler"
"github.com/TheThingsNetwork/ttn/api/trace"
"github.com/TheThingsNetwork/ttn/utils/errors"
Expand All @@ -28,11 +29,7 @@ type challengeResponseWithHandler struct {
var errDuplicateActivation = errors.New("Not handling duplicate activation on this gateway")

func (b *broker) HandleActivation(activation *pb.DeviceActivationRequest) (res *pb.DeviceActivationResponse, err error) {
ctx := b.Ctx.WithFields(ttnlog.Fields{
"GatewayID": activation.GatewayMetadata.GatewayId,
"AppEUI": *activation.AppEui,
"DevEUI": *activation.DevEui,
})
ctx := b.Ctx.WithFields(fields.Get(activation))
start := time.Now()
deduplicatedActivationRequest := new(pb.DeduplicatedDeviceActivationRequest)
deduplicatedActivationRequest.ServerTime = start.UnixNano()
Expand Down
7 changes: 2 additions & 5 deletions core/broker/downlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"
"time"

ttnlog "github.com/TheThingsNetwork/go-utils/log"
pb "github.com/TheThingsNetwork/ttn/api/broker"
"github.com/TheThingsNetwork/ttn/api/fields"
"github.com/TheThingsNetwork/ttn/api/trace"
"github.com/TheThingsNetwork/ttn/utils/errors"
)
Expand All @@ -21,10 +21,7 @@ func (a ByScore) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a ByScore) Less(i, j int) bool { return a[i].Score < a[j].Score }

func (b *broker) HandleDownlink(downlink *pb.DownlinkMessage) error {
ctx := b.Ctx.WithFields(ttnlog.Fields{
"DevEUI": *downlink.DevEui,
"AppEUI": *downlink.AppEui,
})
ctx := b.Ctx.WithFields(fields.Get(downlink))
var err error
start := time.Now()
defer func() {
Expand Down
3 changes: 2 additions & 1 deletion core/broker/uplink.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
ttnlog "github.com/TheThingsNetwork/go-utils/log"
pb "github.com/TheThingsNetwork/ttn/api/broker"
pb_discovery "github.com/TheThingsNetwork/ttn/api/discovery"
"github.com/TheThingsNetwork/ttn/api/fields"
"github.com/TheThingsNetwork/ttn/api/networkserver"
pb_lorawan "github.com/TheThingsNetwork/ttn/api/protocol/lorawan"
"github.com/TheThingsNetwork/ttn/api/trace"
Expand All @@ -25,7 +26,7 @@ import (
const maxFCntGap = 16384

func (b *broker) HandleUplink(uplink *pb.UplinkMessage) (err error) {
ctx := b.Ctx.WithField("GatewayID", uplink.GatewayMetadata.GatewayId)
ctx := b.Ctx.WithFields(fields.Get(uplink))
start := time.Now()
deduplicatedUplink := new(pb.DeduplicatedUplinkMessage)
deduplicatedUplink.ServerTime = start.UnixNano()
Expand Down
5 changes: 3 additions & 2 deletions core/component/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package component
import (
"github.com/TheThingsNetwork/go-utils/grpc/interceptor"
"github.com/TheThingsNetwork/go-utils/log"
"github.com/TheThingsNetwork/ttn/api/fields"
"github.com/TheThingsNetwork/ttn/utils/errors"
"github.com/mwitkow/go-grpc-middleware"
"golang.org/x/net/context" // See https://github.com/grpc/grpc-go/issues/711"
Expand All @@ -16,7 +17,7 @@ import (
func (c *Component) ServerOptions() []grpc.ServerOption {

unaryLog := interceptor.Unary(func(req interface{}, info *grpc.UnaryServerInfo) (log.Interface, string) {
return c.Ctx, "Request"
return c.Ctx.WithFields(fields.Get(req)), "Request"
})

unaryErr := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
Expand All @@ -25,7 +26,7 @@ func (c *Component) ServerOptions() []grpc.ServerOption {
return iface, err
}

streamLog := interceptor.Stream(func(req interface{}, info *grpc.StreamServerInfo) (log.Interface, string) {
streamLog := interceptor.Stream(func(srv interface{}, info *grpc.StreamServerInfo) (log.Interface, string) {
return c.Ctx, "Stream"
})

Expand Down
16 changes: 2 additions & 14 deletions core/handler/activation.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

ttnlog "github.com/TheThingsNetwork/go-utils/log"
pb_broker "github.com/TheThingsNetwork/ttn/api/broker"
"github.com/TheThingsNetwork/ttn/api/fields"
pb "github.com/TheThingsNetwork/ttn/api/handler"
"github.com/TheThingsNetwork/ttn/api/trace"
"github.com/TheThingsNetwork/ttn/core/handler/device"
Expand Down Expand Up @@ -71,20 +72,7 @@ func (h *handler) HandleActivationChallenge(challenge *pb_broker.ActivationChall

func (h *handler) HandleActivation(activation *pb_broker.DeduplicatedDeviceActivationRequest) (res *pb.DeviceActivationResponse, err error) {
appID, devID := activation.AppId, activation.DevId
var appEUI types.AppEUI
if activation.AppEui != nil {
appEUI = *activation.AppEui
}
var devEUI types.DevEUI
if activation.DevEui != nil {
devEUI = *activation.DevEui
}
ctx := h.Ctx.WithFields(ttnlog.Fields{
"DevEUI": devEUI,
"AppEUI": appEUI,
"AppID": appID,
"DevID": devID,
})
ctx := h.Ctx.WithFields(fields.Get(activation))
start := time.Now()
defer func() {
if err != nil {
Expand Down
7 changes: 2 additions & 5 deletions core/handler/uplink.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ package handler
import (
"time"

ttnlog "github.com/TheThingsNetwork/go-utils/log"
pb_broker "github.com/TheThingsNetwork/ttn/api/broker"
"github.com/TheThingsNetwork/ttn/api/fields"
"github.com/TheThingsNetwork/ttn/api/trace"
"github.com/TheThingsNetwork/ttn/core/types"
)
Expand All @@ -17,10 +17,7 @@ var ResponseDeadline = 100 * time.Millisecond

func (h *handler) HandleUplink(uplink *pb_broker.DeduplicatedUplinkMessage) (err error) {
appID, devID := uplink.AppId, uplink.DevId
ctx := h.Ctx.WithFields(ttnlog.Fields{
"AppID": appID,
"DevID": devID,
})
ctx := h.Ctx.WithFields(fields.Get(uplink))
start := time.Now()
defer func() {
if err != nil {
Expand Down
Loading

0 comments on commit 4bc2ffe

Please sign in to comment.