Java Code Examples for io.opencensus.trace.Span#addAnnotation()

The following examples show how to use io.opencensus.trace.Span#addAnnotation() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DefaultRateLimitedCache.java    From heroic with Apache License 2.0 6 votes vote down vote up
public boolean acquire(K key, final Runnable cacheHit) {
    try (Scope ss = tracer.spanBuilder("DefaultRateLimitedCache").startScopedSpan()) {
        Span span = tracer.getCurrentSpan();

        if (cache.get(key) != null) {
            cacheHit.run();
            span.addAnnotation("Found key in cache");
            return false;
        }

        span.addAnnotation("Acquiring rate limiter");
        rateLimiter.acquire();
        span.addAnnotation("Acquired rate limiter");

        if (cache.putIfAbsent(key, true) != null) {
            cacheHit.run();
            return false;
        }

        return true;
    }
}
 
Example 2
Source File: BigtableMutatorImpl.java    From heroic with Apache License 2.0 6 votes vote down vote up
private BulkMutation getOrAddBulkMutation(String tableName) {
    final Span span = tracer.spanBuilder("BigtableMutator.getOrAddBulkMutation").startSpan();
    try (Scope ws = tracer.withSpan(span)) {
        span.addAnnotation("Acquiring lock");
        synchronized (tableAccessLock) {
            span.addAnnotation("Lock acquired");

            if (tableToBulkMutation.containsKey(tableName)) {
                span.setStatus(Status.ALREADY_EXISTS.withDescription("Mutation exists in map"));
                span.end();
                return tableToBulkMutation.get(tableName);
            }

            final BulkMutation bulkMutation = session.createBulkMutation(
                session
                    .getOptions()
                    .getInstanceName()
                    .toTableName(tableName));

            tableToBulkMutation.put(tableName, bulkMutation);

            span.end();
            return bulkMutation;
        }
    }
}
 
Example 3
Source File: OcAgentTraceExporterIntegrationTest.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
private void doWork(String spanName, int i) {
  try (Scope scope = tracer.spanBuilder(spanName).startScopedSpan()) {
    // Simulate some work.
    Span span = tracer.getCurrentSpan();

    try {
      Thread.sleep(10L);
    } catch (InterruptedException e) {
      span.setStatus(Status.INTERNAL.withDescription(e.toString()));
    }

    Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>();
    attributes.put("inner work iteration number", AttributeValue.longAttributeValue(i));
    span.addAnnotation("Invoking doWork", attributes);
  }
}
 
Example 4
Source File: Handler.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
static Object proceed(
    ProceedingJoinPoint call, Tracer tracer, String spanName, String... annotations)
    throws Throwable {
  Scope scope = tracer.spanBuilder(spanName).startScopedSpan();
  try {
    for (String annotation : annotations) {
      tracer.getCurrentSpan().addAnnotation(annotation);
    }

    return call.proceed();

  } catch (Throwable t) {
    Map<String, AttributeValue> attributes = new HashMap<String, AttributeValue>();
    String message = t.getMessage();
    attributes.put(
        "message", AttributeValue.stringAttributeValue(message == null ? "null" : message));
    attributes.put("type", AttributeValue.stringAttributeValue(t.getClass().toString()));

    Span span = tracer.getCurrentSpan();
    span.addAnnotation("error", attributes);
    span.setStatus(Status.UNKNOWN);
    throw t;
  } finally {
    scope.close();
  }
}
 
Example 5
Source File: HelloWorldServer.java    From opencensus-java with Apache License 2.0 6 votes vote down vote up
@Override
public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
  Span span = tracer.getCurrentSpan();
  span.putAttribute("my_attribute", AttributeValue.stringAttributeValue("red"));
  span.addAnnotation(
      "Constructing greeting.",
      ImmutableMap.of(
          "name", AttributeValue.stringAttributeValue(req.getName()),
          "name length", AttributeValue.longAttributeValue(req.getName().length())));
  sleepFor(10);
  performWork(span);
  span.addAnnotation("Sleeping.");
  sleepFor(30);
  HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build();
  responseObserver.onNext(reply);
  responseObserver.onCompleted();
  logger.info("SayHello RPC handled.");
}
 
Example 6
Source File: TelemetryUtils.java    From meghanada-server with GNU General Public License v3.0 6 votes vote down vote up
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);
}
 
Example 7
Source File: SpanOperationsBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Add an annotation as description only. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Span addAnnotationEmpty(Data data) {
  Span span = data.annotationSpanEmpty;
  span.addAnnotation(ANNOTATION_DESCRIPTION);
  return span;
}
 
Example 8
Source File: CoreIngestionGroup.java    From heroic with Apache License 2.0 5 votes vote down vote up
protected AsyncFuture<Ingestion> syncWrite(final Request request) {
    final Span span = tracer.spanBuilder("CoreIngestionGroup.syncWrite").startSpan();

    if (!filter.get().apply(request.getSeries())) {
        reporter.reportDroppedByFilter();
        span.setStatus(Status.FAILED_PRECONDITION.withDescription("Dropped by filter"));
        span.end();
        return async.resolved(Ingestion.of(ImmutableList.of()));
    }

    try {
        span.addAnnotation("Acquiring write lock");
        writePermits.acquire();
    } catch (final InterruptedException e) {
        String error = "Failed to acquire semaphore for bounded request";
        span.setStatus(Status.INTERNAL.withDescription(error));
        span.end();
        return async.failed(new Exception(error, e));
    }

    span.addAnnotation("Acquired write lock");
    reporter.incrementConcurrentWrites();

    try (Scope ws = tracer.withSpan(span)) {
        return doWrite(request).onFinished(() -> {
            writePermits.release();
            reporter.decrementConcurrentWrites();
            span.end();
        });
    }
}
 
Example 9
Source File: BigtableMutatorImpl.java    From heroic with Apache License 2.0 5 votes vote down vote up
private AsyncFuture<Void> mutateBatchRow(
    String tableName, ByteString rowKey, Mutations mutations
) {
    final Span span = tracer.spanBuilder("BigtableMutator.mutateBatchRow").startSpan();
    try (Scope ws = tracer.withSpan(span)) {
        span.putAttribute("table", AttributeValue.stringAttributeValue(tableName));

        final BulkMutation bulkMutation = getOrAddBulkMutation(tableName);

        span.addAnnotation("Adding rows to bulk mutation");
        return convertVoid(bulkMutation.add(toMutateRowRequest(tableName, rowKey, mutations)))
            .onFinished(span::end);
    }
}
 
Example 10
Source File: SpanOperationsBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** 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;
}
 
Example 11
Source File: SpanOperationsBenchmark.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
/** Add an annotation with attributes. */
@Benchmark
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public Span addAnnotationWithAttributes(Data data) {
  Span span = data.annotationSpanAttributes;
  span.addAnnotation(ANNOTATION_DESCRIPTION, data.attributeMap);
  return span;
}
 
Example 12
Source File: HelloWorldServer.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static void sleepFor(int milliseconds) {
  try {
    Thread.sleep(milliseconds);
  } catch (InterruptedException e) {
    Span span = tracer.getCurrentSpan();
    span.addAnnotation("Exception thrown when performing work " + e.getMessage());
    span.setStatus(Status.UNKNOWN);
  }
}
 
Example 13
Source File: HelloWorldServer.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static void performWork(Span parent) {
  SpanBuilder spanBuilder =
      tracer
          .spanBuilderWithExplicitParent("internal_work", parent)
          .setRecordEvents(true)
          .setSampler(Samplers.alwaysSample());
  try (Scope scope = spanBuilder.startScopedSpan()) {
    Span span = tracer.getCurrentSpan();
    span.putAttribute("my_attribute", AttributeValue.stringAttributeValue("blue"));
    span.addAnnotation("Performing work.");
    sleepFor(20); // Working hard here.
    span.addAnnotation("Done work.");
  }
}
 
Example 14
Source File: MultiSpansTracing.java    From opencensus-java with Apache License 2.0 5 votes vote down vote up
private static void doWork() {
  Span rootSpan = tracer.spanBuilderWithExplicitParent("MyRootSpan", null).startSpan();
  rootSpan.addAnnotation("Annotation to the root Span before child is created.");
  Span childSpan = tracer.spanBuilderWithExplicitParent("MyChildSpan", rootSpan).startSpan();
  childSpan.addAnnotation("Annotation to the child Span");
  childSpan.end();
  rootSpan.addAnnotation("Annotation to the root Span after child is ended.");
  rootSpan.end();
}
 
Example 15
Source File: TelemetryUtils.java    From meghanada-server with GNU General Public License v3.0 5 votes vote down vote up
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));
}
 
Example 16
Source File: TelemetryUtils.java    From meghanada-server with GNU General Public License v3.0 4 votes vote down vote up
private static void addAnnotaion(String message) {
  Span current = tracer.getCurrentSpan();
  current.addAnnotation(message);
}
 
Example 17
Source File: TelemetryUtils.java    From meghanada-server with GNU General Public License v3.0 4 votes vote down vote up
private static void addAnnotaion(Annotation annotation) {
  Span current = tracer.getCurrentSpan();
  current.addAnnotation(annotation);
}