Search in sources :

Example 46 with Span

use of io.opencensus.trace.Span in project instrumentation-java by census-instrumentation.

the class InProcessSampledSpanStoreImplTest method addSpanNameToAllLatencyBuckets.

private void addSpanNameToAllLatencyBuckets(String spanName) {
    for (LatencyBucketBoundaries boundaries : LatencyBucketBoundaries.values()) {
        Span sampledSpan = createSampledSpan(spanName);
        Span notSampledSpan = createNotSampledSpan(spanName);
        if (boundaries.getLatencyLowerNs() < NUM_NANOS_PER_SECOND) {
            testClock.advanceTime(Duration.create(0, (int) boundaries.getLatencyLowerNs()));
        } else {
            testClock.advanceTime(Duration.create(boundaries.getLatencyLowerNs() / NUM_NANOS_PER_SECOND, (int) (boundaries.getLatencyLowerNs() % NUM_NANOS_PER_SECOND)));
        }
        sampledSpan.end();
        notSampledSpan.end();
    }
}
Also used : Span(io.opencensus.trace.Span) LatencyBucketBoundaries(io.opencensus.trace.export.SampledSpanStore.LatencyBucketBoundaries)

Example 47 with Span

use of io.opencensus.trace.Span in project instrumentation-java by census-instrumentation.

the class CreateMetricDescriptorExporter method registerMetricDescriptor.

// Returns true if the given metricDescriptor is successfully registered to Stackdriver
// Monitoring, or the
// exact same metric has already been registered. Returns false otherwise.
private boolean registerMetricDescriptor(io.opencensus.metrics.export.MetricDescriptor metricDescriptor) {
    String metricName = metricDescriptor.getName();
    io.opencensus.metrics.export.MetricDescriptor existingMetricDescriptor = registeredMetricDescriptors.get(metricName);
    if (existingMetricDescriptor != null) {
        if (existingMetricDescriptor.equals(metricDescriptor)) {
            // Ignore metricDescriptor that are already registered.
            return true;
        } else {
            logger.log(Level.WARNING, "A different metric with the same name is already registered: " + existingMetricDescriptor);
            return false;
        }
    }
    registeredMetricDescriptors.put(metricName, metricDescriptor);
    if (isBuiltInMetric(metricName)) {
        // skip creating metric descriptor for stackdriver built-in metrics.
        return true;
    }
    Span span = tracer.getCurrentSpan();
    span.addAnnotation("Create Stackdriver Metric.");
    MetricDescriptor stackDriverMetricDescriptor = StackdriverExportUtils.createMetricDescriptor(metricDescriptor, projectId, domain, displayNamePrefix, constantLabels);
    CreateMetricDescriptorRequest request = CreateMetricDescriptorRequest.newBuilder().setName(projectName.toString()).setMetricDescriptor(stackDriverMetricDescriptor).build();
    try {
        metricServiceClient.createMetricDescriptor(request);
        span.addAnnotation("Finish creating MetricDescriptor.");
        return true;
    } catch (ApiException e) {
        logger.log(Level.WARNING, "ApiException thrown when creating MetricDescriptor.", e);
        span.setStatus(Status.CanonicalCode.valueOf(e.getStatusCode().getCode().name()).toStatus().withDescription("ApiException thrown when creating MetricDescriptor: " + StackdriverExportUtils.exceptionMessage(e)));
        return false;
    } catch (Throwable e) {
        logger.log(Level.WARNING, "Exception thrown when creating MetricDescriptor.", e);
        span.setStatus(Status.UNKNOWN.withDescription("Exception thrown when creating MetricDescriptor: " + StackdriverExportUtils.exceptionMessage(e)));
        return false;
    }
}
Also used : MetricDescriptor(com.google.api.MetricDescriptor) CreateMetricDescriptorRequest(com.google.monitoring.v3.CreateMetricDescriptorRequest) Span(io.opencensus.trace.Span) ApiException(com.google.api.gax.rpc.ApiException)

Aggregations

Span (io.opencensus.trace.Span)47 Test (org.junit.Test)17 Benchmark (org.openjdk.jmh.annotations.Benchmark)14 BenchmarkMode (org.openjdk.jmh.annotations.BenchmarkMode)14 OutputTimeUnit (org.openjdk.jmh.annotations.OutputTimeUnit)14 Scope (io.opencensus.common.Scope)8 BlankSpan (io.opencensus.trace.BlankSpan)8 ArrayList (java.util.ArrayList)4 SpanBuilder (io.opencensus.trace.SpanBuilder)3 SpanContext (io.opencensus.trace.SpanContext)3 ApiException (com.google.api.gax.rpc.ApiException)2 ByteString (com.google.protobuf.ByteString)2 HttpRequestContext (io.opencensus.contrib.http.HttpRequestContext)2 Metric (io.opencensus.metrics.export.Metric)2 TagContext (io.opencensus.tags.TagContext)2 AttributeValue (io.opencensus.trace.AttributeValue)2 SpanId (io.opencensus.trace.SpanId)2 TraceId (io.opencensus.trace.TraceId)2 TraceConfig (io.opencensus.trace.config.TraceConfig)2 TraceParams (io.opencensus.trace.config.TraceParams)2