Skip to content

Commit a83b21c

Browse files
committed
feat: trace when sending notidy
1 parent d515e84 commit a83b21c

File tree

4 files changed

+15
-29
lines changed

4 files changed

+15
-29
lines changed

notify/notify.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package notify
22

33
import (
4+
"context"
45
"errors"
56
"github.com/parnurzeal/gorequest"
7+
"go.opentelemetry.io/otel"
8+
"go.opentelemetry.io/otel/propagation"
69
"sync"
710
)
811

@@ -53,6 +56,7 @@ type SendResult struct {
5356
}
5457

5558
type Template struct {
59+
ctx context.Context
5660
StaffID []string `json:"StaffID"`
5761
WeChat *WeChatNoticeBody `json:"template,omitempty"`
5862
DingTalk *DingTalkNoticeBody `json:"dingtalk,omitempty"`
@@ -63,6 +67,11 @@ func New() *Template {
6367
return &Template{}
6468
}
6569

70+
func (t *Template) WithContext(ctx context.Context) *Template {
71+
t.ctx = ctx
72+
return t
73+
}
74+
6675
func (t *Template) Receiver(staffId ...string) *Template {
6776
t.StaffID = append(t.StaffID, staffId...)
6877
return t
@@ -95,8 +104,12 @@ func (t *Template) Send() error {
95104

96105
notifier := notifierPool.Get().(*gorequest.SuperAgent)
97106
defer notifierPool.Put(notifier)
98-
_, _, errs := notifier.Send(t).EndStruct(result)
99107

108+
// Add tracing information to the request header.
109+
propagator := otel.GetTextMapPropagator()
110+
propagator.Inject(t.ctx, propagation.HeaderCarrier(notifier.Header))
111+
112+
_, _, errs := notifier.Send(t).EndStruct(result)
100113
if errs != nil || result.Msg != "success" || result.Data.Failed != 0 {
101114
return errors.New("send notify failed")
102115
}

transfer/option.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package transfer
22

33
import (
44
"github.com/pkg/errors"
5-
"go.opentelemetry.io/otel/propagation"
65
"log"
76
"net"
87
"net/url"
@@ -52,15 +51,3 @@ func (s Cache) apply(request *Request) {
5251
request.SuperAgent.Header.Del("x-hduhelp-cache")
5352
}
5453
}
55-
56-
type PropagatorManager struct {
57-
propagator propagation.TextMapPropagator
58-
}
59-
60-
func WithPropagator(propagator propagation.TextMapPropagator) interface{ apply() } {
61-
return PropagatorManager{propagator: propagator}
62-
}
63-
64-
func (p PropagatorManager) apply() {
65-
instance.propagator = p.propagator
66-
}

transfer/service.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
package transfer
22

3-
import (
4-
"go.opentelemetry.io/otel"
5-
"go.opentelemetry.io/otel/propagation"
6-
)
7-
83
var instance *Server
94

105
type Server struct {
@@ -13,8 +8,6 @@ type Server struct {
138
endpoint string
149

1510
debug bool
16-
17-
propagator propagation.TextMapPropagator
1811
}
1912

2013
func Init(appID, appKey string, options ...interface {
@@ -38,10 +31,3 @@ func defaultServer() *Server {
3831
endpoint: "https://api.hduhelp.com/transfer",
3932
}
4033
}
41-
42-
func (s *Server) getPropagator() propagation.TextMapPropagator {
43-
if s.propagator == nil {
44-
return otel.GetTextMapPropagator()
45-
}
46-
return s.propagator
47-
}

transfer/transfer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (r *Request) EndStruct(data interface{}) error {
141141
defer span.End()
142142

143143
r.make() // 会把 SuperAgent 清空
144-
instance.getPropagator().Inject(newCtx, propagation.HeaderCarrier(r.SuperAgent.Header))
144+
otel.GetTextMapPropagator().Inject(newCtx, propagation.HeaderCarrier(r.SuperAgent.Header))
145145

146146
var bytes []byte
147147
r.ResponseData = new(Response)

0 commit comments

Comments
 (0)