public async Task CanReadDocumentDbMetrics()
        {
            // arrange
            var pecker = new AzureMetricsApiSourcePecker();
            var source = new PeckSource
            {
                SourceConnectionString = _connectionString,
                LastOffset             = DateTimeOffset.UtcNow.AddHours(-1),
                IntervalMinutes        = 30,
                Name         = "TheSourceName",
                CustomConfig = ProviderNames.DocumentDB
            };

            // act
            var entities = await pecker.PeckAsync(source);

            // assert
            Assert.NotEmpty(entities);

            foreach (var entity in entities)
            {
                Assert.NotNull(entity.PartitionKey);
                Assert.NotNull(entity.RowKey);
            }
        }
Example #2
0
        public async Task StoreAsync(PeckSource source, IEnumerable <ITableEntity> results)
        {
            var account = CloudStorageAccount.Parse(source.DestinationConnectionString);
            var client  = account.CreateCloudTableClient();
            var table   = client.GetTableReference(source.DestinationTableName);
            await table.CreateIfNotExistsAsync();

            var copy   = results.ToArray(); // have to do this because of batching
            var groups = copy.GroupBy(x => x.PartitionKey);

            foreach (var g in groups)
            {
                var list           = new List <TableBatchOperation>();
                var batchOperation = new TableBatchOperation();
                foreach (var result in g)
                {
                    if (batchOperation.Count >= 40)
                    {
                        list.Add(batchOperation);
                        batchOperation = new TableBatchOperation();
                    }
                    batchOperation.Add(TableOperation.InsertOrReplace(result));
                }

                list.Add(batchOperation);
                foreach (var batch in list)
                {
                    await table.ExecuteBatchAsync(batch);
                }
            }
        }
Example #3
0
        public async Task <IEnumerable <ITableEntity> > PeckAsync(PeckSource source)
        {
            if (_query == null)
            {
                _query = GetQuery();
            }


            var results    = new List <ITableEntity>();
            var connection = new SqlConnection(source.SourceConnectionString);

            try
            {
                await connection.OpenAsync();

                var command = new SqlCommand(_query, connection);
                var reader  = await command.ExecuteReaderAsync();

                var columnNames = Enumerable.Range(0, reader.FieldCount).Select(x => reader.GetName(x)).ToDictionary(s => s);

                while (reader.Read())
                {
                    // row key
                    var rowKeyParams = GetRowKeyFieldNames().Select(x => columnNames.ContainsKey(x) ? reader[x].ToString() : x).ToArray();
                    var rowKey       = rowKeyParams.Length == 0 ? Guid.NewGuid().ToString("N") : string.Join("_", rowKeyParams);

                    // par key
                    var ofsted       = new DateTimeOffset(reader.GetDateTime(reader.GetOrdinal(GetUtcTimestampFieldName())), TimeSpan.Zero);
                    var minuteOffset = new DateTimeOffset(DateTime.Parse(ofsted.UtcDateTime.ToString("yyyy-MM-dd HH:mm:00")), TimeSpan.Zero);
                    var shardKey     = (DateTimeOffset.MaxValue.Ticks - minuteOffset.Ticks).ToString("D19");

                    var entity = new DynamicTableEntity(shardKey, rowKey);
                    foreach (var columnName in columnNames.Keys)
                    {
                        entity.Properties.Add(columnName, EntityProperty.CreateEntityPropertyFromObject(reader[columnName]));
                    }

                    results.Add(entity);
                }


                return(results);
            }
            finally
            {
                connection.Close();
            }
        }
Example #4
0
        public async Task <IEnumerable <ITableEntity> > PeckAsync(PeckSource source)
        {
            var config = new ConfigurationMapper().Map(source.SourceConnectionString);

            var startTimeUtc = source.LastOffset.DateTime;

            var metricCollectionService = CreateMetricCollectionService(config);
            var request = CreateRequest(source.CustomConfig, config.ResourceId, startTimeUtc, source.IntervalMinutes);

            var metrics = await metricCollectionService.CollectMetrics(
                request);

            var timeCapturedUtc = DateTimeOffset.UtcNow;

            return(metrics.Select(m => PeckResultExtensions.ToEntity(m, source.Name, timeCapturedUtc)).ToArray());
        }
Example #5
0
        public static BusPeckResult Peck(this QueueDescription queueDescription, PeckSource source)
        {
            var peckResult = new BusPeckResult()
            {
                ActiveMessageCount     = queueDescription.MessageCountDetails.ActiveMessageCount,
                DeadLetterMessageCount = queueDescription.MessageCountDetails.DeadLetterMessageCount,
                ScheduledMessageCount  = queueDescription.MessageCountDetails.ScheduledMessageCount,
                MaxSizeInMB            = queueDescription.MaxSizeInMegabytes,
                SizeInMB     = queueDescription.SizeInBytes / (1024 * 1024),
                QueueName    = SanitiseName(queueDescription.Path),
                SourceName   = source.Name,
                TimeCaptured = DateTimeOffset.UtcNow,
                QueueType    = "Q"
            };

            return(peckResult);
        }
Example #6
0
        public async Task <IEnumerable <ITableEntity> > PeckAsync(PeckSource source)
        {
            var peckResults      = new List <BusPeckResult>();
            var namespaceManager = NamespaceManager.CreateFromConnectionString(source.SourceConnectionString);

            foreach (var topic in await namespaceManager.GetTopicsAsync())
            {
                peckResults.Add(topic.Peck(source));
                foreach (var s in await namespaceManager.GetSubscriptionsAsync(topic.Path))
                {
                    peckResults.Add(s.Peck(source));
                }
            }

            foreach (var q in await namespaceManager.GetQueuesAsync())
            {
                peckResults.Add(q.Peck(source));
            }

            return(peckResults.Select(x => x.ToEntity()));
        }
Example #7
0
        public static BusPeckResult Peck(this SubscriptionDescription subscriptionDescription, PeckSource source)
        {
            var peckResult = new BusPeckResult()
            {
                ActiveMessageCount     = subscriptionDescription.MessageCountDetails.ActiveMessageCount,
                DeadLetterMessageCount = subscriptionDescription.MessageCountDetails.DeadLetterMessageCount,
                ScheduledMessageCount  = subscriptionDescription.MessageCountDetails.ScheduledMessageCount,
                MaxSizeInMB            = 0, // N/A
                SizeInMB     = 0,           // N/A
                QueueName    = SanitiseName(string.Format("{0}_{1}", subscriptionDescription.TopicPath, subscriptionDescription.Name)),
                SourceName   = source.Name,
                TimeCaptured = DateTimeOffset.UtcNow,
                QueueType    = "S"
            };

            return(peckResult);
        }