コード例 #1
0
        internal static TableOperation CreateMetricsInsertOperation(ScaleMetrics metrics, string hostId, ScaleMonitorDescriptor descriptor, DateTime?now = null)
        {
            now = now ?? DateTime.UtcNow;

            // Use an inverted ticks rowkey to order the table in descending order, allowing us to easily
            // query for latest logs. Adding a guid as part of the key to ensure uniqueness.
            string rowKey = TableStorageHelpers.GetRowKey(now.Value);

            var entity = TableEntityConverter.ToEntity(metrics, hostId, rowKey, metrics.Timestamp);

            entity.Properties.Add(MonitorIdPropertyName, EntityProperty.GeneratePropertyForString(descriptor.Id));

            // We map the sample timestamp to its own column so it doesn't conflict with the built in column.
            // We want to ensure that timestamp values for returned metrics are precise and monotonically
            // increasing when ordered results are returned. The built in timestamp doesn't guarantee this.
            entity.Properties.Add(SampleTimestampPropertyName, EntityProperty.GeneratePropertyForDateTimeOffset(metrics.Timestamp));

            return(TableOperation.Insert(entity));
        }
コード例 #2
0
 public DiagnosticEvent(string hostId, DateTime timestamp)
 {
     RowKey       = TableStorageHelpers.GetRowKey(timestamp);
     PartitionKey = $"{hostId}-{timestamp:yyyyMMdd}";
     Timestamp    = timestamp;
 }
        public async Task ReadWriteMetrics_IntegerConversion_HandlesLongs()
        {
            var monitor1 = new TestScaleMonitor1();
            var monitors = new IScaleMonitor[] { monitor1 };

            // first write a couple entities manually to the table to simulate
            // the change in entity property type (int -> long)
            // this shows that the table can have entities of both formats with
            // no versioning issues

            // add an entity with Count property of type int
            var entity = new DynamicTableEntity
            {
                RowKey       = TableStorageHelpers.GetRowKey(DateTime.UtcNow),
                PartitionKey = TestHostId,
                Properties   = new Dictionary <string, EntityProperty>()
            };
            var expectedIntCountValue = int.MaxValue;

            entity.Properties.Add("Timestamp", new EntityProperty(DateTime.UtcNow));
            entity.Properties.Add("Count", new EntityProperty(expectedIntCountValue));
            entity.Properties.Add(TableStorageScaleMetricsRepository.MonitorIdPropertyName, EntityProperty.GeneratePropertyForString(monitor1.Descriptor.Id));
            var batch = new TableBatchOperation();

            batch.Add(TableOperation.Insert(entity));

            // add an entity with Count property of type long
            entity = new DynamicTableEntity
            {
                RowKey       = TableStorageHelpers.GetRowKey(DateTime.UtcNow),
                PartitionKey = TestHostId,
                Properties   = new Dictionary <string, EntityProperty>()
            };
            var expectedLongCountValue = long.MaxValue;

            entity.Properties.Add("Timestamp", new EntityProperty(DateTime.UtcNow));
            entity.Properties.Add("Count", new EntityProperty(expectedLongCountValue));
            entity.Properties.Add(TableStorageScaleMetricsRepository.MonitorIdPropertyName, EntityProperty.GeneratePropertyForString(monitor1.Descriptor.Id));
            batch.Add(TableOperation.Insert(entity));

            await _repository.ExecuteBatchSafeAsync(batch);

            // push a long max value through serialization
            var metricsMap = new Dictionary <IScaleMonitor, ScaleMetrics>();

            metricsMap.Add(monitor1, new TestScaleMetrics1 {
                Count = long.MaxValue
            });
            await _repository.WriteMetricsAsync(metricsMap);

            // add one more
            metricsMap = new Dictionary <IScaleMonitor, ScaleMetrics>();
            metricsMap.Add(monitor1, new TestScaleMetrics1 {
                Count = 12345
            });
            await _repository.WriteMetricsAsync(metricsMap);

            // read the metrics back
            var result = await _repository.ReadMetricsAsync(monitors);

            Assert.Equal(1, result.Count);
            var monitorMetricsList = result[monitor1];

            Assert.Equal(4, monitorMetricsList.Count);

            // verify the explicitly written int record was read correctly
            var currSample = (TestScaleMetrics1)monitorMetricsList[0];

            Assert.Equal(expectedIntCountValue, currSample.Count);

            // verify the explicitly written long record was read correctly
            currSample = (TestScaleMetrics1)monitorMetricsList[1];
            Assert.Equal(expectedLongCountValue, currSample.Count);

            // verify the final roundtripped values
            currSample = (TestScaleMetrics1)monitorMetricsList[2];
            Assert.Equal(long.MaxValue, currSample.Count);

            currSample = (TestScaleMetrics1)monitorMetricsList[3];
            Assert.Equal(12345, currSample.Count);
        }