Beispiel #1
0
        /// <summary>
        /// Sums up the values passed in and reports the total once per minute
        /// </summary>
        /// <param name="category">Category of the metric</param>
        /// <param name="metricName">Name of the metric</param>
        /// <param name="value"></param>
        public static void Sum(string category, string metricName, double value, MetricSetting advancedSettings = null)
        {
            var m = new Metric(category, metricName, MetricType.Counter);

            m.Value    = value;
            m.Settings = advancedSettings;
            StackifyLib.Internal.Metrics.MetricClient.QueueMetric(m);
        }
Beispiel #2
0
        /// <summary>
        /// Sums up the values passed in and reports the average of the values once per minute
        /// </summary>
        /// <param name="category">Category of the metric</param>
        /// <param name="metricName">Name of the metric</param>
        /// <param name="value"></param>
        public static void Average(string category, string metricName, double value, MetricSetting advancedSettings = null)
        {
            var m = new Metric(category, metricName, MetricType.MetricAverage)
            {
                Value = value, Settings = advancedSettings
            };

            MetricClient.QueueMetric(m);
        }
        /// <summary>
        /// The List Metric Settings operation lists the metric settings for
        /// the resource.
        /// </summary>
        /// <param name='resourceId'>
        /// Required. The id of the resource.
        /// </param>
        /// <param name='metricNamespace'>
        /// Required. The namespace of the metrics.
        /// </param>
        /// <param name='cancellationToken'>
        /// Cancellation token.
        /// </param>
        /// <returns>
        /// The list metric settings operation response.
        /// </returns>
        public async Task <MetricSettingListResponse> ListAsync(string resourceId, string metricNamespace, CancellationToken cancellationToken)
        {
            // Validate
            if (resourceId == null)
            {
                throw new ArgumentNullException("resourceId");
            }
            if (metricNamespace == null)
            {
                throw new ArgumentNullException("metricNamespace");
            }

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

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

            // Construct URL
            string url = "";

            url = url + "/";
            if (this.Client.Credentials.SubscriptionId != null)
            {
                url = url + Uri.EscapeDataString(this.Client.Credentials.SubscriptionId);
            }
            url = url + "/services/monitoring/metricsettings";
            List <string> queryParameters = new List <string>();

            queryParameters.Add("resourceId=" + Uri.EscapeDataString(resourceId));
            queryParameters.Add("namespace=" + Uri.EscapeDataString(metricNamespace));
            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", "2013-10-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
                    MetricSettingListResponse result = null;
                    // Deserialize Response
                    if (statusCode == HttpStatusCode.OK)
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string responseContent = await httpResponse.Content.ReadAsStringAsync().ConfigureAwait(false);

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

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

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

                                    JToken resourceIdValue = valueValue["ResourceId"];
                                    if (resourceIdValue != null && resourceIdValue.Type != JTokenType.Null)
                                    {
                                        string resourceIdInstance = ((string)resourceIdValue);
                                        metricSettingInstance.ResourceId = resourceIdInstance;
                                    }

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

                                    JToken valueValue2 = valueValue["Value"];
                                    if (valueValue2 != null && valueValue2.Type != JTokenType.Null)
                                    {
                                        string typeName = ((string)valueValue2["odata.type"]);
                                        if (typeName == "Microsoft.WindowsAzure.Management.Monitoring.Metrics.Models.AvailabilityMetricSettingValue")
                                        {
                                            AvailabilityMetricSettingValue availabilityMetricSettingValueInstance = new AvailabilityMetricSettingValue();

                                            JToken availableLocationsArray = valueValue2["AvailableLocations"];
                                            if (availableLocationsArray != null && availableLocationsArray.Type != JTokenType.Null)
                                            {
                                                foreach (JToken availableLocationsValue in ((JArray)availableLocationsArray))
                                                {
                                                    NameConfig nameConfigInstance = new NameConfig();
                                                    availabilityMetricSettingValueInstance.AvailableLocations.Add(nameConfigInstance);

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

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

                                            JToken endpointsArray = valueValue2["Endpoints"];
                                            if (endpointsArray != null && endpointsArray.Type != JTokenType.Null)
                                            {
                                                foreach (JToken endpointsValue in ((JArray)endpointsArray))
                                                {
                                                    EndpointConfig endpointConfigInstance = new EndpointConfig();
                                                    availabilityMetricSettingValueInstance.Endpoints.Add(endpointConfigInstance);

                                                    JToken configIdValue = endpointsValue["ConfigId"];
                                                    if (configIdValue != null && configIdValue.Type != JTokenType.Null)
                                                    {
                                                        string configIdInstance = ((string)configIdValue);
                                                        endpointConfigInstance.ConfigId = configIdInstance;
                                                    }

                                                    JToken nameValue2 = endpointsValue["Name"];
                                                    if (nameValue2 != null && nameValue2.Type != JTokenType.Null)
                                                    {
                                                        string nameInstance2 = ((string)nameValue2);
                                                        endpointConfigInstance.Name = nameInstance2;
                                                    }

                                                    JToken locationValue = endpointsValue["Location"];
                                                    if (locationValue != null && locationValue.Type != JTokenType.Null)
                                                    {
                                                        string locationInstance = ((string)locationValue);
                                                        endpointConfigInstance.Location = locationInstance;
                                                    }

                                                    JToken urlValue = endpointsValue["Url"];
                                                    if (urlValue != null && urlValue.Type != JTokenType.Null)
                                                    {
                                                        Uri urlInstance = TypeConversion.TryParseUri(((string)urlValue));
                                                        endpointConfigInstance.Url = urlInstance;
                                                    }
                                                }
                                            }
                                            metricSettingInstance.Value = availabilityMetricSettingValueInstance;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    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();
                }
            }
        }
Beispiel #4
0
        /// <summary>
        /// Increment or decrement a guage metric type
        /// </summary>
        /// <param name="category">Category of the metric</param>
        /// <param name="metricName">Name of the metric</param>
        /// <param name="incrementBy">Value can be positive or negative to decrement. Defaults to 1</param>
        public static void IncrementGauge(string category, string metricName, double incrementBy, MetricSetting advancedSettings)
        {
            //leaving the count as 1 below because when it gets processed later it would sum up the count there
            var m = new Metric(category, metricName, MetricType.MetricLast);

            m.Value       = incrementBy;
            m.IsIncrement = true;
            m.Settings    = advancedSettings;

            MetricClient.QueueMetric(m);
        }