Exemplo n.º 1
0
        public async Task Can_format_payload_with_multiple_fields_correctly()
        {
            // Arrange
            await using var ms         = new MemoryStream();
            await using var jsonWriter = new Utf8JsonWriter(ms);
            var fields = new Dictionary <string, object>
            {
                { "field1key", "field1value" },
                { "field2key", 2 },
                { "field3key", false }
            };
            var timestamp = new DateTime(2017, 1, 1, 1, 1, 1, DateTimeKind.Utc);
            var point     = new DatadogPoint(null, "measurement", fields, MetricTags.Empty, new DefaultDatadogMetricJsonWriter(), FlushInterval, timestamp);

            // Act
            jsonWriter.WriteStartArray();
            await point.Write(jsonWriter);

            jsonWriter.WriteEndArray();
            await jsonWriter.FlushAsync();

            var result = Encoding.UTF8.GetString(ms.ToArray());

            // Assert
            result.Should().Be("[{\"metric\":\"measurement.field1key\",\"points\":[[1483232461,0]],\"type\":\"gauge\",\"tags\":[]},{\"metric\":\"measurement.field2key\",\"points\":[[1483232461,2]],\"type\":\"gauge\",\"tags\":[]},{\"metric\":\"measurement.field3key\",\"points\":[[1483232461,0]],\"type\":\"gauge\",\"tags\":[]}]");
        }
Exemplo n.º 2
0
        public void At_least_one_field_is_required()
        {
            // Arrange
            var    fields = new Dictionary <string, object>();
            Action action = () =>
            {
                // Act
                var point = new DatadogPoint(null, "measurement", fields, MetricTags.Empty, new DefaultDatadogMetricJsonWriter(), FlushInterval);
            };

            // Assert
            action.Should().Throw <ArgumentException>();
        }
Exemplo n.º 3
0
        public void Field_key_cannot_be_empty()
        {
            // Arrange
            var fields = new Dictionary <string, object> {
                { string.Empty, "value" }
            };
            Action action = () =>
            {
                // Act
                var point = new DatadogPoint(null, "measurement", fields, MetricTags.Empty, new DefaultDatadogMetricJsonWriter(), FlushInterval);
            };

            // Assert
            action.Should().Throw <ArgumentException>();
        }
Exemplo n.º 4
0
        public async Task Should_add_context_when_provided()
        {
            // Arrange
            await using var ms         = new MemoryStream();
            await using var jsonWriter = new Utf8JsonWriter(ms);
            var fields = new Dictionary <string, object> {
                { "key", "value" }
            };
            var tags      = new MetricTags("tagkey", "tagvalue");
            var timestamp = new DateTime(2017, 1, 1, 1, 1, 1, DateTimeKind.Utc);
            var point     = new DatadogPoint("context", "measurement", fields, tags, new DefaultDatadogMetricJsonWriter(), FlushInterval, timestamp);

            // Act
            await point.Write(jsonWriter);

            await jsonWriter.FlushAsync();

            var result = Encoding.UTF8.GetString(ms.ToArray());

            // Assert
            result.Should().Be("{\"metric\":\"context.measurement.key\",\"points\":[[1483232461,0]],\"type\":\"gauge\",\"tags\":[\"tagkey:tagvalue\"]}", "Hosted Metrics request at the moment allow tags array but its not yet used.");
        }
Exemplo n.º 5
0
        public void Time_stamp_should_be_utc(DateTimeKind dateTimeKind, bool expected)
        {
            // Arrange
            var fields = new Dictionary <string, object> {
                { "key", "value" }
            };
            var timestamp = new DateTime(2017, 1, 1, 1, 1, 1, dateTimeKind);

            Action action = () =>
            {
                // Act
                var point = new DatadogPoint(null, "measurement", fields, MetricTags.Empty, new DefaultDatadogMetricJsonWriter(), FlushInterval, timestamp);
            };

            // Assert
            if (!expected)
            {
                action.Should().Throw <ArgumentException>();
            }
            else
            {
                action.Should().NotThrow();
            }
        }
Exemplo n.º 6
0
        /// <inheritdoc />
        public async Task Write(Utf8JsonWriter jsonWriter, DatadogPoint point, bool writeTimestamp = true)
        {
            if (jsonWriter == null)
            {
                throw new ArgumentNullException(nameof(jsonWriter));
            }

            var tagsDictionary = point.Tags.ToDictionary();

            var tags = tagsDictionary.Where(tag => !ExcludeTags.Contains(tag.Key));

            var utcTimestamp = point.UtcTimestamp ?? DateTime.UtcNow;

            tagsDictionary.TryGetValue("mtype", out var metricType);

            foreach (var f in point.Fields)
            {
                jsonWriter.WriteStartObject();

                var hasPrevious  = false;
                var metricWriter = new StringWriter();

                if (!string.IsNullOrWhiteSpace(point.Context))
                {
                    await metricWriter.WriteAsync(point.Context);

                    hasPrevious = true;
                }

                if (hasPrevious)
                {
                    await metricWriter.WriteAsync(".");
                }

                await metricWriter.WriteAsync(point.Measurement.Replace(' ', '_'));

                if (!string.IsNullOrWhiteSpace(metricType))
                {
                    await metricWriter.WriteAsync(".");

                    await metricWriter.WriteAsync(metricType);
                }

                await metricWriter.WriteAsync(".");

                await metricWriter.WriteAsync(f.Key);

                var metric = metricWriter.ToString();

                jsonWriter.WriteString("metric", metric);

                jsonWriter.WriteStartArray("points");

                jsonWriter.WriteStartArray();
                jsonWriter.WriteNumberValue(DatadogSyntax.FormatTimestamp(utcTimestamp));
                jsonWriter.WriteNumberValue(DatadogSyntax.FormatValue(f.Value, metric));
                jsonWriter.WriteEndArray();

                jsonWriter.WriteEndArray();

                var datadogMetricType = DatadogSyntax.FormatMetricType(metricType);
                jsonWriter.WriteString("type", datadogMetricType);

                if (datadogMetricType == DatadogSyntax.Rate || datadogMetricType == DatadogSyntax.Count)
                {
                    jsonWriter.WriteNumber("interval", point.FlushInterval.Seconds);
                }

                jsonWriter.WritePropertyName("tags");
                jsonWriter.WriteStartArray();

                foreach (var tag in tags)
                {
                    jsonWriter.WriteStringValue($"{tag.Key}:{tag.Value}");
                }

                jsonWriter.WriteEndArray();
                jsonWriter.WriteEndObject();
            }
        }