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();
}
}
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;
}
}
Aggregations