Java源码示例:io.opencensus.trace.Annotation
示例1
public static ParentSpan startExplicitParentSpan(String name) {
Span span;
if (enabledExporter) {
span = tracer.spanBuilderWithExplicitParent(name, null).startSpan();
} else {
span = tracer.spanBuilderWithExplicitParent(name, null).setRecordEvents(true).startSpan();
}
span.addAnnotation(getBaseAnnotation());
Annotation vmAnno =
Annotation.fromDescriptionAndAttributes("java.vm properties", javaAttributeMap);
span.addAnnotation(vmAnno);
Annotation osAnno = Annotation.fromDescriptionAndAttributes("os properties", osAttributeMap);
span.addAnnotation(osAnno);
return new ParentSpan(span, name);
}
示例2
@Test
public void testTracingException() {
final MockSpan span = new MockSpan();
final Foo delegate = new ThrowingFooImpl();
final Foo proxy = TracingProxy.instrument(Foo.class, delegate, tracer);
when(tracer.spanBuilder("ThrowingFooImpl.bar")).thenReturn(spanBuilder);
when(spanBuilder.startSpan()).thenReturn(span);
try {
proxy.bar();
fail();
} catch (Exception ignore) {
}
verify(tracer).spanBuilder("ThrowingFooImpl.bar");
verify(spanBuilder).startSpan();
assertThat(span.ended, is(true));
assertThat(span.status, is(UNKNOWN));
assertThat(span.annotations, contains(Annotation.fromDescription("Exception thrown")));
}
示例3
@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)));
}
}
示例4
@Test
public void handlesException() {
// When
Sample sample = (Sample) context.getBean("sample");
try {
sample.boom();
} catch (Exception ignored) {
// ok
}
// Then
List<SpanData> spanList = handler.waitForExport(1);
assertThat(spanList).isNotNull();
assertThat(spanList.size()).isEqualTo(1);
SpanData spanData = spanList.get(0);
assertThat(spanData.getName()).isEqualTo("boom");
assertThat(spanData.getStatus()).isEqualTo(Status.UNKNOWN);
SpanData.TimedEvents<Annotation> annotations = spanData.getAnnotations();
assertThat(annotations).isNotNull();
List<SpanData.TimedEvent<Annotation>> events = annotations.getEvents();
assertThat(events.size()).isEqualTo(1);
assertThat(events.get(0).getEvent().getDescription()).isEqualTo("error");
}
示例5
@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);
}
示例6
@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);
}
示例7
@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);
}
示例8
private static Annotation getBaseAnnotation() {
if (isNull(meghanadaAnnotation)) {
String version = System.getProperty("meghanada-server.version", "");
String uid = System.getProperty("meghanada-server.uid", "");
Map<String, AttributeValue> m = new HashMap<>(2);
m.put("meghanada-server.version", AttributeValue.stringAttributeValue(version));
m.put("meghanada-server.uid", AttributeValue.stringAttributeValue(uid));
meghanadaAnnotation = Annotation.fromDescriptionAndAttributes("meghanada properties", m);
}
return meghanadaAnnotation;
}
示例9
public static void setStatusINTERNAL(String message) {
// add error info annotation
Span current = tracer.getCurrentSpan();
current.addAnnotation(getBaseAnnotation());
HashMap<String, AttributeValue> newMap = new HashMap<>(javaAttributeMap);
newMap.put("java.vm.memory", AttributeValue.stringAttributeValue(Config.getMemoryString()));
Annotation vmAnno = Annotation.fromDescriptionAndAttributes("java.vm properties", newMap);
current.addAnnotation(vmAnno);
Annotation osAnno = Annotation.fromDescriptionAndAttributes("os properties", osAttributeMap);
current.addAnnotation(osAnno);
TelemetryUtils.setStatus(Status.INTERNAL.withDescription(message));
}
示例10
/**
* Returns a new immutable {@code SpanData}.
*
* @deprecated Use {@link #create(SpanContext, SpanId, Boolean, String, Kind, Timestamp,
* Attributes, TimedEvents, TimedEvents, Links, Integer, Status, Timestamp)}.
*/
@Deprecated
public static SpanData create(
SpanContext context,
@Nullable SpanId parentSpanId,
@Nullable Boolean hasRemoteParent,
String name,
Timestamp startTimestamp,
Attributes attributes,
TimedEvents<Annotation> annotations,
TimedEvents<? extends io.opencensus.trace.BaseMessageEvent> messageOrNetworkEvents,
Links links,
@Nullable Integer childSpanCount,
@Nullable Status status,
@Nullable Timestamp endTimestamp) {
return create(
context,
parentSpanId,
hasRemoteParent,
name,
null,
startTimestamp,
attributes,
annotations,
messageOrNetworkEvents,
links,
childSpanCount,
status,
endTimestamp);
}
示例11
@Test
public void spanData_AllDataEmpty() {
SpanData spanData =
SpanData.create(
spanContext,
parentSpanId,
false,
SPAN_NAME,
null,
startTimestamp,
Attributes.create(Collections.<String, AttributeValue>emptyMap(), 0),
TimedEvents.create(Collections.<SpanData.TimedEvent<Annotation>>emptyList(), 0),
TimedEvents.create(Collections.<SpanData.TimedEvent<MessageEvent>>emptyList(), 0),
Links.create(Collections.<Link>emptyList(), 0),
0,
status,
endTimestamp);
assertThat(spanData.getContext()).isEqualTo(spanContext);
assertThat(spanData.getParentSpanId()).isEqualTo(parentSpanId);
assertThat(spanData.getHasRemoteParent()).isFalse();
assertThat(spanData.getName()).isEqualTo(SPAN_NAME);
assertThat(spanData.getStartTimestamp()).isEqualTo(startTimestamp);
assertThat(spanData.getAttributes().getAttributeMap().isEmpty()).isTrue();
assertThat(spanData.getAnnotations().getEvents().isEmpty()).isTrue();
assertThat(spanData.getNetworkEvents().getEvents().isEmpty()).isTrue();
assertThat(spanData.getMessageEvents().getEvents().isEmpty()).isTrue();
assertThat(spanData.getLinks().getLinks().isEmpty()).isTrue();
assertThat(spanData.getChildSpanCount()).isEqualTo(0);
assertThat(spanData.getStatus()).isEqualTo(status);
assertThat(spanData.getEndTimestamp()).isEqualTo(endTimestamp);
}
示例12
/** Add an annotation with an annotation. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Span addAnnotationWithAnnotation(Data data) {
Span span = data.annotationSpanAnnotation;
Annotation annotation =
Annotation.fromDescriptionAndAttributes(ANNOTATION_DESCRIPTION, data.attributeMap);
span.addAnnotation(annotation);
return span;
}
示例13
/** Create an Annotation. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Annotation createAnnotation(Data data) {
return Annotation.fromDescriptionAndAttributes(ANNOTATION_DESCRIPTION, data.attributeMap);
}
示例14
/**
* Returns an immutable representation of all the data from this {@code Span}.
*
* @return an immutable representation of all the data from this {@code Span}.
* @throws IllegalStateException if the Span doesn't have RECORD_EVENTS option.
*/
public SpanData toSpanData() {
synchronized (this) {
SpanData.Attributes attributesSpanData =
attributes == null
? SpanData.Attributes.create(Collections.<String, AttributeValue>emptyMap(), 0)
: SpanData.Attributes.create(attributes, attributes.getNumberOfDroppedAttributes());
SpanData.TimedEvents<Annotation> annotationsSpanData =
createTimedEvents(getInitializedAnnotations(), timestampConverter);
SpanData.TimedEvents<io.opencensus.trace.MessageEvent> messageEventsSpanData =
createTimedEvents(getInitializedNetworkEvents(), timestampConverter);
SpanData.Links linksSpanData =
links == null
? SpanData.Links.create(Collections.<Link>emptyList(), 0)
: SpanData.Links.create(
new ArrayList<Link>(links.events), links.getNumberOfDroppedEvents());
return SpanData.create(
getContext(),
parentSpanId,
hasRemoteParent,
name,
kind,
timestampConverter.convertNanoTime(startNanoTime),
attributesSpanData,
annotationsSpanData,
messageEventsSpanData,
linksSpanData,
numberOfChildren,
hasBeenEnded ? getStatusWithDefault() : null,
hasBeenEnded ? timestampConverter.convertNanoTime(endNanoTime) : null);
}
}
示例15
@Override
public void addAnnotation(Annotation annotation) {
Preconditions.checkNotNull(annotation, "annotation");
synchronized (this) {
if (hasBeenEnded) {
logger.log(Level.FINE, "Calling addAnnotation() on an ended Span.");
return;
}
getInitializedAnnotations()
.addEvent(new EventWithNanoTime<Annotation>(clock.nowNanos(), annotation));
}
}
示例16
@GuardedBy("this")
private TraceEvents<EventWithNanoTime<Annotation>> getInitializedAnnotations() {
if (annotations == null) {
annotations =
new TraceEvents<EventWithNanoTime<Annotation>>(traceParams.getMaxNumberOfAnnotations());
}
return annotations;
}
示例17
@Test
public void noEventsRecordedAfterEnd() {
RecordEventsSpanImpl span =
RecordEventsSpanImpl.startSpan(
spanContext,
SPAN_NAME,
null,
parentSpanId,
false,
TraceParams.DEFAULT,
startEndHandler,
timestampConverter,
testClock);
span.end();
// Check that adding trace events after Span#end() does not throw any exception and are not
// recorded.
span.putAttributes(attributes);
span.putAttribute(
"MySingleStringAttributeKey",
AttributeValue.stringAttributeValue("MySingleStringAttributeValue"));
span.addAnnotation(Annotation.fromDescription(ANNOTATION_DESCRIPTION));
span.addAnnotation(ANNOTATION_DESCRIPTION, attributes);
span.addNetworkEvent(
NetworkEvent.builder(NetworkEvent.Type.RECV, 1).setUncompressedMessageSize(3).build());
span.addLink(Link.fromSpanContext(spanContext, Link.Type.CHILD_LINKED_SPAN));
SpanData spanData = span.toSpanData();
assertThat(spanData.getStartTimestamp()).isEqualTo(timestamp);
assertThat(spanData.getAttributes().getAttributeMap()).isEmpty();
assertThat(spanData.getAnnotations().getEvents()).isEmpty();
assertThat(spanData.getNetworkEvents().getEvents()).isEmpty();
assertThat(spanData.getLinks().getLinks()).isEmpty();
assertThat(spanData.getStatus()).isEqualTo(Status.OK);
assertThat(spanData.getEndTimestamp()).isEqualTo(timestamp);
}
示例18
@Test
public void droppingAnnotations() {
final int maxNumberOfAnnotations = 8;
TraceParams traceParams =
TraceParams.DEFAULT.toBuilder().setMaxNumberOfAnnotations(maxNumberOfAnnotations).build();
RecordEventsSpanImpl span =
RecordEventsSpanImpl.startSpan(
spanContext,
SPAN_NAME,
null,
parentSpanId,
false,
traceParams,
startEndHandler,
timestampConverter,
testClock);
Annotation annotation = Annotation.fromDescription(ANNOTATION_DESCRIPTION);
for (int i = 0; i < 2 * maxNumberOfAnnotations; i++) {
span.addAnnotation(annotation);
testClock.advanceTime(Duration.create(0, 100));
}
SpanData spanData = span.toSpanData();
assertThat(spanData.getAnnotations().getDroppedEventsCount()).isEqualTo(maxNumberOfAnnotations);
assertThat(spanData.getAnnotations().getEvents().size()).isEqualTo(maxNumberOfAnnotations);
for (int i = 0; i < maxNumberOfAnnotations; i++) {
assertThat(spanData.getAnnotations().getEvents().get(i).getTimestamp())
.isEqualTo(timestamp.addNanos(100L * (maxNumberOfAnnotations + i)));
assertThat(spanData.getAnnotations().getEvents().get(i).getEvent()).isEqualTo(annotation);
}
span.end();
spanData = span.toSpanData();
assertThat(spanData.getAnnotations().getDroppedEventsCount()).isEqualTo(maxNumberOfAnnotations);
assertThat(spanData.getAnnotations().getEvents().size()).isEqualTo(maxNumberOfAnnotations);
for (int i = 0; i < maxNumberOfAnnotations; i++) {
assertThat(spanData.getAnnotations().getEvents().get(i).getTimestamp())
.isEqualTo(timestamp.addNanos(100L * (maxNumberOfAnnotations + i)));
assertThat(spanData.getAnnotations().getEvents().get(i).getEvent()).isEqualTo(annotation);
}
}
示例19
@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();
}
示例20
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();
}
示例21
private static Span.TimeEvents toTimeEventsProto(
TimedEvents<Annotation> annotationTimedEvents,
TimedEvents<io.opencensus.trace.MessageEvent> messageEventTimedEvents) {
Span.TimeEvents.Builder timeEventsBuilder = Span.TimeEvents.newBuilder();
timeEventsBuilder.setDroppedAnnotationsCount(annotationTimedEvents.getDroppedEventsCount());
for (TimedEvent<Annotation> annotation : annotationTimedEvents.getEvents()) {
timeEventsBuilder.addTimeEvent(toTimeAnnotationProto(annotation));
}
timeEventsBuilder.setDroppedMessageEventsCount(messageEventTimedEvents.getDroppedEventsCount());
for (TimedEvent<io.opencensus.trace.MessageEvent> networkEvent :
messageEventTimedEvents.getEvents()) {
timeEventsBuilder.addTimeEvent(toTimeMessageEventProto(networkEvent));
}
return timeEventsBuilder.build();
}
示例22
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();
}
示例23
private static Span.TimeEvents toTimeEventsProto(
TimedEvents<Annotation> annotationTimedEvents,
TimedEvents<io.opencensus.trace.MessageEvent> messageEventTimedEvents) {
Span.TimeEvents.Builder timeEventsBuilder = Span.TimeEvents.newBuilder();
timeEventsBuilder.setDroppedAnnotationsCount(annotationTimedEvents.getDroppedEventsCount());
for (TimedEvent<Annotation> annotation : annotationTimedEvents.getEvents()) {
timeEventsBuilder.addTimeEvent(toTimeAnnotationProto(annotation));
}
timeEventsBuilder.setDroppedMessageEventsCount(messageEventTimedEvents.getDroppedEventsCount());
for (TimedEvent<io.opencensus.trace.MessageEvent> networkEvent :
messageEventTimedEvents.getEvents()) {
timeEventsBuilder.addTimeEvent(toTimeMessageEventProto(networkEvent));
}
return timeEventsBuilder.build();
}
示例24
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();
}
示例25
@Test
public void convertErrorSpanDataToJaegerThriftSpan() throws SenderException {
long startTime = 1519629870001L;
long endTime = 1519630148002L;
String statusMessage = "timeout";
SpanData spanData =
SpanData.create(
sampleSpanContext(),
SpanId.fromBytes(new byte[] {(byte) 0x7F, FF, FF, FF, FF, FF, FF, FF}),
true,
"test",
Kind.SERVER,
Timestamp.fromMillis(startTime),
SpanData.Attributes.create(Collections.<String, AttributeValue>emptyMap(), 0),
SpanData.TimedEvents.create(Collections.<TimedEvent<Annotation>>emptyList(), 0),
SpanData.TimedEvents.create(Collections.<TimedEvent<MessageEvent>>emptyList(), 0),
SpanData.Links.create(Collections.<Link>emptyList(), 0),
0,
Status.DEADLINE_EXCEEDED.withDescription(statusMessage),
Timestamp.fromMillis(endTime));
handler.export(singletonList(spanData));
verify(mockSender).send(eq(process), captor.capture());
List<Span> spans = captor.getValue();
assertThat(spans.size()).isEqualTo(1);
Span span = spans.get(0);
assertThat(span.tags.size()).isEqualTo(3);
assertThat(span.tags)
.containsExactly(
new Tag(JaegerExporterHandler.SPAN_KIND, TagType.STRING).setVStr("server"),
new Tag(JaegerExporterHandler.STATUS_CODE, TagType.LONG).setVLong(4),
new Tag(JaegerExporterHandler.STATUS_MESSAGE, TagType.STRING).setVStr(statusMessage));
}
示例26
private static SpanData.TimedEvent<Annotation> sampleAnnotation() {
return SpanData.TimedEvent.create(
Timestamp.create(1519629872L, 987654321),
Annotation.fromDescriptionAndAttributes(
"annotation #1",
ImmutableMap.of(
"bool", AttributeValue.booleanAttributeValue(true),
"long", AttributeValue.longAttributeValue(1337L),
"string",
AttributeValue.stringAttributeValue(
"Kind words do not cost much. Yet they accomplish much. -- Pascal"))));
}
示例27
@Override
public void addAnnotation(Annotation annotation) {}
示例28
private static void addAnnotaion(Annotation annotation) {
Span current = tracer.getCurrentSpan();
current.addAnnotation(annotation);
}
示例29
public static void addAnnotation(Annotation annotation) {
TelemetryUtils.addAnnotaion(annotation);
}
示例30
public Annotation build(String description) {
return Annotation.fromDescriptionAndAttributes(description, this.map);
}