github.com/songzhibin97/gkit@v1.2.13/trace/transport_http.go (about) 1 package trace 2 3 import ( 4 "context" 5 "net/http" 6 ) 7 8 // Transport is an HTTP transport. 9 type Transport struct { 10 endpoint string 11 operation string 12 reqHeader headerCarrier 13 replyHeader headerCarrier 14 request *http.Request 15 pathTemplate string 16 } 17 18 // Kind returns the transport kind. 19 func (tr *Transport) Kind() Kind { 20 return KindHTTP 21 } 22 23 // Endpoint returns the transport endpoint. 24 func (tr *Transport) Endpoint() string { 25 return tr.endpoint 26 } 27 28 // Operation returns the transport operation. 29 func (tr *Transport) Operation() string { 30 return tr.operation 31 } 32 33 // Request returns the HTTP request. 34 func (tr *Transport) Request() *http.Request { 35 return tr.request 36 } 37 38 // RequestHeader returns the request header. 39 func (tr *Transport) RequestHeader() Header { 40 return tr.reqHeader 41 } 42 43 // ResponseHeader returns the reply header. 44 func (tr *Transport) ResponseHeader() Header { 45 return tr.replyHeader 46 } 47 48 // PathTemplate returns the http path template. 49 func (tr *Transport) PathTemplate() string { 50 return tr.pathTemplate 51 } 52 53 // SetOperation sets the transport operation. 54 func SetOperation(ctx context.Context, op string) { 55 if tr, ok := FromServerTransportContext(ctx); ok { 56 if tr, ok := tr.(*Transport); ok { 57 tr.operation = op 58 } 59 } 60 } 61 62 type headerCarrier http.Header 63 64 // Get returns the value associated with the passed key. 65 func (hc headerCarrier) Get(key string) string { 66 return http.Header(hc).Get(key) 67 } 68 69 // Set stores the key-value pair. 70 func (hc headerCarrier) Set(key string, value string) { 71 http.Header(hc).Set(key, value) 72 } 73 74 // Keys lists the keys stored in this carrier. 75 func (hc headerCarrier) Keys() []string { 76 keys := make([]string, 0, len(hc)) 77 for k := range http.Header(hc) { 78 keys = append(keys, k) 79 } 80 return keys 81 }