private Annotation sessionAnnotation(Session session) { AttributeValue sessionId = AttributeValue.stringAttributeValue(session.getName()); return Annotation.fromDescriptionAndAttributes( "Using Session", ImmutableMap.of("sessionId", sessionId)); }
private static String renderAnnotation(Annotation annotation) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(annotation.getDescription()); if (!annotation.getAttributes().isEmpty()) { stringBuilder.append(" "); stringBuilder.append(renderAttributes(annotation.getAttributes())); } return stringBuilder.toString(); }
@Test public void fromDescriptionAndAttributes_EmptyAttributes() { Annotation annotation = Annotation.fromDescriptionAndAttributes( "MyAnnotationText", Collections.<String, AttributeValue>emptyMap()); assertThat(annotation.getDescription()).isEqualTo("MyAnnotationText"); assertThat(annotation.getAttributes().size()).isEqualTo(0); }
@Test public void fromDescription() { Annotation annotation = Annotation.fromDescription("MyAnnotationText"); assertThat(annotation.getDescription()).isEqualTo("MyAnnotationText"); assertThat(annotation.getAttributes().size()).isEqualTo(0); }
@Test public void annotation_ToString() { Annotation annotation = Annotation.fromDescription("MyAnnotationText"); assertThat(annotation.toString()).contains("MyAnnotationText"); Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>(); attributes.put( "MyStringAttributeKey", AttributeValue.stringAttributeValue("MyStringAttributeValue")); annotation = Annotation.fromDescriptionAndAttributes("MyAnnotationText2", attributes); assertThat(annotation.toString()).contains("MyAnnotationText2"); assertThat(annotation.toString()).contains(attributes.toString()); } }
@Test public void sqlExecute() throws Exception { // When String sql = "select 1"; Sample sample = (Sample) context.getBean("sample"); sample.execute(sql); // Then List<SpanData> data = handler.waitForExport(1); assertThat(data).isNotNull(); assertThat(data.size()).isEqualTo(1); assertThat(data.get(0).getName()).isEqualTo("execute-4705ea0d"); // sql-{hash of sql statement} List<SpanData.TimedEvent<Annotation>> events = data.get(0).getAnnotations().getEvents(); assertThat(events.size()).isEqualTo(1); assertThat(events.get(0).getEvent().getDescription()).isEqualTo(sql); }
@Test(expected = NullPointerException.class) public void fromDescription_NullDescription() { Annotation.fromDescription(null); }
private static TimeEvent toTimeAnnotationProto(TimedEvent<Annotation> timedEvent) { TimeEvent.Builder timeEventBuilder = TimeEvent.newBuilder().setTime(toTimestampProto(timedEvent.getTimestamp())); Annotation annotation = timedEvent.getEvent(); timeEventBuilder.setAnnotation( TimeEvent.Annotation.newBuilder() .setDescription(toTruncatableStringProto(annotation.getDescription())) .setAttributes(toAttributesBuilderProto(annotation.getAttributes(), 0)) .build()); return timeEventBuilder.build(); }
@Test public void fromDescriptionAndAttributes() { Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>(); attributes.put( "MyStringAttributeKey", AttributeValue.stringAttributeValue("MyStringAttributeValue")); Annotation annotation = Annotation.fromDescriptionAndAttributes("MyAnnotationText", attributes); assertThat(annotation.getDescription()).isEqualTo("MyAnnotationText"); assertThat(annotation.getAttributes()).isEqualTo(attributes); }
@Test public void annotation_EqualsAndHashCode() { EqualsTester tester = new EqualsTester(); Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>(); attributes.put( "MyStringAttributeKey", AttributeValue.stringAttributeValue("MyStringAttributeValue")); tester .addEqualityGroup( Annotation.fromDescription("MyAnnotationText"), Annotation.fromDescriptionAndAttributes( "MyAnnotationText", Collections.<String, AttributeValue>emptyMap())) .addEqualityGroup( Annotation.fromDescriptionAndAttributes("MyAnnotationText", attributes), Annotation.fromDescriptionAndAttributes("MyAnnotationText", attributes)) .addEqualityGroup(Annotation.fromDescription("MyAnnotationText2")); tester.testEquals(); }
@Test public void sqlQuery() throws Exception { // When String sql = "select 2"; Sample sample = (Sample) context.getBean("sample"); sample.executeQuery(sql); // Then List<SpanData> data = handler.waitForExport(1); assertThat(data).isNotNull(); assertThat(data.size()).isEqualTo(1); assertThat(data.get(0).getName()).isEqualTo("executeQuery-4705ea0e"); SpanData.TimedEvents<Annotation> annotations = data.get(0).getAnnotations(); List<SpanData.TimedEvent<Annotation>> events = annotations.getEvents(); assertThat(events.size()).isEqualTo(1); assertThat(events.get(0).getEvent().getDescription()).isEqualTo(sql); }
@Test public void doNotCrash() { Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>(); attributes.put( "MyStringAttributeKey", AttributeValue.stringAttributeValue("MyStringAttributeValue")); Map<String, AttributeValue> multipleAttributes = new HashMap<String, AttributeValue>(); multipleAttributes.put( "MyStringAttributeKey", AttributeValue.stringAttributeValue("MyStringAttributeValue")); multipleAttributes.put("MyBooleanAttributeKey", AttributeValue.booleanAttributeValue(true)); multipleAttributes.put("MyLongAttributeKey", AttributeValue.longAttributeValue(123)); // Tests only that all the methods are not crashing/throwing errors. BlankSpan.INSTANCE.putAttribute( "MyStringAttributeKey2", AttributeValue.stringAttributeValue("MyStringAttributeValue2")); BlankSpan.INSTANCE.addAttributes(attributes); BlankSpan.INSTANCE.addAttributes(multipleAttributes); BlankSpan.INSTANCE.addAnnotation("MyAnnotation"); BlankSpan.INSTANCE.addAnnotation("MyAnnotation", attributes); BlankSpan.INSTANCE.addAnnotation("MyAnnotation", multipleAttributes); BlankSpan.INSTANCE.addAnnotation(Annotation.fromDescription("MyAnnotation")); BlankSpan.INSTANCE.addNetworkEvent(NetworkEvent.builder(NetworkEvent.Type.SENT, 1L).build()); BlankSpan.INSTANCE.addMessageEvent(MessageEvent.builder(MessageEvent.Type.SENT, 1L).build()); BlankSpan.INSTANCE.addLink( Link.fromSpanContext(SpanContext.INVALID, Link.Type.CHILD_LINKED_SPAN)); BlankSpan.INSTANCE.setStatus(Status.OK); BlankSpan.INSTANCE.end(EndSpanOptions.DEFAULT); BlankSpan.INSTANCE.end(); }
private static TimeEvent toTimeAnnotationProto(TimedEvent<Annotation> timedEvent) { TimeEvent.Builder timeEventBuilder = TimeEvent.newBuilder().setTime(toTimestampProto(timedEvent.getTimestamp())); Annotation annotation = timedEvent.getEvent(); timeEventBuilder.setAnnotation( TimeEvent.Annotation.newBuilder() .setDescription(toTruncatableStringProto(annotation.getDescription())) .setAttributes(toAttributesBuilderProto(annotation.getAttributes(), 0)) .build()); return timeEventBuilder.build(); }
private Annotation sessionAnnotation(Session session) { AttributeValue sessionId = AttributeValue.stringAttributeValue(session.getName()); return Annotation.fromDescriptionAndAttributes( "Using Session", ImmutableMap.of("sessionId", sessionId)); }
span.putAttributes(attributes); testClock.advanceTime(Duration.create(0, 100)); span.addAnnotation(Annotation.fromDescription(ANNOTATION_DESCRIPTION)); testClock.advanceTime(Duration.create(0, 100)); span.addAnnotation(ANNOTATION_DESCRIPTION, attributes); .isEqualTo(timestamp.addNanos(100)); assertThat(spanData.getAnnotations().getEvents().get(0).getEvent()) .isEqualTo(Annotation.fromDescription(ANNOTATION_DESCRIPTION)); assertThat(spanData.getAnnotations().getEvents().get(1).getTimestamp()) .isEqualTo(timestamp.addNanos(200)); assertThat(spanData.getAnnotations().getEvents().get(1).getEvent()) .isEqualTo(Annotation.fromDescriptionAndAttributes(ANNOTATION_DESCRIPTION, attributes)); assertThat(spanData.getNetworkEvents().getDroppedEventsCount()).isEqualTo(0); assertThat(spanData.getNetworkEvents().getEvents().size()).isEqualTo(1);
@Test public void sqlUpdate() throws Exception { // When String sql = "update content set value = 1"; Sample sample = (Sample) context.getBean("sample"); sample.executeUpdate(sql); // Then List<SpanData> data = handler.waitForExport(1); assertThat(data).isNotNull(); assertThat(data.size()).isEqualTo(1); assertThat(data.get(0).getName()).isEqualTo("executeUpdate-acaeb423"); List<SpanData.TimedEvent<Annotation>> events = data.get(0).getAnnotations().getEvents(); assertThat(events.size()).isEqualTo(1); assertThat(events.get(0).getEvent().getDescription()).isEqualTo(sql); } }
timestampConverter, testClock); Annotation annotation = Annotation.fromDescription(ANNOTATION_DESCRIPTION); for (int i = 0; i < 2 * maxNumberOfAnnotations; i++) { span.addAnnotation(annotation);
private static TimeEvent toTimeAnnotationProto(TimedEvent<Annotation> timedEvent) { TimeEvent.Builder timeEventBuilder = TimeEvent.newBuilder().setTime(toTimestampProto(timedEvent.getTimestamp())); Annotation annotation = timedEvent.getEvent(); timeEventBuilder.setAnnotation( TimeEvent.Annotation.newBuilder() .setDescription(toTruncatableStringProto(annotation.getDescription())) .setAttributes(toAttributesBuilderProto(annotation.getAttributes(), 0)) .build()); return timeEventBuilder.build(); }
@Override public void addAnnotation(String description, Map<String, AttributeValue> attributes) { Preconditions.checkNotNull(description, "description"); Preconditions.checkNotNull(attributes, "attribute"); synchronized (this) { if (hasBeenEnded) { logger.log(Level.FINE, "Calling addAnnotation() on an ended Span."); return; } getInitializedAnnotations() .addEvent( new EventWithNanoTime<Annotation>( clock.nowNanos(), Annotation.fromDescriptionAndAttributes(description, attributes))); } }
span.putAttributes(attributes); testClock.advanceTime(Duration.create(0, 100)); span.addAnnotation(Annotation.fromDescription(ANNOTATION_DESCRIPTION)); testClock.advanceTime(Duration.create(0, 100)); span.addAnnotation(ANNOTATION_DESCRIPTION, attributes); .isEqualTo(timestamp.addNanos(100)); assertThat(spanData.getAnnotations().getEvents().get(0).getEvent()) .isEqualTo(Annotation.fromDescription(ANNOTATION_DESCRIPTION)); assertThat(spanData.getAnnotations().getEvents().get(1).getTimestamp()) .isEqualTo(timestamp.addNanos(200)); assertThat(spanData.getAnnotations().getEvents().get(1).getEvent()) .isEqualTo(Annotation.fromDescriptionAndAttributes(ANNOTATION_DESCRIPTION, attributes)); assertThat(spanData.getNetworkEvents().getDroppedEventsCount()).isEqualTo(0); assertThat(spanData.getNetworkEvents().getEvents().size()).isEqualTo(1);