private static void recordMessageEvent( Span span, MessageEvent.Type type, int seqNo, long optionalWireSize, long optionalUncompressedSize) { MessageEvent.Builder eventBuilder = MessageEvent.builder(type, seqNo); if (optionalUncompressedSize != -1) { eventBuilder.setUncompressedMessageSize(optionalUncompressedSize); } if (optionalWireSize != -1) { eventBuilder.setCompressedMessageSize(optionalWireSize); } span.addMessageEvent(eventBuilder.build()); }
/** * A convenience to record a {@link MessageEvent} with given parameters. * * @param span the span which this {@code MessageEvent} will be added to. * @param id the id of the event. * @param type the {@code MessageEvent.Type} of the event. * @param uncompressedMessageSize size of the message before compressed (optional). * @param compressedMessageSize size of the message after compressed (optional). * @since 0.19 */ static void recordMessageEvent( Span span, long id, Type type, long uncompressedMessageSize, long compressedMessageSize) { MessageEvent messageEvent = MessageEvent.builder(type, id) .setUncompressedMessageSize(uncompressedMessageSize) .setCompressedMessageSize(compressedMessageSize) .build(); span.addMessageEvent(messageEvent); }
/** * A convenience to record a {@link MessageEvent} with given parameters. * * @param span the span which this {@code MessageEvent} will be added to. * @param id the id of the event. * @param type the {@code MessageEvent.Type} of the event. * @param uncompressedMessageSize size of the message before compressed (optional). * @param compressedMessageSize size of the message after compressed (optional). * @since 0.19 */ static void recordMessageEvent( Span span, long id, Type type, long uncompressedMessageSize, long compressedMessageSize) { MessageEvent messageEvent = MessageEvent.builder(type, id) .setUncompressedMessageSize(uncompressedMessageSize) .setCompressedMessageSize(compressedMessageSize) .build(); span.addMessageEvent(messageEvent); }
@Test(expected = NullPointerException.class) public void buildMessageEvent_NullType() { MessageEvent.builder(null, 1L).build(); }
@Test public void messageEvent_ToString() { MessageEvent messageEvent = MessageEvent.builder(MessageEvent.Type.SENT, 1L) .setUncompressedMessageSize(123L) .setCompressedMessageSize(63L) .build(); assertThat(messageEvent.toString()).contains("type=SENT"); assertThat(messageEvent.toString()).contains("messageId=1"); assertThat(messageEvent.toString()).contains("compressedMessageSize=63"); assertThat(messageEvent.toString()).contains("uncompressedMessageSize=123"); } }
@Test public void buildMessageEvent_WithCompressedMessageSize() { MessageEvent messageEvent = MessageEvent.builder(MessageEvent.Type.SENT, 1L).setCompressedMessageSize(123L).build(); assertThat(messageEvent.getType()).isEqualTo(MessageEvent.Type.SENT); assertThat(messageEvent.getMessageId()).isEqualTo(1L); assertThat(messageEvent.getCompressedMessageSize()).isEqualTo(123L); }
@Test public void buildMessageEvent_WithRequiredFields() { MessageEvent messageEvent = MessageEvent.builder(MessageEvent.Type.SENT, 1L).build(); assertThat(messageEvent.getType()).isEqualTo(MessageEvent.Type.SENT); assertThat(messageEvent.getMessageId()).isEqualTo(1L); assertThat(messageEvent.getUncompressedMessageSize()).isEqualTo(0L); }
@Test public void buildMessageEvent_WithUncompressedMessageSize() { MessageEvent messageEvent = MessageEvent.builder(MessageEvent.Type.SENT, 1L).setUncompressedMessageSize(123L).build(); assertThat(messageEvent.getType()).isEqualTo(MessageEvent.Type.SENT); assertThat(messageEvent.getMessageId()).isEqualTo(1L); assertThat(messageEvent.getUncompressedMessageSize()).isEqualTo(123L); }
@Test public void buildMessageEvent_WithAllValues() { MessageEvent messageEvent = MessageEvent.builder(MessageEvent.Type.RECEIVED, 1L) .setUncompressedMessageSize(123L) .setCompressedMessageSize(63L) .build(); assertThat(messageEvent.getType()).isEqualTo(MessageEvent.Type.RECEIVED); assertThat(messageEvent.getMessageId()).isEqualTo(1L); assertThat(messageEvent.getUncompressedMessageSize()).isEqualTo(123L); assertThat(messageEvent.getCompressedMessageSize()).isEqualTo(63L); }
private static SpanData.TimedEvent<MessageEvent> sampleMessageEvent() { return SpanData.TimedEvent.create( Timestamp.create(1519629871L, 123456789), MessageEvent.builder(MessageEvent.Type.SENT, 42L) .setCompressedMessageSize(69) .setUncompressedMessageSize(96) .build()); }
/** * Cast or convert a {@link io.opencensus.trace.BaseMessageEvent} to {@link * io.opencensus.trace.MessageEvent}. * * <p>Warning: if the input is a {@code io.opencensus.trace.NetworkEvent} and contains {@code * kernelTimestamp} information, this information will be dropped. * * @param event the {@code BaseMessageEvent} that is being cast or converted. * @return a {@code MessageEvent} representation of the input. */ public static io.opencensus.trace.MessageEvent asMessageEvent( io.opencensus.trace.BaseMessageEvent event) { Utils.checkNotNull(event, "event"); if (event instanceof io.opencensus.trace.MessageEvent) { return (io.opencensus.trace.MessageEvent) event; } io.opencensus.trace.NetworkEvent networkEvent = (io.opencensus.trace.NetworkEvent) event; io.opencensus.trace.MessageEvent.Type type = (networkEvent.getType() == io.opencensus.trace.NetworkEvent.Type.RECV) ? io.opencensus.trace.MessageEvent.Type.RECEIVED : io.opencensus.trace.MessageEvent.Type.SENT; return io.opencensus.trace.MessageEvent.builder(type, networkEvent.getMessageId()) .setUncompressedMessageSize(networkEvent.getUncompressedMessageSize()) .setCompressedMessageSize(networkEvent.getCompressedMessageSize()) .build(); }
@Test public void addMessageEventDefaultImplementation() { Span mockSpan = Mockito.mock(Span.class); MessageEvent messageEvent = MessageEvent.builder(MessageEvent.Type.SENT, 123) .setUncompressedMessageSize(456) .setCompressedMessageSize(789) .build(); NetworkEvent networkEvent = NetworkEvent.builder(NetworkEvent.Type.SENT, 123) .setUncompressedMessageSize(456) .setCompressedMessageSize(789) .build(); Mockito.doCallRealMethod().when(mockSpan).addMessageEvent(messageEvent); mockSpan.addMessageEvent(messageEvent); verify(mockSpan).addNetworkEvent(eq(networkEvent)); } }
@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(); }
@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. noRecordEventsSpan.putAttribute( "MyStringAttributeKey2", AttributeValue.stringAttributeValue("MyStringAttributeValue2")); noRecordEventsSpan.addAttributes(attributes); noRecordEventsSpan.addAttributes(multipleAttributes); noRecordEventsSpan.addAnnotation("MyAnnotation"); noRecordEventsSpan.addAnnotation("MyAnnotation", attributes); noRecordEventsSpan.addAnnotation("MyAnnotation", multipleAttributes); noRecordEventsSpan.addAnnotation(Annotation.fromDescription("MyAnnotation")); noRecordEventsSpan.addNetworkEvent(NetworkEvent.builder(NetworkEvent.Type.SENT, 1L).build()); noRecordEventsSpan.addMessageEvent(MessageEvent.builder(MessageEvent.Type.SENT, 1L).build()); noRecordEventsSpan.addLink( Link.fromSpanContext(SpanContext.INVALID, Link.Type.CHILD_LINKED_SPAN)); noRecordEventsSpan.setStatus(Status.OK); noRecordEventsSpan.end(EndSpanOptions.DEFAULT); noRecordEventsSpan.end(); } }