public void can_format_payload() { var nameFormatter = new DefaultGraphiteNameFormatter(); var payload = new GraphitePayload(); var fieldsOne = new Dictionary <string, object> { { "key", "value" } }; var timestampOne = new DateTime(2017, 1, 1, 1, 1, 1, DateTimeKind.Utc); var pointOne = new GraphitePoint(null, "measurement", fieldsOne, MetricTags.Empty, timestampOne); var fieldsTwo = new Dictionary <string, object> { { "field1key", "field1value" }, { "field2key", 2 }, { "field3key", false } }; var timestampTwo = new DateTime(2017, 1, 2, 1, 1, 1, DateTimeKind.Utc); var pointTwo = new GraphitePoint(null, "measurement", fieldsTwo, MetricTags.Empty, timestampTwo); payload.Add(pointOne); payload.Add(pointTwo); payload.Format(nameFormatter).Should() .Be( "measurement.key value 1483232461\nmeasurement.field1key field1value 1483318861\nmeasurement.field2key 2 1483318861\nmeasurement.field3key f 1483318861\n"); }
/// <inheritdoc /> public void Write(TextWriter textWriter, GraphitePoint point, bool writeTimestamp = true) { if (textWriter == null) { throw new ArgumentNullException(nameof(textWriter)); } var hasPrevious = false; var measurementWriter = new StringWriter(); measurementWriter.Write(_prefix); var tagsDictionary = point.Tags.ToDictionary(GraphiteSyntax.EscapeName); if (tagsDictionary.TryGetValue("mtype", out var metricType) && !string.IsNullOrWhiteSpace(metricType)) { measurementWriter.Write(metricType); hasPrevious = true; } if (hasPrevious) { measurementWriter.Write("."); } measurementWriter.Write(GraphiteSyntax.EscapeName(point.Measurement, true)); var tags = tagsDictionary.Where(tag => !ExcludeTags.Contains(tag.Key)); foreach (var tag in tags) { measurementWriter.Write('.'); measurementWriter.Write(GraphiteSyntax.EscapeName(tag.Key)); measurementWriter.Write('.'); measurementWriter.Write(tag.Value); } measurementWriter.Write('.'); var prefix = measurementWriter.ToString(); var utcTimestamp = point.UtcTimestamp ?? DateTime.UtcNow; foreach (var f in point.Fields) { textWriter.Write(prefix); textWriter.Write(GraphiteSyntax.EscapeName(f.Key)); textWriter.Write(' '); textWriter.Write(GraphiteSyntax.FormatValue(f.Value)); textWriter.Write(' '); textWriter.Write(GraphiteSyntax.FormatTimestamp(utcTimestamp)); textWriter.Write('\n'); } }
public void at_least_one_field_is_required() { var fields = new Dictionary <string, object>(); Action action = () => { var point = new GraphitePoint(null, "measurement", fields, MetricTags.Empty); }; action.ShouldThrow <ArgumentException>(); }
public void field_key_cannot_be_empty() { var fields = new Dictionary <string, object> { { string.Empty, "value" } }; Action action = () => { var point = new GraphitePoint(null, "measurement", fields, MetricTags.Empty); }; action.ShouldThrow <ArgumentException>(); }
public void measurement_is_required() { var fields = new Dictionary <string, object> { { "key", "value" } }; Action action = () => { var point = new GraphitePoint(null, string.Empty, fields, MetricTags.Empty); }; action.ShouldThrow <ArgumentException>(); }
public void At_least_one_field_is_required() { // Arrange var fields = new Dictionary <string, object>(); Action action = () => { // Act var point = new GraphitePoint(null, "measurement", fields, MetricTags.Empty, new DefaultGraphitePointTextWriter()); }; // Assert action.Should().Throw <ArgumentException>(); }
public void Measurement_is_required() { // Arrange var fields = new Dictionary <string, object> { { "key", "value" } }; Action action = () => { // Act var point = new GraphitePoint(null, string.Empty, fields, MetricTags.Empty, new DefaultGraphitePointTextWriter()); }; // Assert action.Should().Throw <ArgumentException>(); }
public void Field_key_cannot_be_empty() { // Arrange var fields = new Dictionary <string, object> { { string.Empty, "value" } }; Action action = () => { // Act var point = new GraphitePoint(null, "measurement", fields, MetricTags.Empty, new DefaultGraphitePointTextWriter()); }; // Assert action.Should().Throw <ArgumentException>(); }
public void can_format_payload_correctly() { var nameFormatter = new DefaultGraphiteNameFormatter(); var fields = new Dictionary <string, object> { { "key", "value" } }; var timestamp = new DateTime(2017, 1, 1, 1, 1, 1, DateTimeKind.Utc); var point = new GraphitePoint(null, "measurement", fields, MetricTags.Empty, timestamp); var paload = new GraphitePayload { point }; paload.Format(nameFormatter).Should().Be("measurement.key value 1483232461\n"); }
public void Can_format_payload_correctly() { // Arrange var textWriter = new StringWriter(); var fields = new Dictionary <string, object> { { "key", "value" } }; var timestamp = new DateTime(2017, 1, 1, 1, 1, 1, DateTimeKind.Utc); var point = new GraphitePoint(null, "measurement", fields, MetricTags.Empty, new DefaultGraphitePointTextWriter(), timestamp); // Act point.Write(textWriter); // Assert textWriter.ToString().Should().Be("measurement.key value 1483232461\n"); }
public void when_null_text_writer_ignore_and_dont_throw() { var payload = new GraphitePayload(); var fields = new Dictionary <string, object> { { "key", "value" } }; var pointOne = new GraphitePoint(null, "measurement", fields, MetricTags.Empty); Action action = () => { payload.Add(pointOne); payload.Format(null); }; action.ShouldNotThrow(); }
public void Should_add_context_when_provided() { // Arrange var textWriter = new StringWriter(); 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 GraphitePoint("context", "measurement", fields, tags, new DefaultGraphitePointTextWriter(), timestamp); // Act point.Write(textWriter); // Assert textWriter.ToString().Should().Be("context.measurement.tagkey.tagvalue.key value 1483232461\n"); }
public void Can_format_payload_with_multiple_fields_correctly() { // Arrange var textWriter = new StringWriter(); 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 GraphitePoint(null, "measurement", fields, MetricTags.Empty, new DefaultGraphitePointTextWriter(), timestamp); // Act point.WriteAsync(textWriter); // Assert textWriter.ToString().Should() .Be("measurement.field1key field1value 1483232461\nmeasurement.field2key 2 1483232461\nmeasurement.field3key f 1483232461\n"); }
public void time_stamp_should_be_utc(DateTimeKind dateTimeKind, bool expected) { var fields = new Dictionary <string, object> { { "key", "value" } }; var timestamp = new DateTime(2017, 1, 1, 1, 1, 1, dateTimeKind); Action action = () => { var point = new GraphitePoint(null, "measurement", fields, MetricTags.Empty, timestamp); }; if (!expected) { action.ShouldThrow <ArgumentException>(); } else { action.ShouldNotThrow(); } }
public IEnumerable <string> Format(GraphitePoint point) { var tags = point.Tags; var tagsDictionary = tags.Keys.Zip(tags.Values, (key, value) => new { key, value }) .ToDictionary(x => $"tag:{x.key}", x => GraphiteSyntax.EscapeName(x.value), StringComparer.OrdinalIgnoreCase); tagsDictionary["context"] = point.Context; tagsDictionary["name"] = point.Name; foreach (var field in point.Fields) { tagsDictionary["field"] = field.Key; var sb = new StringBuilder(Render(tagsDictionary)); sb.Append(' '); sb.Append(GraphiteSyntax.FormatValue(field.Value)); sb.Append(' '); sb.Append(GraphiteSyntax.FormatTimestamp(point.UtcTimestamp)); yield return(sb.ToString()); } }
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 GraphitePoint(null, "measurement", fields, MetricTags.Empty, new DefaultGraphitePointTextWriter(), timestamp); }; // Assert if (!expected) { action.Should().Throw <ArgumentException>(); } else { action.Should().NotThrow(); } }