1// Code generated by smithy-go-codegen DO NOT EDIT.
2
3package cloudwatch
4
5import (
6	"context"
7	"fmt"
8	awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware"
9	"github.com/aws/aws-sdk-go-v2/aws/signer/v4"
10	"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
11	"github.com/aws/smithy-go/middleware"
12	smithyhttp "github.com/aws/smithy-go/transport/http"
13	"time"
14)
15
16// You can use the GetMetricData API to retrieve as many as 500 different metrics
17// in a single request, with a total of as many as 100,800 data points. You can
18// also optionally perform math expressions on the values of the returned
19// statistics, to create new time series that represent new insights into your
20// data. For example, using Lambda metrics, you could divide the Errors metric by
21// the Invocations metric to get an error rate time series. For more information
22// about metric math expressions, see Metric Math Syntax and Functions
23// (https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/using-metric-math.html#metric-math-syntax)
24// in the Amazon CloudWatch User Guide. Calls to the GetMetricData API have a
25// different pricing structure than calls to GetMetricStatistics. For more
26// information about pricing, see Amazon CloudWatch Pricing
27// (https://aws.amazon.com/cloudwatch/pricing/). Amazon CloudWatch retains metric
28// data as follows:
29//
30// * Data points with a period of less than 60 seconds are
31// available for 3 hours. These data points are high-resolution metrics and are
32// available only for custom metrics that have been defined with a
33// StorageResolution of 1.
34//
35// * Data points with a period of 60 seconds (1-minute)
36// are available for 15 days.
37//
38// * Data points with a period of 300 seconds
39// (5-minute) are available for 63 days.
40//
41// * Data points with a period of 3600
42// seconds (1 hour) are available for 455 days (15 months).
43//
44// Data points that are
45// initially published with a shorter period are aggregated together for long-term
46// storage. For example, if you collect data using a period of 1 minute, the data
47// remains available for 15 days with 1-minute resolution. After 15 days, this data
48// is still available, but is aggregated and retrievable only with a resolution of
49// 5 minutes. After 63 days, the data is further aggregated and is available with a
50// resolution of 1 hour. If you omit Unit in your request, all data that was
51// collected with any unit is returned, along with the corresponding units that
52// were specified when the data was reported to CloudWatch. If you specify a unit,
53// the operation returns only data that was collected with that unit specified. If
54// you specify a unit that does not match the data collected, the results of the
55// operation are null. CloudWatch does not perform unit conversions.
56func (c *Client) GetMetricData(ctx context.Context, params *GetMetricDataInput, optFns ...func(*Options)) (*GetMetricDataOutput, error) {
57	if params == nil {
58		params = &GetMetricDataInput{}
59	}
60
61	result, metadata, err := c.invokeOperation(ctx, "GetMetricData", params, optFns, addOperationGetMetricDataMiddlewares)
62	if err != nil {
63		return nil, err
64	}
65
66	out := result.(*GetMetricDataOutput)
67	out.ResultMetadata = metadata
68	return out, nil
69}
70
71type GetMetricDataInput struct {
72
73	// The time stamp indicating the latest data to be returned. The value specified is
74	// exclusive; results include data points up to the specified time stamp. For
75	// better performance, specify StartTime and EndTime values that align with the
76	// value of the metric's Period and sync up with the beginning and end of an hour.
77	// For example, if the Period of a metric is 5 minutes, specifying 12:05 or 12:30
78	// as EndTime can get a faster response from CloudWatch than setting 12:07 or 12:29
79	// as the EndTime.
80	//
81	// This member is required.
82	EndTime *time.Time
83
84	// The metric queries to be returned. A single GetMetricData call can include as
85	// many as 500 MetricDataQuery structures. Each of these structures can specify
86	// either a metric to retrieve, or a math expression to perform on retrieved data.
87	//
88	// This member is required.
89	MetricDataQueries []types.MetricDataQuery
90
91	// The time stamp indicating the earliest data to be returned. The value specified
92	// is inclusive; results include data points with the specified time stamp.
93	// CloudWatch rounds the specified time stamp as follows:
94	//
95	// * Start time less than
96	// 15 days ago - Round down to the nearest whole minute. For example, 12:32:34 is
97	// rounded down to 12:32:00.
98	//
99	// * Start time between 15 and 63 days ago - Round down
100	// to the nearest 5-minute clock interval. For example, 12:32:34 is rounded down to
101	// 12:30:00.
102	//
103	// * Start time greater than 63 days ago - Round down to the nearest
104	// 1-hour clock interval. For example, 12:32:34 is rounded down to 12:00:00.
105	//
106	// If
107	// you set Period to 5, 10, or 30, the start time of your request is rounded down
108	// to the nearest time that corresponds to even 5-, 10-, or 30-second divisions of
109	// a minute. For example, if you make a query at (HH:mm:ss) 01:05:23 for the
110	// previous 10-second period, the start time of your request is rounded down and
111	// you receive data from 01:05:10 to 01:05:20. If you make a query at 15:07:17 for
112	// the previous 5 minutes of data, using a period of 5 seconds, you receive data
113	// timestamped between 15:02:15 and 15:07:15. For better performance, specify
114	// StartTime and EndTime values that align with the value of the metric's Period
115	// and sync up with the beginning and end of an hour. For example, if the Period of
116	// a metric is 5 minutes, specifying 12:05 or 12:30 as StartTime can get a faster
117	// response from CloudWatch than setting 12:07 or 12:29 as the StartTime.
118	//
119	// This member is required.
120	StartTime *time.Time
121
122	// This structure includes the Timezone parameter, which you can use to specify
123	// your time zone so that the labels of returned data display the correct time for
124	// your time zone.
125	LabelOptions *types.LabelOptions
126
127	// The maximum number of data points the request should return before paginating.
128	// If you omit this, the default of 100,800 is used.
129	MaxDatapoints *int32
130
131	// Include this value, if it was returned by the previous GetMetricData operation,
132	// to get the next set of data points.
133	NextToken *string
134
135	// The order in which data points should be returned. TimestampDescending returns
136	// the newest data first and paginates when the MaxDatapoints limit is reached.
137	// TimestampAscending returns the oldest data first and paginates when the
138	// MaxDatapoints limit is reached.
139	ScanBy types.ScanBy
140}
141
142type GetMetricDataOutput struct {
143
144	// Contains a message about this GetMetricData operation, if the operation results
145	// in such a message. An example of a message that might be returned is Maximum
146	// number of allowed metrics exceeded. If there is a message, as much of the
147	// operation as possible is still executed. A message appears here only if it is
148	// related to the global GetMetricData operation. Any message about a specific
149	// metric returned by the operation appears in the MetricDataResult object returned
150	// for that metric.
151	Messages []types.MessageData
152
153	// The metrics that are returned, including the metric name, namespace, and
154	// dimensions.
155	MetricDataResults []types.MetricDataResult
156
157	// A token that marks the next batch of returned results.
158	NextToken *string
159
160	// Metadata pertaining to the operation's result.
161	ResultMetadata middleware.Metadata
162}
163
164func addOperationGetMetricDataMiddlewares(stack *middleware.Stack, options Options) (err error) {
165	err = stack.Serialize.Add(&awsAwsquery_serializeOpGetMetricData{}, middleware.After)
166	if err != nil {
167		return err
168	}
169	err = stack.Deserialize.Add(&awsAwsquery_deserializeOpGetMetricData{}, middleware.After)
170	if err != nil {
171		return err
172	}
173	if err = addSetLoggerMiddleware(stack, options); err != nil {
174		return err
175	}
176	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
177		return err
178	}
179	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
180		return err
181	}
182	if err = addResolveEndpointMiddleware(stack, options); err != nil {
183		return err
184	}
185	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
186		return err
187	}
188	if err = addRetryMiddlewares(stack, options); err != nil {
189		return err
190	}
191	if err = addHTTPSignerV4Middleware(stack, options); err != nil {
192		return err
193	}
194	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
195		return err
196	}
197	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
198		return err
199	}
200	if err = addClientUserAgent(stack); err != nil {
201		return err
202	}
203	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
204		return err
205	}
206	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
207		return err
208	}
209	if err = addOpGetMetricDataValidationMiddleware(stack); err != nil {
210		return err
211	}
212	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opGetMetricData(options.Region), middleware.Before); err != nil {
213		return err
214	}
215	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
216		return err
217	}
218	if err = addResponseErrorMiddleware(stack); err != nil {
219		return err
220	}
221	if err = addRequestResponseLogging(stack, options); err != nil {
222		return err
223	}
224	return nil
225}
226
227// GetMetricDataAPIClient is a client that implements the GetMetricData operation.
228type GetMetricDataAPIClient interface {
229	GetMetricData(context.Context, *GetMetricDataInput, ...func(*Options)) (*GetMetricDataOutput, error)
230}
231
232var _ GetMetricDataAPIClient = (*Client)(nil)
233
234// GetMetricDataPaginatorOptions is the paginator options for GetMetricData
235type GetMetricDataPaginatorOptions struct {
236	// The maximum number of data points the request should return before paginating.
237	// If you omit this, the default of 100,800 is used.
238	Limit int32
239
240	// Set to true if pagination should stop if the service returns a pagination token
241	// that matches the most recent token provided to the service.
242	StopOnDuplicateToken bool
243}
244
245// GetMetricDataPaginator is a paginator for GetMetricData
246type GetMetricDataPaginator struct {
247	options   GetMetricDataPaginatorOptions
248	client    GetMetricDataAPIClient
249	params    *GetMetricDataInput
250	nextToken *string
251	firstPage bool
252}
253
254// NewGetMetricDataPaginator returns a new GetMetricDataPaginator
255func NewGetMetricDataPaginator(client GetMetricDataAPIClient, params *GetMetricDataInput, optFns ...func(*GetMetricDataPaginatorOptions)) *GetMetricDataPaginator {
256	if params == nil {
257		params = &GetMetricDataInput{}
258	}
259
260	options := GetMetricDataPaginatorOptions{}
261	if params.MaxDatapoints != nil {
262		options.Limit = *params.MaxDatapoints
263	}
264
265	for _, fn := range optFns {
266		fn(&options)
267	}
268
269	return &GetMetricDataPaginator{
270		options:   options,
271		client:    client,
272		params:    params,
273		firstPage: true,
274	}
275}
276
277// HasMorePages returns a boolean indicating whether more pages are available
278func (p *GetMetricDataPaginator) HasMorePages() bool {
279	return p.firstPage || p.nextToken != nil
280}
281
282// NextPage retrieves the next GetMetricData page.
283func (p *GetMetricDataPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*GetMetricDataOutput, error) {
284	if !p.HasMorePages() {
285		return nil, fmt.Errorf("no more pages available")
286	}
287
288	params := *p.params
289	params.NextToken = p.nextToken
290
291	var limit *int32
292	if p.options.Limit > 0 {
293		limit = &p.options.Limit
294	}
295	params.MaxDatapoints = limit
296
297	result, err := p.client.GetMetricData(ctx, &params, optFns...)
298	if err != nil {
299		return nil, err
300	}
301	p.firstPage = false
302
303	prevToken := p.nextToken
304	p.nextToken = result.NextToken
305
306	if p.options.StopOnDuplicateToken && prevToken != nil && p.nextToken != nil && *prevToken == *p.nextToken {
307		p.nextToken = nil
308	}
309
310	return result, nil
311}
312
313func newServiceMetadataMiddleware_opGetMetricData(region string) *awsmiddleware.RegisterServiceMetadata {
314	return &awsmiddleware.RegisterServiceMetadata{
315		Region:        region,
316		ServiceID:     ServiceID,
317		SigningName:   "monitoring",
318		OperationName: "GetMetricData",
319	}
320}
321