Skip to content

Commit c86a5dc

Browse files
committed
fix(transation): updated transation logger to use built-in Micro-Trace-Id
using go-micro's built-in Trace ID generator
1 parent 029560c commit c86a5dc

File tree

6 files changed

+16
-20
lines changed

6 files changed

+16
-20
lines changed

cmd/account/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func getUserList2(limit uint32) {
6464

6565
// New Service
6666
service := micro.NewService(
67-
micro.Name("mkit.client.account"),
67+
micro.Name(constants.ACCOUNT_CLIENT),
6868
micro.Version(config.Version),
6969
micro.WrapClient(logWrapper.NewClientWrapper()), // Showcase ClientWrapper usage
7070
)

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ require (
1313
github.com/envoyproxy/protoc-gen-validate v0.1.0
1414
github.com/gogo/protobuf v1.3.1
1515
github.com/golang/protobuf v1.4.2
16-
github.com/google/uuid v1.1.1
1716
github.com/infobloxopen/atlas-app-toolkit v0.21.1
1817
github.com/infobloxopen/protoc-gen-gorm v0.20.0
1918
github.com/jinzhu/gorm v1.9.12

service/recorder/subscriber/transaction.subscriber.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ func NewTransactionSubscriber(repo repository.TransactionRepository) *Transactio
2727
// Handle is a method to record transaction event, Method can be of any name
2828
func (s *TransactionSubscriber) Handle(ctx context.Context, event *transactionPB.TransactionEvent) (err error) {
2929
md, _ := metadata.FromContext(ctx)
30-
tranId := md[constants.TransID]
30+
tranId := md[constants.TraceIDKey]
3131

3232
if len(tranId) == 0 {
3333
log.Error().Msg("TransactionSubscriber: missing TranID")
@@ -40,6 +40,8 @@ func (s *TransactionSubscriber) Handle(ctx context.Context, event *transactionPB
4040
err = s.repo.Write(ctx, fmt.Sprintf("%s#%s", tranId, from), event)
4141
case constants.GREETER_SERVICE:
4242
err = s.repo.Write(ctx, fmt.Sprintf("%s#%s", tranId, from), event)
43+
case constants.ACCOUNT_CLIENT:
44+
err = s.repo.Write(ctx, fmt.Sprintf("%s#%s", tranId, from), event)
4345
default:
4446
log.Error().Msgf("TransactionSubscriber: unknown from: %s", from)
4547
return fmt.Errorf("TransactionSubscriber: unknown from: %s", from)

shared/constants/constants.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package constants
22

33
const (
4+
ACCOUNT_CLIENT = "mkit.client.account"
45
ACCOUNT_SERVICE = "mkit.service.account"
56
GREETER_SERVICE = "mkit.service.greeter"
67
EMAILER_SERVICE = "mkit.service.emailer"

shared/constants/metadata.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package constants
44

55
const (
66
// There are certain requirements for metadata to be passed in the http header:
7-
// Just use all capital letters for God's sake
8-
TransID = "TRANS-ID"
7+
// gRPC recommended Key format: `lowercase alphanumeric characters and hyphen`
8+
// but go-micro use `camelcase alphanumeric characters and hyphen`
9+
TraceIDKey = "Micro-Trace-Id"
10+
TenantIdKey = "Tenant-Id"
911
)

shared/wrapper/transaction/transaction.wrapper.go

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package transaction
22

33
import (
4-
"context"
4+
"context"
55

6-
"github.com/golang/protobuf/proto"
7-
"github.com/golang/protobuf/ptypes/empty"
8-
"github.com/google/uuid"
9-
"github.com/micro/go-micro/v2"
10-
"github.com/micro/go-micro/v2/metadata"
11-
"github.com/micro/go-micro/v2/server"
12-
"github.com/rs/zerolog/log"
6+
"github.com/golang/protobuf/proto"
7+
"github.com/golang/protobuf/ptypes/empty"
8+
"github.com/micro/go-micro/v2"
9+
"github.com/micro/go-micro/v2/server"
10+
"github.com/rs/zerolog/log"
1311

14-
transactionPB "github.com/xmlking/micro-starter-kit/service/recorder/proto/transaction"
15-
"github.com/xmlking/micro-starter-kit/shared/constants"
12+
transactionPB "github.com/xmlking/micro-starter-kit/service/recorder/proto/transaction"
1613
)
1714

1815
func publish(ctx context.Context, publisher micro.Event, req, rsp proto.Message) (err error) {
@@ -37,9 +34,6 @@ func publish(ctx context.Context, publisher micro.Event, req, rsp proto.Message)
3734
func NewHandlerWrapper(p micro.Event) server.HandlerWrapper {
3835
return func(fn server.HandlerFunc) server.HandlerFunc {
3936
return func(ctx context.Context, req server.Request, rsp interface{}) (err error) {
40-
// add TranID to context if not present
41-
// ctx = metadata.Set(ctx, constants.TransID, uuid.New().String())
42-
ctx = metadata.MergeContext(ctx, map[string]string{constants.TransID: uuid.New().String()}, false)
4337
err = fn(ctx, req, rsp)
4438
// we already logged error in Publish. lets ignore error here. # Note: this is blocking call..
4539
_ = publish(ctx, p, req.Body().(proto.Message), rsp.(proto.Message))
@@ -53,8 +47,6 @@ func NewHandlerWrapper(p micro.Event) server.HandlerWrapper {
5347
func NewSubscriberWrapper(p micro.Event) server.SubscriberWrapper {
5448
return func(fn server.SubscriberFunc) server.SubscriberFunc {
5549
return func(ctx context.Context, req server.Message) (err error) {
56-
// add TranID to context if not present
57-
ctx = metadata.MergeContext(ctx, map[string]string{constants.TransID: uuid.New().String()}, false)
5850
err = fn(ctx, req)
5951
// we already logged error in Publish. lets ignore error here.
6052
// FIXME: `Micro-From-Service` is not replaced

0 commit comments

Comments
 (0)