@Test public void convertSpanToKeyValueFormat_and_fromKeyValueString_escapes_and_unescapes_tag_keys_as_expected() { // given String unescapedTagKey = "fookey=blah withspaceandequals,andcomma"; String tagValue = UUID.randomUUID().toString(); Span span = Span.newBuilder("someSpan", SpanPurpose.CLIENT) .withTag(unescapedTagKey, tagValue) .build(); String expectedEscapedTagKey = "fookey\\u003Dblah\\u0020withspaceandequals\\u002Candcomma"; // when String keyValueStr = SpanParser.convertSpanToKeyValueFormat(span); // then assertThat(keyValueStr).contains("," + SpanParser.KEY_VALUE_TAG_PREFIX + expectedEscapedTagKey + "=\"" + tagValue + "\""); // and when Span deserialized = SpanParser.fromKeyValueString(keyValueStr); // then verifySpanDeepEquals(deserialized, span, true); } }
.withDurationNanos(durationNanosForFullyCompletedSpan) .withTags(tags) .withTag(extraTagKey, extraTagValue) .withTags(evenMoreTags) .withTimestampedAnnotations(annotations)
@UseDataProvider("escapedAndUnescapedQuotesBeforeKeyOrValueEndScenarioDataProvider") @Test public void fromJSON_properly_handles_escaped_quotes_and_unescaped_quotes_preceded_by_backslashes( EscapedAndUnescapedQuotesBeforeKeyOrValueEndScenario scenario ) { // given Span span = Span.newBuilder("someSpan", SpanPurpose.CLIENT) .withTag(scenario.unescapedKey, scenario.unescapedValue) .withTimestampedAnnotation( TimestampedAnnotation.forEpochMicros(1234, scenario.unescapedValue) ) .build(); String json = SpanParser.convertSpanToJSON(span); // when Span result = SpanParser.fromJSON(json); // then assertThat(result.getTags().get(scenario.unescapedKey)).isEqualTo(scenario.unescapedValue); assertThat(result.getTimestampedAnnotations().get(0).getValue()).isEqualTo(scenario.unescapedValue); }
@UseDataProvider("badJsonScenarioDataProvider") @Test public void fromJson_should_return_null_for_garbage_input(BadJsonScenario scenario) { // given: garbage input String garbageInput = scenario.generateBadJson( Span.newBuilder("foo", SpanPurpose.CLIENT) .withTag("footag", "bar") .withTimestampedAnnotations(Arrays.asList( TimestampedAnnotation.forCurrentTime("fooAnnotation"), TimestampedAnnotation.forCurrentTime("barAnnotation") )) .build() ); // when: fromJson is called Span spanFromJson = SpanParser.fromJSON(garbageInput); // then: the return value should be null assertThat(spanFromJson).isNull(); }
@UseDataProvider("escapedAndUnescapedQuotesBeforeKeyOrValueEndScenarioDataProvider") @Test public void fromKeyValueString_properly_handles_escaped_quotes_and_unescaped_quotes_preceded_by_backslashes( EscapedAndUnescapedQuotesBeforeKeyOrValueEndScenario scenario ) { // given Span span = Span.newBuilder("someSpan", SpanPurpose.CLIENT) .withTag(scenario.unescapedKey, scenario.unescapedValue) .build(); String keyValueStr = SpanParser.convertSpanToKeyValueFormat(span); // when Span result = SpanParser.fromKeyValueString(keyValueStr); // then assertThat(result.getTags().get(scenario.unescapedKey)).isEqualTo(scenario.unescapedValue); }
@UseDataProvider("badKeyValueScenarioDataProvider") @Test public void fromKeyValueString_should_return_null_for_garbage_input(BadKeyValueScenario scenario) { // given: garbage input String garbageInput = scenario.generateBadKeyValueStr( Span.newBuilder("foo", SpanPurpose.CLIENT) .withTag("footag", "bar") .build() ); // when: fromKeyValueString is called Span spanFromKeyValStr = SpanParser.fromKeyValueString(garbageInput); // then: the return value should be null assertThat(spanFromKeyValStr).isNull(); }
@Test public void fromKeyValueString_delegates_to_span_parser() { // given Span span = Span.newBuilder("foo", SpanPurpose.CLIENT) .withTag("blahtag", UUID.randomUUID().toString()) .build(); String keyValueStr = span.toKeyValueString(); // when Span result = span.fromKeyValueString(keyValueStr); // then verifySpanDeepEquals(result, span, true); }
@Test public void fromJSON_delegates_to_span_parser() { // given Span span = Span.newBuilder("foo", SpanPurpose.CLIENT) .withTag("blahtag", UUID.randomUUID().toString()) .build(); String json = span.toJSON(); // when Span result = span.fromJSON(json); // then verifySpanDeepEquals(result, span, true); }