This repository has been archived by the owner on Sep 8, 2023. It is now read-only.
/
xray_client.go
80 lines (67 loc) · 2.65 KB
/
xray_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright 2018-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with the License. A copy of the License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and limitations under the License.
package conn
import (
"os"
"strconv"
"strings"
"time"
"github.com/shogo82148/aws-xray-yadaemon/pkg/cfg"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/xray"
log "github.com/cihub/seelog"
)
// XRay defines X-Ray api call structure.
type XRay interface {
PutTraceSegments(input *xray.PutTraceSegmentsInput) (*xray.PutTraceSegmentsOutput, error)
PutTelemetryRecords(input *xray.PutTelemetryRecordsInput) (*xray.PutTelemetryRecordsOutput, error)
}
// XRayClient represents X-Ray client.
type XRayClient struct {
xRay *xray.XRay
}
// PutTraceSegments makes PutTraceSegments api call on X-Ray client.
func (c *XRayClient) PutTraceSegments(input *xray.PutTraceSegmentsInput) (*xray.PutTraceSegmentsOutput, error) {
return c.xRay.PutTraceSegments(input)
}
// PutTelemetryRecords makes PutTelemetryRecords api call on X-Ray client.
func (c *XRayClient) PutTelemetryRecords(input *xray.PutTelemetryRecordsInput) (*xray.PutTelemetryRecordsOutput, error) {
return c.xRay.PutTelemetryRecords(input)
}
// NewXRay creates a new instance of the XRay client with a aws configuration and session .
func NewXRay(awsConfig *aws.Config, s *session.Session) XRay {
x := xray.New(s, awsConfig)
log.Debugf("Using Endpoint: %s", x.Endpoint)
x.Handlers.Build.PushBackNamed(request.NamedHandler{
Name: "tracing.XRayVersionUserAgentHandler",
Fn: request.MakeAddToUserAgentHandler("xray", cfg.Version, os.Getenv("AWS_EXECUTION_ENV")),
})
x.Handlers.Sign.PushFrontNamed(request.NamedHandler{
Name: "tracing.TimestampHandler",
Fn: func(r *request.Request) {
r.HTTPRequest.Header.Set("X-Amzn-Xray-Timestamp", strconv.FormatFloat(float64(time.Now().UnixNano())/float64(time.Second), 'f', 9, 64))
},
})
return &XRayClient{
xRay: x,
}
}
// IsTimeoutError checks whether error is timeout error.
func IsTimeoutError(err error) bool {
awsError, ok := err.(awserr.Error)
if ok {
if strings.Contains(awsError.Error(), "net/http: request canceled") {
return true
}
}
return false
}