...

Source file src/github.com/Azure/azure-sdk-for-go/services/preview/appinsights/v1/insights/metrics.go

Documentation: github.com/Azure/azure-sdk-for-go/services/preview/appinsights/v1/insights

     1  package insights
     2  
     3  // Copyright (c) Microsoft Corporation. All rights reserved.
     4  // Licensed under the MIT License. See License.txt in the project root for license information.
     5  //
     6  // Code generated by Microsoft (R) AutoRest Code Generator.
     7  // Changes may cause incorrect behavior and will be lost if the code is regenerated.
     8  
     9  import (
    10  	"context"
    11  	"github.com/Azure/go-autorest/autorest"
    12  	"github.com/Azure/go-autorest/autorest/azure"
    13  	"github.com/Azure/go-autorest/autorest/validation"
    14  	"github.com/Azure/go-autorest/tracing"
    15  	"net/http"
    16  )
    17  
    18  // MetricsClient is the composite Swagger for Application Insights Data Client
    19  type MetricsClient struct {
    20  	BaseClient
    21  }
    22  
    23  // NewMetricsClient creates an instance of the MetricsClient client.
    24  func NewMetricsClient() MetricsClient {
    25  	return NewMetricsClientWithBaseURI(DefaultBaseURI)
    26  }
    27  
    28  // NewMetricsClientWithBaseURI creates an instance of the MetricsClient client using a custom endpoint.  Use this when
    29  // interacting with an Azure cloud that uses a non-standard base URI (sovereign clouds, Azure stack).
    30  func NewMetricsClientWithBaseURI(baseURI string) MetricsClient {
    31  	return MetricsClient{NewWithBaseURI(baseURI)}
    32  }
    33  
    34  // Get gets metric values for a single metric
    35  // Parameters:
    36  // appID - ID of the application. This is Application ID from the API Access settings blade in the Azure
    37  // portal.
    38  // metricID - ID of the metric. This is either a standard AI metric, or an application-specific custom metric.
    39  // timespan - the timespan over which to retrieve metric values. This is an ISO8601 time period value. If
    40  // timespan is omitted, a default time range of `PT12H` ("last 12 hours") is used. The actual timespan that is
    41  // queried may be adjusted by the server based. In all cases, the actual time span used for the query is
    42  // included in the response.
    43  // interval - the time interval to use when retrieving metric values. This is an ISO8601 duration. If interval
    44  // is omitted, the metric value is aggregated across the entire timespan. If interval is supplied, the server
    45  // may adjust the interval to a more appropriate size based on the timespan used for the query. In all cases,
    46  // the actual interval used for the query is included in the response.
    47  // aggregation - the aggregation to use when computing the metric values. To retrieve more than one aggregation
    48  // at a time, separate them with a comma. If no aggregation is specified, then the default aggregation for the
    49  // metric is used.
    50  // segment - the name of the dimension to segment the metric values by. This dimension must be applicable to
    51  // the metric you are retrieving. To segment by more than one dimension at a time, separate them with a comma
    52  // (,). In this case, the metric data will be segmented in the order the dimensions are listed in the
    53  // parameter.
    54  // top - the number of segments to return.  This value is only valid when segment is specified.
    55  // orderby - the aggregation function and direction to sort the segments by.  This value is only valid when
    56  // segment is specified.
    57  // filter - an expression used to filter the results.  This value should be a valid OData filter expression
    58  // where the keys of each clause should be applicable dimensions for the metric you are retrieving.
    59  func (client MetricsClient) Get(ctx context.Context, appID string, metricID MetricID, timespan string, interval *string, aggregation []MetricsAggregation, segment []MetricsSegment, top *int32, orderby string, filter string) (result MetricsResult, err error) {
    60  	if tracing.IsEnabled() {
    61  		ctx = tracing.StartSpan(ctx, fqdn+"/MetricsClient.Get")
    62  		defer func() {
    63  			sc := -1
    64  			if result.Response.Response != nil {
    65  				sc = result.Response.Response.StatusCode
    66  			}
    67  			tracing.EndSpan(ctx, sc, err)
    68  		}()
    69  	}
    70  	if err := validation.Validate([]validation.Validation{
    71  		{TargetValue: aggregation,
    72  			Constraints: []validation.Constraint{{Target: "aggregation", Name: validation.Null, Rule: false,
    73  				Chain: []validation.Constraint{{Target: "aggregation", Name: validation.MinItems, Rule: 1, Chain: nil}}}}},
    74  		{TargetValue: segment,
    75  			Constraints: []validation.Constraint{{Target: "segment", Name: validation.Null, Rule: false,
    76  				Chain: []validation.Constraint{{Target: "segment", Name: validation.MinItems, Rule: 1, Chain: nil}}}}}}); err != nil {
    77  		return result, validation.NewError("insights.MetricsClient", "Get", err.Error())
    78  	}
    79  
    80  	req, err := client.GetPreparer(ctx, appID, metricID, timespan, interval, aggregation, segment, top, orderby, filter)
    81  	if err != nil {
    82  		err = autorest.NewErrorWithError(err, "insights.MetricsClient", "Get", nil, "Failure preparing request")
    83  		return
    84  	}
    85  
    86  	resp, err := client.GetSender(req)
    87  	if err != nil {
    88  		result.Response = autorest.Response{Response: resp}
    89  		err = autorest.NewErrorWithError(err, "insights.MetricsClient", "Get", resp, "Failure sending request")
    90  		return
    91  	}
    92  
    93  	result, err = client.GetResponder(resp)
    94  	if err != nil {
    95  		err = autorest.NewErrorWithError(err, "insights.MetricsClient", "Get", resp, "Failure responding to request")
    96  		return
    97  	}
    98  
    99  	return
   100  }
   101  
   102  // GetPreparer prepares the Get request.
   103  func (client MetricsClient) GetPreparer(ctx context.Context, appID string, metricID MetricID, timespan string, interval *string, aggregation []MetricsAggregation, segment []MetricsSegment, top *int32, orderby string, filter string) (*http.Request, error) {
   104  	pathParameters := map[string]interface{}{
   105  		"appId":    autorest.Encode("path", appID),
   106  		"metricId": autorest.Encode("path", metricID),
   107  	}
   108  
   109  	queryParameters := map[string]interface{}{}
   110  	if len(timespan) > 0 {
   111  		queryParameters["timespan"] = autorest.Encode("query", timespan)
   112  	}
   113  	if interval != nil {
   114  		queryParameters["interval"] = autorest.Encode("query", *interval)
   115  	}
   116  	if aggregation != nil && len(aggregation) > 0 {
   117  		queryParameters["aggregation"] = autorest.Encode("query", aggregation, ",")
   118  	}
   119  	if segment != nil && len(segment) > 0 {
   120  		queryParameters["segment"] = autorest.Encode("query", segment, ",")
   121  	}
   122  	if top != nil {
   123  		queryParameters["top"] = autorest.Encode("query", *top)
   124  	}
   125  	if len(orderby) > 0 {
   126  		queryParameters["orderby"] = autorest.Encode("query", orderby)
   127  	}
   128  	if len(filter) > 0 {
   129  		queryParameters["filter"] = autorest.Encode("query", filter)
   130  	}
   131  
   132  	preparer := autorest.CreatePreparer(
   133  		autorest.AsGet(),
   134  		autorest.WithBaseURL(client.BaseURI),
   135  		autorest.WithPathParameters("/apps/{appId}/metrics/{metricId}", pathParameters),
   136  		autorest.WithQueryParameters(queryParameters))
   137  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   138  }
   139  
   140  // GetSender sends the Get request. The method will close the
   141  // http.Response Body if it receives an error.
   142  func (client MetricsClient) GetSender(req *http.Request) (*http.Response, error) {
   143  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   144  }
   145  
   146  // GetResponder handles the response to the Get request. The method always
   147  // closes the http.Response Body.
   148  func (client MetricsClient) GetResponder(resp *http.Response) (result MetricsResult, err error) {
   149  	err = autorest.Respond(
   150  		resp,
   151  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   152  		autorest.ByUnmarshallingJSON(&result),
   153  		autorest.ByClosing())
   154  	result.Response = autorest.Response{Response: resp}
   155  	return
   156  }
   157  
   158  // GetMetadata gets metadata describing the available metrics
   159  // Parameters:
   160  // appID - ID of the application. This is Application ID from the API Access settings blade in the Azure
   161  // portal.
   162  func (client MetricsClient) GetMetadata(ctx context.Context, appID string) (result SetObject, err error) {
   163  	if tracing.IsEnabled() {
   164  		ctx = tracing.StartSpan(ctx, fqdn+"/MetricsClient.GetMetadata")
   165  		defer func() {
   166  			sc := -1
   167  			if result.Response.Response != nil {
   168  				sc = result.Response.Response.StatusCode
   169  			}
   170  			tracing.EndSpan(ctx, sc, err)
   171  		}()
   172  	}
   173  	req, err := client.GetMetadataPreparer(ctx, appID)
   174  	if err != nil {
   175  		err = autorest.NewErrorWithError(err, "insights.MetricsClient", "GetMetadata", nil, "Failure preparing request")
   176  		return
   177  	}
   178  
   179  	resp, err := client.GetMetadataSender(req)
   180  	if err != nil {
   181  		result.Response = autorest.Response{Response: resp}
   182  		err = autorest.NewErrorWithError(err, "insights.MetricsClient", "GetMetadata", resp, "Failure sending request")
   183  		return
   184  	}
   185  
   186  	result, err = client.GetMetadataResponder(resp)
   187  	if err != nil {
   188  		err = autorest.NewErrorWithError(err, "insights.MetricsClient", "GetMetadata", resp, "Failure responding to request")
   189  		return
   190  	}
   191  
   192  	return
   193  }
   194  
   195  // GetMetadataPreparer prepares the GetMetadata request.
   196  func (client MetricsClient) GetMetadataPreparer(ctx context.Context, appID string) (*http.Request, error) {
   197  	pathParameters := map[string]interface{}{
   198  		"appId": autorest.Encode("path", appID),
   199  	}
   200  
   201  	preparer := autorest.CreatePreparer(
   202  		autorest.AsGet(),
   203  		autorest.WithBaseURL(client.BaseURI),
   204  		autorest.WithPathParameters("/apps/{appId}/metrics/metadata", pathParameters))
   205  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   206  }
   207  
   208  // GetMetadataSender sends the GetMetadata request. The method will close the
   209  // http.Response Body if it receives an error.
   210  func (client MetricsClient) GetMetadataSender(req *http.Request) (*http.Response, error) {
   211  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   212  }
   213  
   214  // GetMetadataResponder handles the response to the GetMetadata request. The method always
   215  // closes the http.Response Body.
   216  func (client MetricsClient) GetMetadataResponder(resp *http.Response) (result SetObject, err error) {
   217  	err = autorest.Respond(
   218  		resp,
   219  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   220  		autorest.ByUnmarshallingJSON(&result.Value),
   221  		autorest.ByClosing())
   222  	result.Response = autorest.Response{Response: resp}
   223  	return
   224  }
   225  
   226  // GetMultiple gets metric values for multiple metrics
   227  // Parameters:
   228  // appID - ID of the application. This is Application ID from the API Access settings blade in the Azure
   229  // portal.
   230  // body - the batched metrics query.
   231  func (client MetricsClient) GetMultiple(ctx context.Context, appID string, body []MetricsPostBodySchema) (result ListMetricsResultsItem, err error) {
   232  	if tracing.IsEnabled() {
   233  		ctx = tracing.StartSpan(ctx, fqdn+"/MetricsClient.GetMultiple")
   234  		defer func() {
   235  			sc := -1
   236  			if result.Response.Response != nil {
   237  				sc = result.Response.Response.StatusCode
   238  			}
   239  			tracing.EndSpan(ctx, sc, err)
   240  		}()
   241  	}
   242  	if err := validation.Validate([]validation.Validation{
   243  		{TargetValue: body,
   244  			Constraints: []validation.Constraint{{Target: "body", Name: validation.Null, Rule: true, Chain: nil}}}}); err != nil {
   245  		return result, validation.NewError("insights.MetricsClient", "GetMultiple", err.Error())
   246  	}
   247  
   248  	req, err := client.GetMultiplePreparer(ctx, appID, body)
   249  	if err != nil {
   250  		err = autorest.NewErrorWithError(err, "insights.MetricsClient", "GetMultiple", nil, "Failure preparing request")
   251  		return
   252  	}
   253  
   254  	resp, err := client.GetMultipleSender(req)
   255  	if err != nil {
   256  		result.Response = autorest.Response{Response: resp}
   257  		err = autorest.NewErrorWithError(err, "insights.MetricsClient", "GetMultiple", resp, "Failure sending request")
   258  		return
   259  	}
   260  
   261  	result, err = client.GetMultipleResponder(resp)
   262  	if err != nil {
   263  		err = autorest.NewErrorWithError(err, "insights.MetricsClient", "GetMultiple", resp, "Failure responding to request")
   264  		return
   265  	}
   266  
   267  	return
   268  }
   269  
   270  // GetMultiplePreparer prepares the GetMultiple request.
   271  func (client MetricsClient) GetMultiplePreparer(ctx context.Context, appID string, body []MetricsPostBodySchema) (*http.Request, error) {
   272  	pathParameters := map[string]interface{}{
   273  		"appId": autorest.Encode("path", appID),
   274  	}
   275  
   276  	preparer := autorest.CreatePreparer(
   277  		autorest.AsContentType("application/json; charset=utf-8"),
   278  		autorest.AsPost(),
   279  		autorest.WithBaseURL(client.BaseURI),
   280  		autorest.WithPathParameters("/apps/{appId}/metrics", pathParameters),
   281  		autorest.WithJSON(body))
   282  	return preparer.Prepare((&http.Request{}).WithContext(ctx))
   283  }
   284  
   285  // GetMultipleSender sends the GetMultiple request. The method will close the
   286  // http.Response Body if it receives an error.
   287  func (client MetricsClient) GetMultipleSender(req *http.Request) (*http.Response, error) {
   288  	return client.Send(req, autorest.DoRetryForStatusCodes(client.RetryAttempts, client.RetryDuration, autorest.StatusCodesForRetry...))
   289  }
   290  
   291  // GetMultipleResponder handles the response to the GetMultiple request. The method always
   292  // closes the http.Response Body.
   293  func (client MetricsClient) GetMultipleResponder(resp *http.Response) (result ListMetricsResultsItem, err error) {
   294  	err = autorest.Respond(
   295  		resp,
   296  		azure.WithErrorUnlessStatusCode(http.StatusOK),
   297  		autorest.ByUnmarshallingJSON(&result.Value),
   298  		autorest.ByClosing())
   299  	result.Response = autorest.Response{Response: resp}
   300  	return
   301  }
   302  

View as plain text