1
- // +build metrics
2
-
3
1
package metrics
4
2
5
3
import (
6
- "errors"
7
4
"fmt"
8
5
"log"
9
6
"os"
10
7
"strings"
11
8
"time"
12
9
13
- "github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/cfnerr"
14
10
"github.com/aws-cloudformation/cloudformation-cli-go-plugin/cfn/logging"
15
11
"github.com/aws/aws-sdk-go/aws"
16
12
"github.com/aws/aws-sdk-go/service/cloudwatch"
@@ -30,108 +26,76 @@ const (
30
26
DimensionKeyAcionType = "Action"
31
27
//DimensionKeyExceptionType is the ExceptionType in the dimension.
32
28
DimensionKeyExceptionType = "ExceptionType"
33
- //DimensionKeyResouceType is the ResourceType in the dimension.
34
- DimensionKeyResouceType = "ResourceType"
29
+ //DimensionKeyResourceType is the ResourceType in the dimension.
30
+ DimensionKeyResourceType = "ResourceType"
35
31
//ServiceInternalError ...
36
32
ServiceInternalError string = "ServiceInternal"
37
33
)
38
34
39
35
// A Publisher represents an object that publishes metrics to AWS Cloudwatch.
40
36
type Publisher struct {
41
- client cloudwatchiface.CloudWatchAPI // AWS CloudWatch Service Client
42
- namespace string // custom resouces's namespace
43
- logger * log.Logger
37
+ client cloudwatchiface.CloudWatchAPI // AWS CloudWatch Service Client
38
+ namespace string // custom resouces's namespace
39
+ logger * log.Logger
40
+ resourceType string // type of resource
44
41
}
45
42
46
43
// New creates a new Publisher.
47
- func New (client cloudwatchiface.CloudWatchAPI ) * Publisher {
44
+ func New (client cloudwatchiface.CloudWatchAPI , account string , resType string ) * Publisher {
48
45
if len (os .Getenv ("AWS_SAM_LOCAL" )) > 0 {
49
46
client = newNoopClient ()
50
47
}
51
48
49
+ rn := ResourceTypeName (resType )
52
50
return & Publisher {
53
- client : client ,
54
- logger : logging .New ("metrics" ),
51
+ client : client ,
52
+ logger : logging .New ("metrics" ),
53
+ namespace : fmt .Sprintf ("%s/%s/%s" , MetricNameSpaceRoot , account , rn ),
54
+ resourceType : rn ,
55
55
}
56
56
}
57
57
58
58
// PublishExceptionMetric publishes an exception metric.
59
- func (p * Publisher ) PublishExceptionMetric (date time.Time , action string , e error ) error {
60
-
61
- if len (p .namespace ) == 0 {
62
- message := fmt .Sprintf ("Name Space was not set" )
63
- err := errors .New (message )
64
- return cfnerr .New (ServiceInternalError , "Publisher error" , err )
65
- }
66
-
59
+ func (p * Publisher ) PublishExceptionMetric (date time.Time , action string , e error ) {
67
60
dimensions := map [string ]string {
68
61
DimensionKeyAcionType : string (action ),
69
62
DimensionKeyExceptionType : e .Error (),
70
- DimensionKeyResouceType : p .namespace ,
71
- }
72
-
73
- _ , err := p .publishMetric (MetricNameHanderException , dimensions , cloudwatch .StandardUnitCount , 1.0 , date )
74
-
75
- if err != nil {
76
- return cfnerr .New (ServiceInternalError , "Publisher error" , err )
63
+ DimensionKeyResourceType : p .resourceType ,
77
64
}
78
65
79
- return nil
66
+ p . publishMetric ( MetricNameHanderException , dimensions , cloudwatch . StandardUnitCount , 1.0 , date )
80
67
}
81
68
82
69
// PublishInvocationMetric publishes an invocation metric.
83
- func (p * Publisher ) PublishInvocationMetric (date time.Time , action string ) error {
84
-
85
- if len (p .namespace ) == 0 {
86
- message := fmt .Sprintf ("Name Space was not set" )
87
- err := errors .New (message )
88
- return cfnerr .New (ServiceInternalError , "Publisher error" , err )
89
- }
90
-
70
+ func (p * Publisher ) PublishInvocationMetric (date time.Time , action string ) {
91
71
dimensions := map [string ]string {
92
- DimensionKeyAcionType : string (action ),
93
- DimensionKeyResouceType : p .namespace ,
72
+ DimensionKeyAcionType : string (action ),
73
+ DimensionKeyResourceType : p .resourceType ,
94
74
}
95
75
96
- _ , err := p .publishMetric (MetricNameHanderInvocationCount , dimensions , cloudwatch .StandardUnitCount , 1.0 , date )
76
+ p .publishMetric (MetricNameHanderInvocationCount , dimensions , cloudwatch .StandardUnitCount , 1.0 , date )
97
77
98
- if err != nil {
99
- return cfnerr .New (ServiceInternalError , "Publisher error" , err )
100
- }
101
-
102
- return nil
103
78
}
104
79
105
80
// PublishDurationMetric publishes an duration metric.
106
81
//
107
82
// A duration metric is the timing of something.
108
- func (p * Publisher ) PublishDurationMetric (date time.Time , action string , secs float64 ) error {
109
- if len (p .namespace ) == 0 {
110
- message := fmt .Sprintf ("Name Space was not set" )
111
- err := errors .New (message )
112
- return cfnerr .New (ServiceInternalError , "Publisher error" , err )
113
- }
83
+ func (p * Publisher ) PublishDurationMetric (date time.Time , action string , secs float64 ) {
114
84
dimensions := map [string ]string {
115
- DimensionKeyAcionType : string (action ),
116
- DimensionKeyResouceType : p .namespace ,
85
+ DimensionKeyAcionType : string (action ),
86
+ DimensionKeyResourceType : p .resourceType ,
117
87
}
118
88
119
- _ , err := p .publishMetric (MetricNameHanderDuration , dimensions , cloudwatch .StandardUnitMilliseconds , secs , date )
89
+ p .publishMetric (MetricNameHanderDuration , dimensions , cloudwatch .StandardUnitMilliseconds , secs , date )
120
90
121
- if err != nil {
122
- return cfnerr .New (ServiceInternalError , "Publisher error" , err )
123
- }
124
-
125
- return nil
126
91
}
127
92
128
- func (p * Publisher ) publishMetric (metricName string , data map [string ]string , unit string , value float64 , date time.Time ) ( * cloudwatch. PutMetricDataOutput , error ) {
93
+ func (p * Publisher ) publishMetric (metricName string , data map [string ]string , unit string , value float64 , date time.Time ) {
129
94
130
95
var d []* cloudwatch.Dimension
131
96
132
97
for k , v := range data {
133
98
dim := & cloudwatch.Dimension {
134
-
135
99
Name : aws .String (k ),
136
100
Value : aws .String (v ),
137
101
}
@@ -152,25 +116,22 @@ func (p *Publisher) publishMetric(metricName string, data map[string]string, uni
152
116
MetricData : md ,
153
117
}
154
118
155
- out , err := p .client .PutMetricData (& pi )
119
+ _ , err := p .client .PutMetricData (& pi )
156
120
157
121
if err != nil {
122
+ p .logger .Printf ("An error occurred while publishing metrics: %s" , err )
158
123
159
- return nil , cfnerr .New (ServiceInternalError , "Publisher error" , err )
160
124
}
161
-
162
- return out , nil
163
125
}
164
126
165
- // SetResourceTypeName returns a type name by removing (::) and replaing with (/)
127
+ // ResourceTypeName returns a type name by removing (::) and replaing with (/)
166
128
//
167
129
// Example
168
130
//
169
- // pub := metrics.New(cw )
131
+ // r := metrics.ResourceTypeName("AWS::Service::Resource" )
170
132
//
171
133
// // Will return "AWS/Service/Resource"
172
- // pub.SetResourceTypeName("AWS::Service::Resource")
173
- func (p * Publisher ) SetResourceTypeName (t string ) {
174
- p .namespace = strings .ReplaceAll (t , "::" , "/" )
134
+ func ResourceTypeName (t string ) string {
135
+ return strings .ReplaceAll (t , "::" , "/" )
175
136
176
137
}
0 commit comments