use of zipkin.storage.cassandra3.Schema.TraceIdUDT in project zipkin by openzipkin.
the class CassandraSpanConsumer method accept.
/**
* This fans out into many requests, last count was 2 * spans.size. If any of these fail, the
* returned future will fail. Most callers drop or log the result.
*/
@Override
public ListenableFuture<Void> accept(List<Span> rawSpans) {
ImmutableSet.Builder<ListenableFuture<?>> futures = ImmutableSet.builder();
for (Span span : rawSpans) {
// indexing occurs by timestamp, so derive one if not present.
Long timestamp = guessTimestamp(span);
TraceIdUDT traceId = new TraceIdUDT(span.traceIdHigh, span.traceId);
boolean isServerRecvSpan = isServerRecvSpan(span);
futures.add(storeSpan(span, traceId, isServerRecvSpan, timestamp));
for (String serviceName : span.serviceNames()) {
// QueryRequest.min/maxDuration
if (timestamp != null) {
// Contract for Repository.storeTraceServiceSpanName is to store the span twice, once with
// the span name and another with empty string.
futures.add(storeTraceServiceSpanName(serviceName, span.name, timestamp, span.duration, traceId));
if (!span.name.isEmpty()) {
// If span.name == "", this would be redundant
futures.add(storeTraceServiceSpanName(serviceName, "", timestamp, span.duration, traceId));
}
futures.add(storeServiceSpanName(serviceName, span.name));
}
}
}
return transform(Futures.allAsList(futures.build()), TO_VOID);
}
use of zipkin.storage.cassandra3.Schema.TraceIdUDT in project zipkin by openzipkin.
the class CassandraSpanStore method getTraceIdsByServiceNames.
ListenableFuture<Map<TraceIdUDT, Long>> getTraceIdsByServiceNames(QueryRequest request) {
long oldestData = indexTtl == 0 ? 0 : (System.currentTimeMillis() - indexTtl * 1000);
long startTsMillis = Math.max((request.endTs - request.lookback), oldestData);
long endTsMillis = Math.max(request.endTs, oldestData);
try {
Set<String> serviceNames;
if (null != request.serviceName) {
serviceNames = Collections.singleton(request.serviceName);
} else {
serviceNames = new LinkedHashSet<>(getServiceNames().get());
if (serviceNames.isEmpty()) {
return immediateFuture(Collections.<TraceIdUDT, Long>emptyMap());
}
}
int startBucket = CassandraUtil.durationIndexBucket(startTsMillis * 1000);
int endBucket = CassandraUtil.durationIndexBucket(endTsMillis * 1000);
if (startBucket > endBucket) {
throw new IllegalArgumentException("Start bucket (" + startBucket + ") > end bucket (" + endBucket + ")");
}
Set<Integer> buckets = ContiguousSet.create(Range.closed(startBucket, endBucket), integers());
boolean withDuration = null != request.minDuration || null != request.maxDuration;
List<ListenableFuture<Map<TraceIdUDT, Long>>> futures = new ArrayList<>();
if (200 < serviceNames.size() * buckets.size()) {
LOG.warn("read against " + TABLE_TRACE_BY_SERVICE_SPAN + " fanning out to " + serviceNames.size() * buckets.size() + " requests");
//@xxx the fan-out of requests here can be improved
}
for (String serviceName : serviceNames) {
for (Integer bucket : buckets) {
BoundStatement bound = CassandraUtil.bindWithName(withDuration ? selectTraceIdsByServiceSpanNameAndDuration : selectTraceIdsByServiceSpanName, "select-trace-ids-by-service-name").setString("service_name", serviceName).setString("span_name", null != request.spanName ? request.spanName : "").setInt("bucket", bucket).setUUID("start_ts", UUIDs.startOf(startTsMillis)).setUUID("end_ts", UUIDs.endOf(endTsMillis)).setInt("limit_", request.limit);
if (withDuration) {
bound = bound.setLong("start_duration", null != request.minDuration ? request.minDuration : 0).setLong("end_duration", null != request.maxDuration ? request.maxDuration : Long.MAX_VALUE);
}
bound.setFetchSize(Integer.MAX_VALUE);
futures.add(transform(session.executeAsync(bound), traceIdToTimestamp));
}
}
return transform(allAsList(futures), collapseTraceIdMaps);
} catch (RuntimeException | InterruptedException | ExecutionException ex) {
return immediateFailedFuture(ex);
}
}
use of zipkin.storage.cassandra3.Schema.TraceIdUDT in project zipkin by openzipkin.
the class DefaultSessionFactory method initializeUDTs.
private static void initializeUDTs(Session session) {
Schema.ensureExists(DEFAULT_KEYSPACE + "_udts", session);
MappingManager mapping = new MappingManager(session);
// The UDTs are hardcoded against the zipkin keyspace.
// If a different keyspace is being used the codecs must be re-applied to this different keyspace
TypeCodec<TraceIdUDT> traceIdCodec = mapping.udtCodec(TraceIdUDT.class);
TypeCodec<EndpointUDT> endpointCodec = mapping.udtCodec(EndpointUDT.class);
TypeCodec<AnnotationUDT> annoCodec = mapping.udtCodec(AnnotationUDT.class);
TypeCodec<BinaryAnnotationUDT> bAnnoCodec = mapping.udtCodec(BinaryAnnotationUDT.class);
KeyspaceMetadata keyspace = session.getCluster().getMetadata().getKeyspace(session.getLoggedKeyspace());
session.getCluster().getConfiguration().getCodecRegistry().register(new TypeCodecImpl(keyspace.getUserType("trace_id"), TraceIdUDT.class, traceIdCodec)).register(new TypeCodecImpl(keyspace.getUserType("endpoint"), EndpointUDT.class, endpointCodec)).register(new TypeCodecImpl(keyspace.getUserType("annotation"), AnnotationUDT.class, annoCodec)).register(new TypeCodecImpl(keyspace.getUserType("binary_annotation"), BinaryAnnotationUDT.class, bAnnoCodec));
}
use of zipkin.storage.cassandra3.Schema.TraceIdUDT in project zipkin by openzipkin.
the class CassandraSpanConsumer method storeSpan.
/**
* Store the span in the underlying storage for later retrieval.
*/
ListenableFuture<?> storeSpan(Span span, TraceIdUDT traceId, boolean isServerRecvSpan, Long timestamp) {
try {
if ((null == timestamp || 0 == timestamp) && !isServerRecvSpan && metadata.compactionClass.contains("TimeWindowCompactionStrategy")) {
LOG.warn("Span {} in trace {} had no timestamp. " + "If this happens a lot consider switching back to SizeTieredCompactionStrategy for " + "{}.traces", span.id, span.traceId, session.getLoggedKeyspace());
}
List<AnnotationUDT> annotations = new ArrayList<>(span.annotations.size());
for (Annotation annotation : span.annotations) {
annotations.add(new AnnotationUDT(annotation));
}
List<BinaryAnnotationUDT> binaryAnnotations = new ArrayList<>(span.binaryAnnotations.size());
for (BinaryAnnotation annotation : span.binaryAnnotations) {
binaryAnnotations.add(new BinaryAnnotationUDT(annotation));
}
Set<String> annotationKeys = CassandraUtil.annotationKeys(span);
if (!strictTraceId && traceId.getHigh() != 0L) {
storeSpan(span, new TraceIdUDT(0L, traceId.getLow()), isServerRecvSpan, timestamp);
}
BoundStatement bound = bindWithName(insertSpan, "insert-span").set("trace_id", traceId, TraceIdUDT.class).setUUID("ts_uuid", new UUID(UUIDs.startOf(null != timestamp ? (timestamp / 1000) : 0).getMostSignificantBits(), UUIDs.random().getLeastSignificantBits())).setLong("id", span.id).setString("span_name", span.name).setList("annotations", annotations).setList("binary_annotations", binaryAnnotations).setString("all_annotations", Joiner.on(',').join(annotationKeys));
if (null != span.timestamp) {
bound = bound.setLong("ts", span.timestamp);
}
if (null != span.duration) {
bound = bound.setLong("duration", span.duration);
}
if (null != span.parentId) {
bound = bound.setLong("parent_id", span.parentId);
}
return session.executeAsync(bound);
} catch (RuntimeException ex) {
return Futures.immediateFailedFuture(ex);
}
}
Aggregations