Пример #1
0
 /// <summary>
 /// Report how long something took to happen and this type of metric will calculate the average time it takes
 /// </summary>
 /// <param name="category">Category of the metric</param>
 /// <param name="metricName">Name of the metric</param>
 /// <param name="timeTaken">How long the event being tracked took</param>
 public static void Time(string category, string metricName, TimeSpan timeTaken)
 {
     MetricClient.QueueMetric(new Metric(category, metricName, MetricType.CounterTime)
     {
         Value = timeTaken.TotalSeconds
     });
 }
        public async Task WhenSendingMetricQueryThenTheResultsAreAsExpected()
        {
            // Authenticate (set real values in resourceIdentifier to run this test).
            var authenticationServices = new AuthenticationServices();
            await authenticationServices.AuthenticateUserAsync();

            ICredentialsFactory credentialsFactory = new ActiveDirectoryCredentialsFactory(authenticationServices);

            var          resourceId = $"/subscriptions/{this.resourceIdentifier.SubscriptionId}/resourceGroups/{this.resourceIdentifier.ResourceGroupName}/providers/Microsoft.Storage/storageAccounts/{this.resourceIdentifier.ResourceName}/queueServices/default";
            MetricClient client     = new MetricClient(this.tracerMock.Object, credentialsFactory);

            var parameters = new QueryParameters()
            {
                StartTime    = DateTime.UtcNow.Date.AddDays(-1),
                EndTime      = DateTime.UtcNow.Date,
                Aggregations = new List <Aggregation> {
                    Aggregation.Total
                },
                MetricNames = new List <string>()
                {
                    "QueueMessageCount"
                },
                Interval = TimeSpan.FromMinutes(60)
            };

            var metrics1 = (await client.GetResourceMetricsAsync(resourceId, parameters)).ToList();
            var metrics2 = (await client.GetResourceMetricsAsync(this.resourceIdentifier, StorageServiceType.Queue, parameters)).ToList();

            Assert.IsTrue(metrics1.Any() && metrics2.Any(), "Lists are not full with data");
            Assert.IsTrue(metrics1.First().Timeseries.Any() && metrics2.First().Timeseries.Any(), "Metrics do not contain Time series");
            Assert.IsTrue(metrics1[0].Timeseries[0].Data.Any() && metrics2[0].Timeseries[0].Data.Any(), "Time series are not full with data");
        }
        public async Task WhenSubjectAndKeyContainRestrictedCharacters_GetIndividualBySubject_UrlEncodesRestrictedCharacters()
        {
            // Setup mock expectation for a encoded url call
            var mockHttpHandler = new MockHttpMessageHandler();
            var response        = @"{
				'id': 'ca98dfajl3kja9kjh34589734',
				'applicationId': 'ci123doiu3',
				'key': 'testKey 123',
				'subject': 'test:subject=123',
				'value': 5
			}"            .Replace("'", "\"");

            mockHttpHandler.Expect(HttpMethod.Get, "https://api.useast1.badgeup.io/v2/apps/1337/metrics/test%3Asubject%3D123/testKey%20123").Respond("application/json", response);
            using (var badgeUpClient = new BadgeUpHttpClient(ApiKey.Create("eyJhY2NvdW50SWQiOiJ0aGViZXN0IiwiYXBwbGljYXRpb25JZCI6IjEzMzciLCJrZXkiOiJpY2VjcmVhbWFuZGNvb2tpZXN5dW0ifQ=="), "https://api.useast1.badgeup.io"))
            {
                badgeUpClient.SetHttpClient(mockHttpHandler.ToHttpClient());

                // pass subject and key with restricted characters
                var metricClient = new MetricClient(badgeUpClient);
                var metric       = await metricClient.GetIndividualBySubject("test:subject=123", "testKey 123");

                Assert.Equal("test:subject=123", metric.Subject);
                Assert.Equal("testKey 123", metric.Key);
            }

            // expect encoded url to have been called
            mockHttpHandler.VerifyNoOutstandingExpectation();
        }
        public async Task WhenCallingGetResourceMetricsWithNamespaceHappyFlow()
        {
            string expectedUri = "/subscriptions/SUBSCRIPTION_ID/resourceGroups/RESOURCE_GROUP_NAME/providers/Microsoft.Storage/storageAccounts/STORAGE_NAME/queueServices/default";

            var azureResponse = new AzureOperationResponse <ResponseInner>()
            {
                Body      = new ResponseInner("timespan", GetMetricList()),
                Request   = new HttpRequestMessage(),
                RequestId = "RequestId",
                Response  = new HttpResponseMessage()
            };

            this.metricsOperationsMock
            .Setup(metric => metric.ListWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <ODataQuery <MetadataValueInner> >(), It.IsAny <string>(), It.IsAny <TimeSpan?>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int?>(), It.IsAny <string>(), It.IsAny <ResultType?>(), It.IsAny <string>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(azureResponse);

            this.monitorManagementClientMock.SetupGet(monitorClient => monitorClient.Metrics).Returns(this.metricsOperationsMock.Object);
            IMetricClient metricClient = new MetricClient(this.tracerMock.Object, this.monitorManagementClientMock.Object);

            List <MetricQueryResult> metrics = (await metricClient.GetResourceMetricsAsync(this.resourceIdentifier, StorageServiceType.Queue, new QueryParameters()
            {
                MetricNamespace = "NAMESPACE"
            }, default(CancellationToken))).ToList();

            // Validate that right Uri was generated
            this.metricsOperationsMock.Verify(metric => metric.ListWithHttpMessagesAsync(expectedUri, It.IsAny <ODataQuery <MetadataValueInner> >(), null, null, string.Empty, null, null, null, null, "NAMESPACE", null, CancellationToken.None));
            Assert.AreEqual(metrics.Count, 2, "2 metrics are expected");
        }
Пример #5
0
        /// <summary>
        /// Flushes any items in the queue when shutting down an app
        /// </summary>
        public static void Shutdown()
        {
            //flush logs queue
            _LogClient.Close();

            //flush any remaining metrics as well
            MetricClient.StopMetricsQueue("Logger Shutdown called");
        }
Пример #6
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;

            MetricClient.QueueMetric(m);
        }
Пример #7
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);
        }
        static StackifyTraceListener()
        {
            GlobalEventSystemFacade.SubscribeToPrepareForShutDownEvent(args =>
            {
                StackifyAPILogger.Log("Composite C1 TraceListener shutting down");
                MetricClient.StopMetricsQueue("Composite C1 TraceListener shutting down");

                StackifyLogger.Shutdown();
            });
        }
        public async Task WhenSubjectAndKeyContainRestrictedCharacters_GetAllBySubject_UrlEncodesRestrictedCharacters()
        {
            // Setup mock expectation for a encoded url call
            var mockHttpHandler = new MockHttpMessageHandler();
            var response        = @"{
				'pages': {
					'previous': null,
					'next': null
				},
				'data': [
					{
						'id': 'cja8a0980j3jkajhaaa345810',
						'applicationId': 'e7kmr6m0ob',
						'key': 'test',
						'subject': 'test:subject=123',
						'value': 5
					},
					{
						'id': 'cjmx29083hjkaaa325usgaitx',
						'applicationId': 'e7kmr6m0ob',
						'key': 'test:key',
						'subject': 'test:subject=123',
						'value': 1
					},
					{
						'id': 'cjmy215tq87hdo1b9hrb2qlsj',
						'applicationId': 'e7kmr6m0ob',
						'key': 'test:metric',
						'subject': 'test:subject=123',
						'value': 5
					}
				]
			}"            .Replace("'", "\"");

            mockHttpHandler.Expect(HttpMethod.Get, "https://api.useast1.badgeup.io/v2/apps/1337/metrics/test%3Asubject%3D123").Respond("application/json", response);
            using (var badgeUpClient = new BadgeUpHttpClient(ApiKey.Create("eyJhY2NvdW50SWQiOiJ0aGViZXN0IiwiYXBwbGljYXRpb25JZCI6IjEzMzciLCJrZXkiOiJpY2VjcmVhbWFuZGNvb2tpZXN5dW0ifQ=="), "https://api.useast1.badgeup.io"))
            {
                badgeUpClient.SetHttpClient(mockHttpHandler.ToHttpClient());

                // pass subject and key with restricted characters
                var metricClient = new MetricClient(badgeUpClient);
                var metrics      = await metricClient.GetAllBySubject("test:subject=123");

                Assert.Equal(3, metrics.Count);
                Assert.Equal("test:subject=123", metrics[0].Subject);
                Assert.Equal("test:subject=123", metrics[1].Subject);
                Assert.Equal("test:subject=123", metrics[2].Subject);
                Assert.Equal("test", metrics[0].Key);
                Assert.Equal("test:key", metrics[1].Key);
                Assert.Equal("test:metric", metrics[2].Key);
            }

            // expect encoded url to have been called
            mockHttpHandler.VerifyNoOutstandingExpectation();
        }
Пример #10
0
        /// <summary>
        /// Guage type metric that reports the last value reported once a minute
        /// </summary>
        /// <param name="category">Category of the metric</param>
        /// <param name="metricName">Name of the metric</param>
        /// <param name="value">Explicit value to set the metric to</param>
        /// <param name="autoResendLastValueIfNothingReported">Every minute resend the last value if nothing reported</param>
        public static void SetGauge(string category, string metricName, double value, bool autoResendLastValueIfNothingReported = false)
        {
            var m = new Metric(category, metricName, MetricType.MetricLast)
            {
                Value = value, Settings = new MetricSetting {
                    AutoReportLastValueIfNothingReported = autoResendLastValueIfNothingReported
                }
            };

            MetricClient.QueueMetric(m);
        }
Пример #11
0
        /// <summary>
        /// Counts how many times something happens per minute
        /// </summary>
        /// <param name="category">Category of the metric</param>
        /// <param name="metricName">Name of the metric</param>
        /// <param name="incrementBy"></param>
        /// <param name="autoSendIfZero">If nothing is reported for a minute, should we report a 0?</param>
        public static void Count(string category, string metricName, int incrementBy = 1, bool autoReportZeroIfNothingReported = false)
        {
            var m = new Metric(category, metricName, MetricType.Counter);

            m.Value    = incrementBy;
            m.Settings = new MetricSetting {
                AutoReportZeroIfNothingReported = autoReportZeroIfNothingReported
            };

            MetricClient.QueueMetric(m);
        }
Пример #12
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);
        }
        public async Task WhenCallingGetResourceMetricsWithServiceTypeHappyFlow()
        {
            var    timestamp   = DateTime.UtcNow;
            string expectedUri = "/subscriptions/SUBSCRIPTION_ID/resourceGroups/RESOURCE_GROUP_NAME/providers/Microsoft.Storage/storageAccounts/STORAGE_NAME/queueServices/default";

            var azureResponse = new AzureOperationResponse <ResponseInner>()
            {
                Body      = new ResponseInner("timespan", GetMetricList(timestamp)),
                Request   = new HttpRequestMessage(),
                RequestId = "RequestId",
                Response  = new HttpResponseMessage()
            };

            this.metricsOperationsMock
            .Setup(metric => metric.ListWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <ODataQuery <MetadataValueInner> >(), It.IsAny <string>(), It.IsAny <TimeSpan?>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <int?>(), It.IsAny <string>(), It.IsAny <ResultType?>(), It.IsAny <string>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>()))
            .ReturnsAsync(azureResponse);

            this.monitorManagementClientMock.SetupGet(monitorClient => monitorClient.Metrics).Returns(this.metricsOperationsMock.Object);
            IMetricClient metricClient = new MetricClient(this.tracerMock.Object, this.resourceIdentifier.SubscriptionId, this.monitorManagementClientMock.Object);

            List <MetricQueryResult> metrics = (await metricClient.GetResourceMetricsAsync(this.resourceIdentifier, ServiceType.AzureStorageQueue, new QueryParameters(), default(CancellationToken))).ToList();

            // Validate that right Uri was generated
            this.metricsOperationsMock.Verify(metric => metric.ListWithHttpMessagesAsync(expectedUri, It.IsAny <ODataQuery <MetadataValueInner> >(), null, null, string.Empty, null, null, null, null, null, null, CancellationToken.None));
            Assert.AreEqual(metrics.Count, 2, "2 metrics are expected");

            // Validate first metric was converted successfully
            Assert.AreEqual("MetricName1", metrics[0].Name, "First metric name isn't correct");
            Assert.AreEqual("ByteSeconds", metrics[0].Unit, "First metric unit isn't correct");
            Assert.AreEqual(0, metrics[0].Timeseries.Count, "First metric timeseries should be empty");

            // Validate second metric was converted successfully
            Assert.AreEqual("MetricName2", metrics[1].Name, "Second metric name isn't correct");
            Assert.AreEqual("MilliSeconds", metrics[1].Unit, "Second metric unit isn't correct");
            Assert.AreEqual(5, metrics[1].Timeseries.Count, "Second metric timeseries should be empty");
            Assert.AreEqual(2, metrics[1].Timeseries[0].Data.Count, "Second metric first timeseries (Dimension1Value1) length should be 2");
            Assert.AreEqual(2, metrics[1].Timeseries[1].Data.Count, "Second metric second timeseries (Dimension1Value2) length should be 2");
            Assert.AreEqual(1.1, metrics[1].Timeseries[0].Data[0].Average, "Second metric first timeseries first average is wrong");
            Assert.AreEqual(1.0, metrics[1].Timeseries[0].Data[1].Average, "Second metric first timeseries second average is wrong");
            Assert.AreEqual(timestamp.AddMinutes(-1), metrics[1].Timeseries[0].Data[0].TimeStamp, "Second metric first timeseries first timestamp is wrong");
            Assert.IsNull(metrics[1].Timeseries[0].Data[0].Total, "Second metric first timeseries first total should be null");
            Assert.IsNull(metrics[1].Timeseries[0].Data[0].Maximum, "Second metric first timeseries first maximum should be null");
            Assert.IsNull(metrics[1].Timeseries[0].Data[0].Minimum, "Second metric first timeseries first minimum should be null");
            Assert.IsNull(metrics[1].Timeseries[0].Data[0].Count, "Second metric first timeseries first count should be null");

            Assert.AreEqual(2.1, metrics[1].Timeseries[1].Data[0].Minimum, "Second metric second timeseries first minimum is wrong");
            Assert.AreEqual(3.1, metrics[1].Timeseries[2].Data[0].Maximum, "Second metric third timeseries first maximum is wrong");
            Assert.AreEqual(4.1, metrics[1].Timeseries[3].Data[0].Total, "Second metric forth timeseries first total is wrong");
            Assert.AreEqual(1, metrics[1].Timeseries[4].Data[0].Count, "Second metric fifth timeseries first count is wrong");

            Assert.AreEqual(1, metrics[1].Timeseries[0].MetaData.Count, "Second metric first timeseries metadata length is wrong");
            Assert.AreEqual("Dimension1", metrics[1].Timeseries[0].MetaData[0].Key, "Second metric first timeseries first metadata key is wrong");
            Assert.AreEqual("Dimension1Value1", metrics[1].Timeseries[0].MetaData[0].Value, "Second metric first timeseries first metadata value is wrong");
        }
Пример #14
0
 /// <summary>
 /// Calculate average time taken and a second metric for how many times it occurred
 /// </summary>
 /// <param name="category">Category of the metric</param>
 /// <param name="metricName">Name of the metric</param>
 /// <param name="timeTaken">How long the event being tracked took</param>
 public static void CountAndTime(string category, string metricName, TimeSpan timeTaken, bool autoReportZeroIfNothingReported = false)
 {
     MetricClient.QueueMetric(new Metric(category, metricName, MetricType.Counter)
     {
         Value = 1, Settings = new MetricSetting()
         {
             AutoReportZeroIfNothingReported = autoReportZeroIfNothingReported
         }
     });
     MetricClient.QueueMetric(new Metric(category, metricName + " Time", MetricType.CounterTime)
     {
         Value = timeTaken.TotalSeconds
     });
 }
Пример #15
0
 public Task CreateContinuousTask(CancellationToken cancellationToken)
 {
     return(new Task(() =>
     {
         var reader = new PerfmonCounterReader();
         using (var messenger = new MetricClient(_metricPublishingConfig))
         {
             foreach (var metricBatch in reader.StreamCounterSamples(_counterPaths, _counterSamplingConfig.SampleInterval, cancellationToken)
                      .SelectMany(set => set.CounterSamples.ToMetrics(_counterSamplingConfig.AddInstanceNameToMetrics))
                      .Chunk(10))
             {
                 messenger.Send(metricBatch);
             }
         }
     }, cancellationToken));
 }
        public async Task WhenCallingGetResourceMetricDefinitionsHappyFlow()
        {
            string resourceUri = "/subscriptions/SUBSCRIPTION_ID/resourceGroups/RESOURCE_GROUP_NAME/providers/Microsoft.Storage/storageAccounts/STORAGE_NAME";

            var expectedDefinitions = GetMetricDefinitionsList();
            var azureResponse       = new AzureOperationResponse <IEnumerable <Microsoft.Azure.Management.Monitor.Fluent.Models.MetricDefinition> >()
            {
                Body      = expectedDefinitions,
                Request   = new HttpRequestMessage(),
                RequestId = "RequestId",
                Response  = new HttpResponseMessage()
            };

            this.metricDefinitionsOperationsMock.Setup(metric => metric.ListWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>())).ReturnsAsync(azureResponse);

            this.monitorManagementClientMock.SetupGet(monitorClient => monitorClient.MetricDefinitions).Returns(this.metricDefinitionsOperationsMock.Object);
            IMetricClient metricClient = new MetricClient(this.tracerMock.Object, this.monitorManagementClientMock.Object);

            List <MetricDefinition> definitions = (await metricClient.GetResourceMetricDefinitionsAsync(resourceUri, default(CancellationToken))).ToList();

            // Validate that right Uri was used
            this.metricDefinitionsOperationsMock.Verify(metric => metric.ListWithHttpMessagesAsync(resourceUri, null, null, CancellationToken.None));
            Assert.AreEqual(definitions.Count, 2, "2 definitions are expected");
        }
        public async Task WhenCallingGetResourceMetricDefinitionsHappyFlow()
        {
            string resourceUri = "/subscriptions/SUBSCRIPTION_ID/resourceGroups/RESOURCE_GROUP_NAME/providers/Microsoft.Storage/storageAccounts/STORAGE_NAME";

            var expectedDefinitions = GetMetricDefinitionsList();
            var azureResponse       = new AzureOperationResponse <IEnumerable <Microsoft.Azure.Management.Monitor.Fluent.Models.MetricDefinition> >()
            {
                Body      = expectedDefinitions,
                Request   = new HttpRequestMessage(),
                RequestId = "RequestId",
                Response  = new HttpResponseMessage()
            };

            this.metricDefinitionsOperationsMock.Setup(metric => metric.ListWithHttpMessagesAsync(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <Dictionary <string, List <string> > >(), It.IsAny <CancellationToken>())).ReturnsAsync(azureResponse);

            this.monitorManagementClientMock.SetupGet(monitorClient => monitorClient.MetricDefinitions).Returns(this.metricDefinitionsOperationsMock.Object);
            IMetricClient metricClient = new MetricClient(this.tracerMock.Object, this.resourceIdentifier.SubscriptionId, this.monitorManagementClientMock.Object);

            List <MetricDefinition> definitions = (await metricClient.GetResourceMetricDefinitionsAsync(resourceUri, default(CancellationToken))).ToList();

            // Validate that right Uri was used
            this.metricDefinitionsOperationsMock.Verify(metric => metric.ListWithHttpMessagesAsync(resourceUri, null, null, CancellationToken.None));
            Assert.AreEqual(definitions.Count, 2, "2 definitions are expected");

            // Validate first metric was converted successfully
            Assert.AreEqual(expectedDefinitions.Count, definitions.Count, "Definition count isn't correct");
            for (int i = 0; i < expectedDefinitions.Count; i++)
            {
                Assert.AreEqual(expectedDefinitions[i].Name.Value, definitions[i].Name, "Metric name isn't correct");
                Assert.IsTrue(expectedDefinitions[i].MetricAvailabilities.Select(x => Tuple.Create(x.Retention, x.TimeGrain)).SequenceEqual(definitions[i].Availabilities), "Metric availabilities aren't correct");
                Assert.IsTrue(expectedDefinitions[i].Dimensions.Select(x => x.Value).SequenceEqual(definitions[i].Dimensions), "Metric dimensions aren't correct");
                Assert.AreEqual(expectedDefinitions[i].IsDimensionRequired, definitions[i].IsDimensionRequired, "Metric is dimension required isn't correct");
                Assert.AreEqual(expectedDefinitions[i].PrimaryAggregationType?.ToString(), definitions[i].PrimaryAggregationType?.ToString(), "Metric primary aggregation type isn't correct");
                Assert.AreEqual(expectedDefinitions[i].Unit?.ToString(), definitions[i].Unit, "Metric unit isn't correct");
            }
        }
Пример #18
0
 public static LatestAggregate GetLatest(string category, string metricName)
 {
     return(MetricClient.GetLatestMetric(category, metricName));
 }
Пример #19
0
 public static List <LatestAggregate> GetLatestAllMetrics()
 {
     return(MetricClient.GetLatestMetrics());
 }
 public async Task WhenMetricIsNull_CreateThrowsException()
 {
     var client = new MetricClient(null);
     await Assert.ThrowsAsync <ArgumentNullException>(() => client.Create(null));
 }