Skip to content

Commit

Permalink
Merge pull request #7430 from TheThingsNetwork/gs-event-data-update-w…
Browse files Browse the repository at this point in the history
…hile-marshaling

Fix gateway server event data change during marshaling
  • Loading branch information
vlasebian authored Dec 6, 2024
2 parents b8f6e79 + 44dc815 commit 52748a4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
9 changes: 9 additions & 0 deletions config/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -4832,6 +4832,15 @@
"file": "pattern.go"
}
},
"error:pkg/events:marshal_data": {
"translations": {
"en": "marshal data"
},
"description": {
"package": "pkg/events",
"file": "events.go"
}
},
"error:pkg/events:no_matching_events": {
"translations": {
"en": "no matching events for regexp `{regexp}`"
Expand Down
22 changes: 22 additions & 0 deletions pkg/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ import (
"strings"
"time"

"github.com/getsentry/sentry-go"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
sentryerrors "go.thethings.network/lorawan-stack/v3/pkg/errors/sentry"
"go.thethings.network/lorawan-stack/v3/pkg/goproto"
"go.thethings.network/lorawan-stack/v3/pkg/jsonpb"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
Expand Down Expand Up @@ -176,7 +178,27 @@ func New(ctx context.Context, name, description string, opts ...Option) Event {
return (&definition{name: name, description: description}).New(ctx, opts...)
}

var errMarshalData = errors.Define("marshal_data", "marshal data")

func marshalData(data any) (anyPB *anypb.Any, err error) {
// TODO: https://github.com/TheThingsIndustries/lorawan-stack-support/issues/1163.
// Remove this after the issue is fixed.
defer func() {
if p := recover(); p != nil {
if pErr, ok := p.(error); ok {
err = errMarshalData.WithCause(pErr)
} else {
err = errMarshalData.WithAttributes("panic", p)
}
event := sentryerrors.NewEvent(err)
sentry.CaptureEvent(event)
}
}()

return mustMarshalData(data)
}

func mustMarshalData(data any) (anyPB *anypb.Any, err error) {
if protoMessage, ok := data.(proto.Message); ok {
anyPB, err = anypb.New(protoMessage)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/gatewayserver/gatewayserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1101,7 +1101,7 @@ func (gs *GatewayServer) updateConnStats(ctx context.Context, conn connectionEnt
Protocol: conn.Connection.Frontend().Protocol(),
GatewayRemoteAddress: conn.Connection.GatewayRemoteAddress(),
}
registerGatewayConnectionStats(ctx, ids, stats)
registerGatewayConnectionStats(ctx, ids, ttnpb.Clone(stats))
if gs.statsRegistry != nil {
if err := gs.statsRegistry.Set(
decoupledCtx,
Expand All @@ -1121,7 +1121,7 @@ func (gs *GatewayServer) updateConnStats(ctx context.Context, conn connectionEnt
ConnectedAt: nil,
DisconnectedAt: timestamppb.Now(),
}
registerGatewayConnectionStats(decoupledCtx, ids, stats)
registerGatewayConnectionStats(decoupledCtx, ids, ttnpb.Clone(stats))
if gs.statsRegistry == nil {
return
}
Expand Down
1 change: 1 addition & 0 deletions pkg/webui/locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -2362,6 +2362,7 @@
"error:pkg/events/redis:channel_closed": "チャネルが閉じています",
"error:pkg/events/redis:unknown_encoding": "不明なエンコーディング",
"error:pkg/events:invalid_regexp": "無効な正規表現",
"error:pkg/events:marshal_data": "",
"error:pkg/events:no_matching_events": "正規表現`{regexp}`に一致するイベントがありません",
"error:pkg/events:unknown_event_name": "不明なイベント`{name}`",
"error:pkg/fetch:fetch_file": "ファイル `{filename}` を取得できません",
Expand Down

0 comments on commit 52748a4

Please sign in to comment.