Example #1
0
        /// <summary>
        /// Create a new performance counter metric object from the provided raw data packet
        /// </summary>
        /// <remarks>The metric definition will automatically be added to the provided collection.</remarks>
        /// <param name="definitions">The definitions dictionary this definition is a part of</param>
        /// <param name="packet">The raw data packet</param>
        internal PerfCounterMetricDefinition(MetricDefinitionCollection definitions, PerfCounterMetricDefinitionPacket packet)
            : base(definitions, packet)
        {
            m_RequiresMultipleSamples = CounterRequiresMultipleSamples(packet.CounterType);

            CalculateIsPercentage();
        }
Example #2
0
        /// <summary>
        /// Create a new performance counter metric definition from the provided performance counter.
        /// </summary>
        /// <remarks>The metric definition will automatically be added to the provided collection.</remarks>
        /// <param name="definitions">The definitions collection to add this definition to.</param>
        /// <param name="newPerformanceCounter">The windows performance counter to add a definition for</param>
        public PerfCounterMetricDefinition(MetricDefinitionCollection definitions, PerformanceCounter newPerformanceCounter)
            : base(definitions, new PerfCounterMetricDefinitionPacket(newPerformanceCounter))
        {
            m_RequiresMultipleSamples = CounterRequiresMultipleSamples(newPerformanceCounter.CounterType);

            CalculateIsPercentage();
        }
 private static void AreEqual(MetricDefinitionCollection exp, MetricDefinitionCollection act)
 {
     if (exp != null)
     {
         for (int i = 0; i < exp.Value.Count; i++)
         {
             AreEqual(exp.Value[i], act.Value[i]);
         }
     }
 }
Example #4
0
        public async Task <Response <MetricDefinitionCollection> > ListAsync(string resourceUri, string metricnamespace = null, CancellationToken cancellationToken = default)
        {
            Argument.AssertNotNull(resourceUri, nameof(resourceUri));

            using var message = CreateListRequest(resourceUri, metricnamespace);
            await _pipeline.SendAsync(message, cancellationToken).ConfigureAwait(false);

            switch (message.Response.Status)
            {
            case 200:
            {
                MetricDefinitionCollection value = default;
                using var document = await JsonDocument.ParseAsync(message.Response.ContentStream, default, cancellationToken).ConfigureAwait(false);

                value = MetricDefinitionCollection.DeserializeMetricDefinitionCollection(document.RootElement);
                return(Response.FromValue(value, message.Response));
            }
        public void GetMetricDefinitionsTest()
        {
            MetricDefinitionCollection expectedMetricDefinitionCollection = GetMetricDefinitionCollection(ResourceUri);

            var response = new HttpResponseMessage(HttpStatusCode.OK)
            {
                Content = new StringContent(expectedMetricDefinitionCollection.ToJson())
            };

            RecordedDelegatingHandler handler = new RecordedDelegatingHandler(response);
            var insightsClient = GetInsightsClient(handler);

            string filterString = "names eq 'CpuPercentage'";
            MetricDefinitionListResponse actualMetricDefinitions =
                (insightsClient.MetricDefinitionOperations as MetricDefinitionOperations)
                .GetMetricDefinitionsAsync(
                    ResourceUri,
                    filterString,
                    new CancellationToken()).Result;

            AreEqual(expectedMetricDefinitionCollection, actualMetricDefinitions.MetricDefinitionCollection);
        }
Example #6
0
        /// <summary>
        /// The List Metric Definitions operation lists the metric definitions
        /// for the resource.
        /// </summary>
        /// <param name='resourceId'>
        /// Required. The id of the resource.The resource id can be built using
        /// the resource id builder class under utilities
        /// </param>
        /// <param name='metricNames'>
        /// Optional. The names of the metrics.
        /// </param>
        /// <param name='metricNamespace'>
        /// Required. The namespace of the metrics.The value is either null or
        /// WindowsAzure.Availability.WindowsAzure.Availability returns the
        /// metric definitions for endpoint monitoring metrics
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The List Metric Definitions operation response.
        /// </returns>
        public async System.Threading.Tasks.Task <Microsoft.WindowsAzure.Management.Monitoring.Metrics.Models.MetricDefinitionListResponse> ListAsync(string resourceId, IList <string> metricNames, string metricNamespace, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceId == null)
            {
                throw new ArgumentNullException("resourceId");
            }

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = Tracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("resourceId", resourceId);
                tracingParameters.Add("metricNames", metricNames);
                tracingParameters.Add("metricNamespace", metricNamespace);
                Tracing.Enter(invocationId, this, "ListAsync", tracingParameters);
            }

            // Construct URL
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            string url     = "/" + (this.Client.Credentials.SubscriptionId != null ? this.Client.Credentials.SubscriptionId.Trim() : "") + "/services/monitoring/metricdefinitions/query?";

            url = url + "&resourceId=" + Uri.EscapeUriString(resourceId.Trim());
            if (metricNamespace != null)
            {
                url = url + "&namespace=" + Uri.EscapeUriString(metricNamespace != null ? metricNamespace.Trim() : "");
            }
            if (metricNames != null && metricNames.Count > 0)
            {
                url = url + "&names=" + Uri.EscapeUriString(string.Join(",", metricNames));
            }
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("Accept", "application/json");
                httpRequest.Headers.Add("x-ms-version", "2013-10-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        Tracing.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        Tracing.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false), CloudExceptionType.Json);
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    MetricDefinitionListResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new MetricDefinitionListResponse();
                    JToken responseDoc = null;
                    if (string.IsNullOrEmpty(responseContent) == false)
                    {
                        responseDoc = JToken.Parse(responseContent);
                    }

                    if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                    {
                        MetricDefinitionCollection metricDefinitionCollectionInstance = new MetricDefinitionCollection();
                        result.MetricDefinitionCollection = metricDefinitionCollectionInstance;

                        JToken valueArray = responseDoc["Value"];
                        if (valueArray != null && valueArray.Type != JTokenType.Null)
                        {
                            foreach (JToken valueValue in ((JArray)valueArray))
                            {
                                MetricDefinition metricDefinitionInstance = new MetricDefinition();
                                metricDefinitionCollectionInstance.Value.Add(metricDefinitionInstance);

                                JToken nameValue = valueValue["Name"];
                                if (nameValue != null && nameValue.Type != JTokenType.Null)
                                {
                                    string nameInstance = ((string)nameValue);
                                    metricDefinitionInstance.Name = nameInstance;
                                }

                                JToken namespaceValue = valueValue["Namespace"];
                                if (namespaceValue != null && namespaceValue.Type != JTokenType.Null)
                                {
                                    string namespaceInstance = ((string)namespaceValue);
                                    metricDefinitionInstance.Namespace = namespaceInstance;
                                }

                                JToken resourceIdSuffixValue = valueValue["ResourceIdSuffix"];
                                if (resourceIdSuffixValue != null && resourceIdSuffixValue.Type != JTokenType.Null)
                                {
                                    string resourceIdSuffixInstance = ((string)resourceIdSuffixValue);
                                    metricDefinitionInstance.ResourceIdSuffix = resourceIdSuffixInstance;
                                }

                                JToken displayNameValue = valueValue["DisplayName"];
                                if (displayNameValue != null && displayNameValue.Type != JTokenType.Null)
                                {
                                    string displayNameInstance = ((string)displayNameValue);
                                    metricDefinitionInstance.DisplayName = displayNameInstance;
                                }

                                JToken unitValue = valueValue["Unit"];
                                if (unitValue != null && unitValue.Type != JTokenType.Null)
                                {
                                    string unitInstance = ((string)unitValue);
                                    metricDefinitionInstance.Unit = unitInstance;
                                }

                                JToken primaryAggregationValue = valueValue["PrimaryAggregation"];
                                if (primaryAggregationValue != null && primaryAggregationValue.Type != JTokenType.Null)
                                {
                                    string primaryAggregationInstance = ((string)primaryAggregationValue);
                                    metricDefinitionInstance.PrimaryAggregation = primaryAggregationInstance;
                                }

                                JToken metricAvailabilitiesArray = valueValue["MetricAvailabilities"];
                                if (metricAvailabilitiesArray != null && metricAvailabilitiesArray.Type != JTokenType.Null)
                                {
                                    foreach (JToken metricAvailabilitiesValue in ((JArray)metricAvailabilitiesArray))
                                    {
                                        MetricAvailability metricAvailabilityInstance = new MetricAvailability();
                                        metricDefinitionInstance.MetricAvailabilities.Add(metricAvailabilityInstance);

                                        JToken timeGrainValue = metricAvailabilitiesValue["TimeGrain"];
                                        if (timeGrainValue != null && timeGrainValue.Type != JTokenType.Null)
                                        {
                                            TimeSpan timeGrainInstance = TypeConversion.From8601TimeSpan(((string)timeGrainValue));
                                            metricAvailabilityInstance.TimeGrain = timeGrainInstance;
                                        }

                                        JToken retentionValue = metricAvailabilitiesValue["Retention"];
                                        if (retentionValue != null && retentionValue.Type != JTokenType.Null)
                                        {
                                            TimeSpan retentionInstance = TypeConversion.From8601TimeSpan(((string)retentionValue));
                                            metricAvailabilityInstance.Retention = retentionInstance;
                                        }
                                    }
                                }

                                JToken minimumAlertableTimeWindowValue = valueValue["MinimumAlertableTimeWindow"];
                                if (minimumAlertableTimeWindowValue != null && minimumAlertableTimeWindowValue.Type != JTokenType.Null)
                                {
                                    TimeSpan minimumAlertableTimeWindowInstance = TypeConversion.From8601TimeSpan(((string)minimumAlertableTimeWindowValue));
                                    metricDefinitionInstance.MinimumAlertableTimeWindow = minimumAlertableTimeWindowInstance;
                                }

                                JToken isAlertableValue = valueValue["IsAlertable"];
                                if (isAlertableValue != null && isAlertableValue.Type != JTokenType.Null)
                                {
                                    bool isAlertableInstance = ((bool)isAlertableValue);
                                    metricDefinitionInstance.IsAlertable = isAlertableInstance;
                                }
                            }
                        }
                    }

                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        /// <summary>
        /// The List Metric Definitions operation lists the metric definitions
        /// for the resource.
        /// </summary>
        /// <param name='resourceUri'>
        /// Required. The resource identifier of the target resource to get
        /// metrics for.
        /// </param>
        /// <param name='filterString'>
        /// Optional. An OData $filter expression that supports querying by the
        /// name of the metric definition. For example, "name.value eq
        /// 'Percentage CPU'". Name is optional, meaning the expression may be
        /// "".
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The List Metric Definitions operation response.
        /// </returns>
        public async Task <MetricDefinitionListResponse> GetMetricDefinitionsAsync(string resourceUri, string filterString, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceUri == null)
            {
                throw new ArgumentNullException("resourceUri");
            }

            // Tracing
            bool   shouldTrace  = CloudContext.Configuration.Tracing.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = Tracing.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("resourceUri", resourceUri);
                tracingParameters.Add("filterString", filterString);
                Tracing.Enter(invocationId, this, "GetMetricDefinitionsAsync", tracingParameters);
            }

            // Construct URL
            string url = "/" + resourceUri.Trim() + "/metricDefinitions?";

            url = url + "api-version=2014-04-01";
            if (filterString != null)
            {
                url = url + "&$filter=" + Uri.EscapeDataString(filterString != null ? filterString.Trim() : "");
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;

            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;

            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("Accept", "application/json");
                httpRequest.Headers.Add("x-ms-version", "2014-04-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        Tracing.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        Tracing.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            Tracing.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    MetricDefinitionListResponse result = null;
                    // Deserialize Response
                    cancellationToken.ThrowIfCancellationRequested();
                    string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                    result = new MetricDefinitionListResponse();
                    JToken responseDoc = null;
                    if (string.IsNullOrEmpty(responseContent) == false)
                    {
                        responseDoc = JToken.Parse(responseContent);
                    }

                    if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                    {
                        MetricDefinitionCollection metricDefinitionCollectionInstance = new MetricDefinitionCollection();
                        result.MetricDefinitionCollection = metricDefinitionCollectionInstance;

                        JToken valueArray = responseDoc["value"];
                        if (valueArray != null && valueArray.Type != JTokenType.Null)
                        {
                            foreach (JToken valueValue in ((JArray)valueArray))
                            {
                                MetricDefinition metricDefinitionInstance = new MetricDefinition();
                                metricDefinitionCollectionInstance.Value.Add(metricDefinitionInstance);

                                JToken nameValue = valueValue["name"];
                                if (nameValue != null && nameValue.Type != JTokenType.Null)
                                {
                                    LocalizableString nameInstance = new LocalizableString();
                                    metricDefinitionInstance.Name = nameInstance;

                                    JToken valueValue2 = nameValue["value"];
                                    if (valueValue2 != null && valueValue2.Type != JTokenType.Null)
                                    {
                                        string valueInstance = ((string)valueValue2);
                                        nameInstance.Value = valueInstance;
                                    }

                                    JToken localizedValueValue = nameValue["localizedValue"];
                                    if (localizedValueValue != null && localizedValueValue.Type != JTokenType.Null)
                                    {
                                        string localizedValueInstance = ((string)localizedValueValue);
                                        nameInstance.LocalizedValue = localizedValueInstance;
                                    }
                                }

                                JToken unitValue = valueValue["unit"];
                                if (unitValue != null && unitValue.Type != JTokenType.Null)
                                {
                                    Unit unitInstance = ((Unit)Enum.Parse(typeof(Unit), ((string)unitValue), true));
                                    metricDefinitionInstance.Unit = unitInstance;
                                }

                                JToken primaryAggregationTypeValue = valueValue["primaryAggregationType"];
                                if (primaryAggregationTypeValue != null && primaryAggregationTypeValue.Type != JTokenType.Null)
                                {
                                    AggregationType primaryAggregationTypeInstance = ((AggregationType)Enum.Parse(typeof(AggregationType), ((string)primaryAggregationTypeValue), true));
                                    metricDefinitionInstance.PrimaryAggregationType = primaryAggregationTypeInstance;
                                }

                                JToken resourceUriValue = valueValue["resourceUri"];
                                if (resourceUriValue != null && resourceUriValue.Type != JTokenType.Null)
                                {
                                    string resourceUriInstance = ((string)resourceUriValue);
                                    metricDefinitionInstance.ResourceUri = resourceUriInstance;
                                }

                                JToken metricAvailabilitiesArray = valueValue["metricAvailabilities"];
                                if (metricAvailabilitiesArray != null && metricAvailabilitiesArray.Type != JTokenType.Null)
                                {
                                    foreach (JToken metricAvailabilitiesValue in ((JArray)metricAvailabilitiesArray))
                                    {
                                        MetricAvailability metricAvailabilityInstance = new MetricAvailability();
                                        metricDefinitionInstance.MetricAvailabilities.Add(metricAvailabilityInstance);

                                        JToken timeGrainValue = metricAvailabilitiesValue["timeGrain"];
                                        if (timeGrainValue != null && timeGrainValue.Type != JTokenType.Null)
                                        {
                                            TimeSpan timeGrainInstance = TypeConversion.From8601TimeSpan(((string)timeGrainValue));
                                            metricAvailabilityInstance.TimeGrain = timeGrainInstance;
                                        }

                                        JToken retentionValue = metricAvailabilitiesValue["retention"];
                                        if (retentionValue != null && retentionValue.Type != JTokenType.Null)
                                        {
                                            TimeSpan retentionInstance = TypeConversion.From8601TimeSpan(((string)retentionValue));
                                            metricAvailabilityInstance.Retention = retentionInstance;
                                        }

                                        JToken locationValue = metricAvailabilitiesValue["location"];
                                        if (locationValue != null && locationValue.Type != JTokenType.Null)
                                        {
                                            MetricLocation locationInstance = new MetricLocation();
                                            metricAvailabilityInstance.Location = locationInstance;

                                            JToken tableEndpointValue = locationValue["tableEndpoint"];
                                            if (tableEndpointValue != null && tableEndpointValue.Type != JTokenType.Null)
                                            {
                                                string tableEndpointInstance = ((string)tableEndpointValue);
                                                locationInstance.TableEndpoint = tableEndpointInstance;
                                            }

                                            JToken tableInfoArray = locationValue["tableInfo"];
                                            if (tableInfoArray != null && tableInfoArray.Type != JTokenType.Null)
                                            {
                                                foreach (JToken tableInfoValue in ((JArray)tableInfoArray))
                                                {
                                                    MetricTableInfo metricTableInfoInstance = new MetricTableInfo();
                                                    locationInstance.TableInfo.Add(metricTableInfoInstance);

                                                    JToken tableNameValue = tableInfoValue["tableName"];
                                                    if (tableNameValue != null && tableNameValue.Type != JTokenType.Null)
                                                    {
                                                        string tableNameInstance = ((string)tableNameValue);
                                                        metricTableInfoInstance.TableName = tableNameInstance;
                                                    }

                                                    JToken startTimeValue = tableInfoValue["startTime"];
                                                    if (startTimeValue != null && startTimeValue.Type != JTokenType.Null)
                                                    {
                                                        DateTime startTimeInstance = ((DateTime)startTimeValue);
                                                        metricTableInfoInstance.StartTime = startTimeInstance;
                                                    }

                                                    JToken endTimeValue = tableInfoValue["endTime"];
                                                    if (endTimeValue != null && endTimeValue.Type != JTokenType.Null)
                                                    {
                                                        DateTime endTimeInstance = ((DateTime)endTimeValue);
                                                        metricTableInfoInstance.EndTime = endTimeInstance;
                                                    }

                                                    JToken sasTokenValue = tableInfoValue["sasToken"];
                                                    if (sasTokenValue != null && sasTokenValue.Type != JTokenType.Null)
                                                    {
                                                        string sasTokenInstance = ((string)sasTokenValue);
                                                        metricTableInfoInstance.SasToken = sasTokenInstance;
                                                    }

                                                    JToken sasTokenExpirationTimeValue = tableInfoValue["sasTokenExpirationTime"];
                                                    if (sasTokenExpirationTimeValue != null && sasTokenExpirationTimeValue.Type != JTokenType.Null)
                                                    {
                                                        DateTime sasTokenExpirationTimeInstance = ((DateTime)sasTokenExpirationTimeValue);
                                                        metricTableInfoInstance.SasTokenExpirationTime = sasTokenExpirationTimeInstance;
                                                    }
                                                }
                                            }

                                            JToken partitionKeyValue = locationValue["partitionKey"];
                                            if (partitionKeyValue != null && partitionKeyValue.Type != JTokenType.Null)
                                            {
                                                string partitionKeyInstance = ((string)partitionKeyValue);
                                                locationInstance.PartitionKey = partitionKeyInstance;
                                            }
                                        }
                                    }
                                }

                                JToken propertiesSequenceElement = ((JToken)valueValue["properties"]);
                                if (propertiesSequenceElement != null && propertiesSequenceElement.Type != JTokenType.Null)
                                {
                                    foreach (JProperty property in propertiesSequenceElement)
                                    {
                                        string propertiesKey   = ((string)property.Name);
                                        string propertiesValue = ((string)property.Value);
                                        metricDefinitionInstance.Properties.Add(propertiesKey, propertiesValue);
                                    }
                                }
                            }
                        }
                    }

                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        Tracing.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }
        /// <summary>
        /// The List Metric Definitions operation lists the metric definitions
        /// for the resource.
        /// </summary>
        /// <param name='resourceUri'>
        /// Required. The resource identifier of the target resource to get
        /// metrics for.
        /// </param>
        /// <param name='filterString'>
        /// Optional. An OData $filter expression that supports querying by the
        /// name of the metric definition. For example, "name.value eq
        /// 'Percentage CPU'". Name is optional, meaning the expression may be
        /// "".
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The List Metric Definitions operation response.
        /// </returns>
        internal async Task <MetricDefinitionListResponse> GetMetricDefinitionsInternalAsync(string resourceUri, string filterString, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceUri == null)
            {
                throw new ArgumentNullException("resourceUri");
            }

            // Tracing
            bool   shouldTrace  = TracingAdapter.IsEnabled;
            string invocationId = null;

            if (shouldTrace)
            {
                invocationId = TracingAdapter.NextInvocationId.ToString();
                Dictionary <string, object> tracingParameters = new Dictionary <string, object>();
                tracingParameters.Add("resourceUri", resourceUri);
                tracingParameters.Add("filterString", filterString);
                TracingAdapter.Enter(invocationId, this, "GetMetricDefinitionsAsync", tracingParameters);
            }

            #region Manually added

            // We need to handle document db differently for now until they onboard to shoebox.
            // Construct URL
            string url = "";
            url = url + "/";
            url = url + resourceUri;
            List <string> queryParameters = new List <string>();
            if (Util.IsLegacyResource(resourceUri))
            {
                url = url + "/metricDefinitions";
                queryParameters.Add("api-version=2014-04-01");
            }
            else
            {
                url = url + "/providers/microsoft.insights/metricDefinitions";
                queryParameters.Add("api-version=2015-07-01");
            }

            #endregion

            List <string> odataFilter = new List <string>();
            if (filterString != null)
            {
                odataFilter.Add(Uri.EscapeDataString(filterString));
            }
            if (odataFilter.Count > 0)
            {
                queryParameters.Add("$filter=" + string.Join(null, odataFilter));
            }
            if (queryParameters.Count > 0)
            {
                url = url + "?" + string.Join("&", queryParameters);
            }
            string baseUrl = this.Client.BaseUri.AbsoluteUri;
            // Trim '/' character from the end of baseUrl and beginning of url.
            if (baseUrl[baseUrl.Length - 1] == '/')
            {
                baseUrl = baseUrl.Substring(0, baseUrl.Length - 1);
            }
            if (url[0] == '/')
            {
                url = url.Substring(1);
            }
            url = baseUrl + "/" + url;
            url = url.Replace(" ", "%20");

            // Create HTTP transport objects
            HttpRequestMessage httpRequest = null;
            try
            {
                httpRequest            = new HttpRequestMessage();
                httpRequest.Method     = HttpMethod.Get;
                httpRequest.RequestUri = new Uri(url);

                // Set Headers
                httpRequest.Headers.Add("Accept", "application/json");
                httpRequest.Headers.Add("x-ms-version", "2014-04-01");

                // Set Credentials
                cancellationToken.ThrowIfCancellationRequested();
                await this.Client.Credentials.ProcessHttpRequestAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                // Send Request
                HttpResponseMessage httpResponse = null;
                try
                {
                    if (shouldTrace)
                    {
                        TracingAdapter.SendRequest(invocationId, httpRequest);
                    }
                    cancellationToken.ThrowIfCancellationRequested();
                    httpResponse = await this.Client.HttpClient.SendAsync(httpRequest, cancellationToken).ConfigureAwait(false);

                    if (shouldTrace)
                    {
                        TracingAdapter.ReceiveResponse(invocationId, httpResponse);
                    }
                    HttpStatusCode statusCode = httpResponse.StatusCode;
                    if (statusCode != HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        CloudException ex = CloudException.Create(httpRequest, null, httpResponse, await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false));
                        if (shouldTrace)
                        {
                            TracingAdapter.Error(invocationId, ex);
                        }
                        throw ex;
                    }

                    // Create Result
                    MetricDefinitionListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

                        result = new MetricDefinitionListResponse();
                        JToken responseDoc = null;
                        if (string.IsNullOrEmpty(responseContent) == false)
                        {
                            responseDoc = JToken.Parse(responseContent);
                        }

                        if (responseDoc != null && responseDoc.Type != JTokenType.Null)
                        {
                            MetricDefinitionCollection metricDefinitionCollectionInstance = new MetricDefinitionCollection();
                            result.MetricDefinitionCollection = metricDefinitionCollectionInstance;

                            JToken valueArray = responseDoc["value"];
                            if (valueArray != null && valueArray.Type != JTokenType.Null)
                            {
                                foreach (JToken valueValue in ((JArray)valueArray))
                                {
                                    MetricDefinition metricDefinitionInstance = new MetricDefinition();
                                    metricDefinitionCollectionInstance.Value.Add(metricDefinitionInstance);

                                    JToken nameValue = valueValue["name"];
                                    if (nameValue != null && nameValue.Type != JTokenType.Null)
                                    {
                                        LocalizableString nameInstance = new LocalizableString();
                                        metricDefinitionInstance.Name = nameInstance;

                                        JToken valueValue2 = nameValue["value"];
                                        if (valueValue2 != null && valueValue2.Type != JTokenType.Null)
                                        {
                                            string valueInstance = ((string)valueValue2);
                                            nameInstance.Value = valueInstance;
                                        }

                                        JToken localizedValueValue = nameValue["localizedValue"];
                                        if (localizedValueValue != null && localizedValueValue.Type != JTokenType.Null)
                                        {
                                            string localizedValueInstance = ((string)localizedValueValue);
                                            nameInstance.LocalizedValue = localizedValueInstance;
                                        }
                                    }

                                    JToken unitValue = valueValue["unit"];
                                    if (unitValue != null && unitValue.Type != JTokenType.Null)
                                    {
                                        Unit unitInstance = ((Unit)Enum.Parse(typeof(Unit), ((string)unitValue), true));
                                        metricDefinitionInstance.Unit = unitInstance;
                                    }

                                    JToken primaryAggregationTypeValue = valueValue["primaryAggregationType"];
                                    if (primaryAggregationTypeValue != null && primaryAggregationTypeValue.Type != JTokenType.Null)
                                    {
                                        AggregationType primaryAggregationTypeInstance = ((AggregationType)Enum.Parse(typeof(AggregationType), ((string)primaryAggregationTypeValue), true));
                                        metricDefinitionInstance.PrimaryAggregationType = primaryAggregationTypeInstance;
                                    }

                                    JToken resourceUriValue = valueValue["resourceUri"];
                                    if (resourceUriValue != null && resourceUriValue.Type != JTokenType.Null)
                                    {
                                        string resourceUriInstance = ((string)resourceUriValue);
                                        metricDefinitionInstance.ResourceId = resourceUriInstance;
                                    }

                                    JToken metricAvailabilitiesArray = valueValue["metricAvailabilities"];
                                    if (metricAvailabilitiesArray != null && metricAvailabilitiesArray.Type != JTokenType.Null)
                                    {
                                        foreach (JToken metricAvailabilitiesValue in ((JArray)metricAvailabilitiesArray))
                                        {
                                            MetricAvailability metricAvailabilityInstance = new MetricAvailability();
                                            metricDefinitionInstance.MetricAvailabilities.Add(metricAvailabilityInstance);

                                            JToken timeGrainValue = metricAvailabilitiesValue["timeGrain"];
                                            if (timeGrainValue != null && timeGrainValue.Type != JTokenType.Null)
                                            {
                                                TimeSpan timeGrainInstance = XmlConvert.ToTimeSpan(((string)timeGrainValue));
                                                metricAvailabilityInstance.TimeGrain = timeGrainInstance;
                                            }

                                            JToken retentionValue = metricAvailabilitiesValue["retention"];
                                            if (retentionValue != null && retentionValue.Type != JTokenType.Null)
                                            {
                                                TimeSpan retentionInstance = XmlConvert.ToTimeSpan(((string)retentionValue));
                                                metricAvailabilityInstance.Retention = retentionInstance;
                                            }

                                            JToken locationValue = metricAvailabilitiesValue["location"];
                                            if (locationValue != null && locationValue.Type != JTokenType.Null)
                                            {
                                                MetricLocation locationInstance = new MetricLocation();
                                                metricAvailabilityInstance.Location = locationInstance;

                                                JToken tableEndpointValue = locationValue["tableEndpoint"];
                                                if (tableEndpointValue != null && tableEndpointValue.Type != JTokenType.Null)
                                                {
                                                    string tableEndpointInstance = ((string)tableEndpointValue);
                                                    locationInstance.TableEndpoint = tableEndpointInstance;
                                                }

                                                JToken tableInfoArray = locationValue["tableInfo"];
                                                if (tableInfoArray != null && tableInfoArray.Type != JTokenType.Null)
                                                {
                                                    foreach (JToken tableInfoValue in ((JArray)tableInfoArray))
                                                    {
                                                        MetricTableInfo metricTableInfoInstance = new MetricTableInfo();
                                                        locationInstance.TableInfo.Add(metricTableInfoInstance);

                                                        JToken tableNameValue = tableInfoValue["tableName"];
                                                        if (tableNameValue != null && tableNameValue.Type != JTokenType.Null)
                                                        {
                                                            string tableNameInstance = ((string)tableNameValue);
                                                            metricTableInfoInstance.TableName = tableNameInstance;
                                                        }

                                                        JToken startTimeValue = tableInfoValue["startTime"];
                                                        if (startTimeValue != null && startTimeValue.Type != JTokenType.Null)
                                                        {
                                                            DateTime startTimeInstance = ((DateTime)startTimeValue);
                                                            metricTableInfoInstance.StartTime = startTimeInstance;
                                                        }

                                                        JToken endTimeValue = tableInfoValue["endTime"];
                                                        if (endTimeValue != null && endTimeValue.Type != JTokenType.Null)
                                                        {
                                                            DateTime endTimeInstance = ((DateTime)endTimeValue);
                                                            metricTableInfoInstance.EndTime = endTimeInstance;
                                                        }

                                                        JToken sasTokenValue = tableInfoValue["sasToken"];
                                                        if (sasTokenValue != null && sasTokenValue.Type != JTokenType.Null)
                                                        {
                                                            string sasTokenInstance = ((string)sasTokenValue);
                                                            metricTableInfoInstance.SasToken = sasTokenInstance;
                                                        }

                                                        JToken sasTokenExpirationTimeValue = tableInfoValue["sasTokenExpirationTime"];
                                                        if (sasTokenExpirationTimeValue != null && sasTokenExpirationTimeValue.Type != JTokenType.Null)
                                                        {
                                                            DateTime sasTokenExpirationTimeInstance = ((DateTime)sasTokenExpirationTimeValue);
                                                            metricTableInfoInstance.SasTokenExpirationTime = sasTokenExpirationTimeInstance;
                                                        }
                                                    }
                                                }

                                                JToken partitionKeyValue = locationValue["partitionKey"];
                                                if (partitionKeyValue != null && partitionKeyValue.Type != JTokenType.Null)
                                                {
                                                    string partitionKeyInstance = ((string)partitionKeyValue);
                                                    locationInstance.PartitionKey = partitionKeyInstance;
                                                }
                                            }

                                            JToken blobLocationValue = metricAvailabilitiesValue["blobLocation"];
                                            if (blobLocationValue != null && blobLocationValue.Type != JTokenType.Null)
                                            {
                                                BlobLocation blobLocationInstance = new BlobLocation();
                                                metricAvailabilityInstance.BlobLocation = blobLocationInstance;

                                                JToken blobEndpointValue = blobLocationValue["blobEndpoint"];
                                                if (blobEndpointValue != null && blobEndpointValue.Type != JTokenType.Null)
                                                {
                                                    string blobEndpointInstance = ((string)blobEndpointValue);
                                                    blobLocationInstance.BlobEndpoint = blobEndpointInstance;
                                                }

                                                JToken blobInfoArray = blobLocationValue["blobInfo"];
                                                if (blobInfoArray != null && blobInfoArray.Type != JTokenType.Null)
                                                {
                                                    foreach (JToken blobInfoValue in ((JArray)blobInfoArray))
                                                    {
                                                        BlobInfo blobInfoInstance = new BlobInfo();
                                                        blobLocationInstance.BlobInfo.Add(blobInfoInstance);

                                                        JToken blobUriValue = blobInfoValue["blobUri"];
                                                        if (blobUriValue != null && blobUriValue.Type != JTokenType.Null)
                                                        {
                                                            string blobUriInstance = ((string)blobUriValue);
                                                            blobInfoInstance.BlobUri = blobUriInstance;
                                                        }

                                                        JToken startTimeValue2 = blobInfoValue["startTime"];
                                                        if (startTimeValue2 != null && startTimeValue2.Type != JTokenType.Null)
                                                        {
                                                            DateTime startTimeInstance2 = ((DateTime)startTimeValue2);
                                                            blobInfoInstance.StartTime = startTimeInstance2;
                                                        }

                                                        JToken endTimeValue2 = blobInfoValue["endTime"];
                                                        if (endTimeValue2 != null && endTimeValue2.Type != JTokenType.Null)
                                                        {
                                                            DateTime endTimeInstance2 = ((DateTime)endTimeValue2);
                                                            blobInfoInstance.EndTime = endTimeInstance2;
                                                        }

                                                        JToken sasTokenValue2 = blobInfoValue["sasToken"];
                                                        if (sasTokenValue2 != null && sasTokenValue2.Type != JTokenType.Null)
                                                        {
                                                            string sasTokenInstance2 = ((string)sasTokenValue2);
                                                            blobInfoInstance.SasToken = sasTokenInstance2;
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }

                                    JToken propertiesSequenceElement = ((JToken)valueValue["properties"]);
                                    if (propertiesSequenceElement != null && propertiesSequenceElement.Type != JTokenType.Null)
                                    {
                                        foreach (JProperty property in propertiesSequenceElement)
                                        {
                                            string propertiesKey   = ((string)property.Name);
                                            string propertiesValue = ((string)property.Value);
                                            metricDefinitionInstance.Properties.Add(propertiesKey, propertiesValue);
                                        }
                                    }

                                    JToken dimensionsArray = valueValue["dimensions"];
                                    if (dimensionsArray != null && dimensionsArray.Type != JTokenType.Null)
                                    {
                                        foreach (JToken dimensionsValue in ((JArray)dimensionsArray))
                                        {
                                            Dimension dimensionInstance = new Dimension();
                                            metricDefinitionInstance.Dimensions.Add(dimensionInstance);

                                            JToken nameValue2 = dimensionsValue["name"];
                                            if (nameValue2 != null && nameValue2.Type != JTokenType.Null)
                                            {
                                                LocalizableString nameInstance2 = new LocalizableString();
                                                dimensionInstance.Name = nameInstance2;

                                                JToken valueValue3 = nameValue2["value"];
                                                if (valueValue3 != null && valueValue3.Type != JTokenType.Null)
                                                {
                                                    string valueInstance2 = ((string)valueValue3);
                                                    nameInstance2.Value = valueInstance2;
                                                }

                                                JToken localizedValueValue2 = nameValue2["localizedValue"];
                                                if (localizedValueValue2 != null && localizedValueValue2.Type != JTokenType.Null)
                                                {
                                                    string localizedValueInstance2 = ((string)localizedValueValue2);
                                                    nameInstance2.LocalizedValue = localizedValueInstance2;
                                                }
                                            }

                                            JToken valuesArray = dimensionsValue["values"];
                                            if (valuesArray != null && valuesArray.Type != JTokenType.Null)
                                            {
                                                foreach (JToken valuesValue in ((JArray)valuesArray))
                                                {
                                                    LocalizableString localizableStringInstance = new LocalizableString();
                                                    dimensionInstance.Values.Add(localizableStringInstance);

                                                    JToken valueValue4 = valuesValue["value"];
                                                    if (valueValue4 != null && valueValue4.Type != JTokenType.Null)
                                                    {
                                                        string valueInstance3 = ((string)valueValue4);
                                                        localizableStringInstance.Value = valueInstance3;
                                                    }

                                                    JToken localizedValueValue3 = valuesValue["localizedValue"];
                                                    if (localizedValueValue3 != null && localizedValueValue3.Type != JTokenType.Null)
                                                    {
                                                        string localizedValueInstance3 = ((string)localizedValueValue3);
                                                        localizableStringInstance.LocalizedValue = localizedValueInstance3;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                    result.StatusCode = statusCode;
                    if (httpResponse.Headers.Contains("x-ms-request-id"))
                    {
                        result.RequestId = httpResponse.Headers.GetValues("x-ms-request-id").FirstOrDefault();
                    }

                    if (shouldTrace)
                    {
                        TracingAdapter.Exit(invocationId, result);
                    }
                    return(result);
                }
                finally
                {
                    if (httpResponse != null)
                    {
                        httpResponse.Dispose();
                    }
                }
            }
            finally
            {
                if (httpRequest != null)
                {
                    httpRequest.Dispose();
                }
            }
        }