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)
14
15// List the specified metrics. You can use the returned metrics with GetMetricData
16// (https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricData.html)
17// or GetMetricStatistics
18// (https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html)
19// to obtain statistical data. Up to 500 results are returned for any one call. To
20// retrieve additional results, use the returned token with subsequent calls. After
21// you create a metric, allow up to 15 minutes before the metric appears. You can
22// see statistics about the metric sooner by using GetMetricData
23// (https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricData.html)
24// or GetMetricStatistics
25// (https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html).
26// ListMetrics doesn't return information about metrics if those metrics haven't
27// reported data in the past two weeks. To retrieve those metrics, use
28// GetMetricData
29// (https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricData.html)
30// or GetMetricStatistics
31// (https://docs.aws.amazon.com/AmazonCloudWatch/latest/APIReference/API_GetMetricStatistics.html).
32func (c *Client) ListMetrics(ctx context.Context, params *ListMetricsInput, optFns ...func(*Options)) (*ListMetricsOutput, error) {
33	if params == nil {
34		params = &ListMetricsInput{}
35	}
36
37	result, metadata, err := c.invokeOperation(ctx, "ListMetrics", params, optFns, c.addOperationListMetricsMiddlewares)
38	if err != nil {
39		return nil, err
40	}
41
42	out := result.(*ListMetricsOutput)
43	out.ResultMetadata = metadata
44	return out, nil
45}
46
47type ListMetricsInput struct {
48
49	// The dimensions to filter against. Only the dimensions that match exactly will be
50	// returned.
51	Dimensions []types.DimensionFilter
52
53	// The name of the metric to filter against. Only the metrics with names that match
54	// exactly will be returned.
55	MetricName *string
56
57	// The metric namespace to filter against. Only the namespace that matches exactly
58	// will be returned.
59	Namespace *string
60
61	// The token returned by a previous call to indicate that there is more data
62	// available.
63	NextToken *string
64
65	// To filter the results to show only metrics that have had data points published
66	// in the past three hours, specify this parameter with a value of PT3H. This is
67	// the only valid value for this parameter. The results that are returned are an
68	// approximation of the value you specify. There is a low probability that the
69	// returned results include metrics with last published data as much as 40 minutes
70	// more than the specified time interval.
71	RecentlyActive types.RecentlyActive
72
73	noSmithyDocumentSerde
74}
75
76type ListMetricsOutput struct {
77
78	// The metrics that match your request.
79	Metrics []types.Metric
80
81	// The token that marks the start of the next batch of returned results.
82	NextToken *string
83
84	// Metadata pertaining to the operation's result.
85	ResultMetadata middleware.Metadata
86
87	noSmithyDocumentSerde
88}
89
90func (c *Client) addOperationListMetricsMiddlewares(stack *middleware.Stack, options Options) (err error) {
91	err = stack.Serialize.Add(&awsAwsquery_serializeOpListMetrics{}, middleware.After)
92	if err != nil {
93		return err
94	}
95	err = stack.Deserialize.Add(&awsAwsquery_deserializeOpListMetrics{}, middleware.After)
96	if err != nil {
97		return err
98	}
99	if err = addSetLoggerMiddleware(stack, options); err != nil {
100		return err
101	}
102	if err = awsmiddleware.AddClientRequestIDMiddleware(stack); err != nil {
103		return err
104	}
105	if err = smithyhttp.AddComputeContentLengthMiddleware(stack); err != nil {
106		return err
107	}
108	if err = addResolveEndpointMiddleware(stack, options); err != nil {
109		return err
110	}
111	if err = v4.AddComputePayloadSHA256Middleware(stack); err != nil {
112		return err
113	}
114	if err = addRetryMiddlewares(stack, options); err != nil {
115		return err
116	}
117	if err = addHTTPSignerV4Middleware(stack, options); err != nil {
118		return err
119	}
120	if err = awsmiddleware.AddRawResponseToMetadata(stack); err != nil {
121		return err
122	}
123	if err = awsmiddleware.AddRecordResponseTiming(stack); err != nil {
124		return err
125	}
126	if err = addClientUserAgent(stack); err != nil {
127		return err
128	}
129	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
130		return err
131	}
132	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
133		return err
134	}
135	if err = addOpListMetricsValidationMiddleware(stack); err != nil {
136		return err
137	}
138	if err = stack.Initialize.Add(newServiceMetadataMiddleware_opListMetrics(options.Region), middleware.Before); err != nil {
139		return err
140	}
141	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
142		return err
143	}
144	if err = addResponseErrorMiddleware(stack); err != nil {
145		return err
146	}
147	if err = addRequestResponseLogging(stack, options); err != nil {
148		return err
149	}
150	return nil
151}
152
153// ListMetricsAPIClient is a client that implements the ListMetrics operation.
154type ListMetricsAPIClient interface {
155	ListMetrics(context.Context, *ListMetricsInput, ...func(*Options)) (*ListMetricsOutput, error)
156}
157
158var _ ListMetricsAPIClient = (*Client)(nil)
159
160// ListMetricsPaginatorOptions is the paginator options for ListMetrics
161type ListMetricsPaginatorOptions struct {
162	// Set to true if pagination should stop if the service returns a pagination token
163	// that matches the most recent token provided to the service.
164	StopOnDuplicateToken bool
165}
166
167// ListMetricsPaginator is a paginator for ListMetrics
168type ListMetricsPaginator struct {
169	options   ListMetricsPaginatorOptions
170	client    ListMetricsAPIClient
171	params    *ListMetricsInput
172	nextToken *string
173	firstPage bool
174}
175
176// NewListMetricsPaginator returns a new ListMetricsPaginator
177func NewListMetricsPaginator(client ListMetricsAPIClient, params *ListMetricsInput, optFns ...func(*ListMetricsPaginatorOptions)) *ListMetricsPaginator {
178	if params == nil {
179		params = &ListMetricsInput{}
180	}
181
182	options := ListMetricsPaginatorOptions{}
183
184	for _, fn := range optFns {
185		fn(&options)
186	}
187
188	return &ListMetricsPaginator{
189		options:   options,
190		client:    client,
191		params:    params,
192		firstPage: true,
193	}
194}
195
196// HasMorePages returns a boolean indicating whether more pages are available
197func (p *ListMetricsPaginator) HasMorePages() bool {
198	return p.firstPage || p.nextToken != nil
199}
200
201// NextPage retrieves the next ListMetrics page.
202func (p *ListMetricsPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*ListMetricsOutput, error) {
203	if !p.HasMorePages() {
204		return nil, fmt.Errorf("no more pages available")
205	}
206
207	params := *p.params
208	params.NextToken = p.nextToken
209
210	result, err := p.client.ListMetrics(ctx, &params, optFns...)
211	if err != nil {
212		return nil, err
213	}
214	p.firstPage = false
215
216	prevToken := p.nextToken
217	p.nextToken = result.NextToken
218
219	if p.options.StopOnDuplicateToken && prevToken != nil && p.nextToken != nil && *prevToken == *p.nextToken {
220		p.nextToken = nil
221	}
222
223	return result, nil
224}
225
226func newServiceMetadataMiddleware_opListMetrics(region string) *awsmiddleware.RegisterServiceMetadata {
227	return &awsmiddleware.RegisterServiceMetadata{
228		Region:        region,
229		ServiceID:     ServiceID,
230		SigningName:   "monitoring",
231		OperationName: "ListMetrics",
232	}
233}
234