use of io.opentelemetry.proto.trace.v1.ResourceSpans in project wavefront-proxy by wavefrontHQ.
the class OtlpTestHelpers method otlpTraceRequest.
public static ExportTraceServiceRequest otlpTraceRequest(io.opentelemetry.proto.trace.v1.Span otlpSpan) {
InstrumentationLibrarySpans ilSpans = InstrumentationLibrarySpans.newBuilder().addSpans(otlpSpan).build();
ResourceSpans rSpans = ResourceSpans.newBuilder().addInstrumentationLibrarySpans(ilSpans).build();
ExportTraceServiceRequest request = ExportTraceServiceRequest.newBuilder().addResourceSpans(rSpans).build();
return request;
}
use of io.opentelemetry.proto.trace.v1.ResourceSpans in project opentelemetry-java-instrumentation by open-telemetry.
the class AgentTestingExporterAccess method getExportedSpans.
@SuppressWarnings("unchecked")
public static List<SpanData> getExportedSpans() {
List<byte[]> exportRequests;
try {
exportRequests = (List<byte[]>) getSpanExportRequests.invokeExact();
} catch (Throwable t) {
throw new AssertionError("Could not invoke getSpanExportRequests", t);
}
List<ResourceSpans> allResourceSpans = exportRequests.stream().map(serialized -> {
try {
return ExportTraceServiceRequest.parseFrom(serialized);
} catch (InvalidProtocolBufferException e) {
throw new AssertionError(e);
}
}).flatMap(request -> request.getResourceSpansList().stream()).collect(toList());
List<SpanData> spans = new ArrayList<>();
for (ResourceSpans resourceSpans : allResourceSpans) {
Resource resource = resourceSpans.getResource();
for (InstrumentationLibrarySpans ilSpans : resourceSpans.getInstrumentationLibrarySpansList()) {
InstrumentationLibrary instrumentationLibrary = ilSpans.getInstrumentationLibrary();
for (Span span : ilSpans.getSpansList()) {
String traceId = bytesToHex(span.getTraceId().toByteArray());
spans.add(TestSpanData.builder().setSpanContext(SpanContext.create(traceId, bytesToHex(span.getSpanId().toByteArray()), TraceFlags.getDefault(), extractTraceState(span.getTraceState()))).setParentSpanContext(SpanContext.create(traceId, bytesToHex(span.getParentSpanId().toByteArray()), TraceFlags.getDefault(), TraceState.getDefault())).setResource(io.opentelemetry.sdk.resources.Resource.create(fromProto(resource.getAttributesList()))).setInstrumentationLibraryInfo(InstrumentationLibraryInfo.create(instrumentationLibrary.getName(), instrumentationLibrary.getVersion())).setName(span.getName()).setStartEpochNanos(span.getStartTimeUnixNano()).setEndEpochNanos(span.getEndTimeUnixNano()).setAttributes(fromProto(span.getAttributesList())).setEvents(span.getEventsList().stream().map(event -> EventData.create(event.getTimeUnixNano(), event.getName(), fromProto(event.getAttributesList()), event.getDroppedAttributesCount() + event.getAttributesCount())).collect(toList())).setStatus(fromProto(span.getStatus())).setKind(fromProto(span.getKind())).setLinks(span.getLinksList().stream().map(link -> LinkData.create(SpanContext.create(bytesToHex(link.getTraceId().toByteArray()), bytesToHex(link.getSpanId().toByteArray()), TraceFlags.getDefault(), extractTraceState(link.getTraceState())), fromProto(link.getAttributesList()), link.getDroppedAttributesCount() + link.getAttributesCount())).collect(toList())).setHasEnded(true).setTotalRecordedEvents(span.getEventsCount() + span.getDroppedEventsCount()).setTotalRecordedLinks(span.getLinksCount() + span.getDroppedLinksCount()).setTotalAttributeCount(span.getAttributesCount() + span.getDroppedAttributesCount()).build());
}
}
}
return spans;
}
use of io.opentelemetry.proto.trace.v1.ResourceSpans in project data-prepper by opensearch-project.
the class EndToEndServiceMapTest method getResourceSpansBatch.
private List<ResourceSpans> getResourceSpansBatch(final String traceId, final List<EndToEndTestSpan> data) {
final ArrayList<ResourceSpans> spansList = new ArrayList<>();
for (int i = 0; i < data.size(); i++) {
final EndToEndTestSpan currData = data.get(i);
final String parentId = currData.parentId;
final String spanId = currData.spanId;
final String serviceName = currData.serviceName;
final String spanName = currData.name;
final Span.SpanKind spanKind = currData.spanKind;
final ResourceSpans rs = getResourceSpans(serviceName, spanName, spanId.getBytes(), parentId != null ? parentId.getBytes() : null, traceId.getBytes(), spanKind);
spansList.add(rs);
}
return spansList;
}
use of io.opentelemetry.proto.trace.v1.ResourceSpans in project data-prepper by opensearch-project.
the class PeerForwarder method doExecute.
@Override
public List<Record<ExportTraceServiceRequest>> doExecute(final Collection<Record<ExportTraceServiceRequest>> records) {
final Map<String, List<ResourceSpans>> groupedRS = new HashMap<>();
// Group ResourceSpans by consistent hashing of traceId
for (final Record<ExportTraceServiceRequest> record : records) {
for (final ResourceSpans rs : record.getData().getResourceSpansList()) {
final List<Map.Entry<String, ResourceSpans>> rsBatch = PeerForwarderUtils.splitByTrace(rs);
for (final Map.Entry<String, ResourceSpans> entry : rsBatch) {
final String traceId = entry.getKey();
final ResourceSpans newRS = entry.getValue();
final String dataPrepperIp = hashRing.getServerIp(traceId).orElse(StaticPeerListProvider.LOCAL_ENDPOINT);
groupedRS.computeIfAbsent(dataPrepperIp, x -> new ArrayList<>()).add(newRS);
}
}
}
final List<Record<ExportTraceServiceRequest>> recordsToProcessLocally = new ArrayList<>();
final List<CompletableFuture<Record>> forwardedRequestFutures = new ArrayList<>();
for (final Map.Entry<String, List<ResourceSpans>> entry : groupedRS.entrySet()) {
final TraceServiceGrpc.TraceServiceBlockingStub client = getClient(entry.getKey());
// Create ExportTraceRequest for storing single batch of spans
ExportTraceServiceRequest.Builder currRequestBuilder = ExportTraceServiceRequest.newBuilder();
int currSpansCount = 0;
for (final ResourceSpans rs : entry.getValue()) {
final int rsSize = PeerForwarderUtils.getResourceSpansSize(rs);
if (currSpansCount >= maxNumSpansPerRequest) {
final ExportTraceServiceRequest currRequest = currRequestBuilder.build();
if (isLocalClient(client)) {
recordsToProcessLocally.add(new Record<>(currRequest));
} else {
forwardedRequestFutures.add(processRequest(client, currRequest));
}
currRequestBuilder = ExportTraceServiceRequest.newBuilder();
currSpansCount = 0;
}
currRequestBuilder.addResourceSpans(rs);
currSpansCount += rsSize;
}
// Dealing with the last batch request
if (currSpansCount > 0) {
final ExportTraceServiceRequest currRequest = currRequestBuilder.build();
if (client == null) {
recordsToProcessLocally.add(new Record<>(currRequest));
} else {
forwardedRequestFutures.add(processRequest(client, currRequest));
}
}
}
for (final CompletableFuture<Record> future : forwardedRequestFutures) {
try {
final Record record = future.get();
if (record != null) {
recordsToProcessLocally.add(record);
}
} catch (InterruptedException | ExecutionException e) {
LOG.error("Problem with asynchronous peer forwarding", e);
}
}
return recordsToProcessLocally;
}
use of io.opentelemetry.proto.trace.v1.ResourceSpans in project data-prepper by opensearch-project.
the class PeerForwarderUtils method splitByTrace.
public static List<Map.Entry<String, ResourceSpans>> splitByTrace(final ResourceSpans rs) {
final List<Map.Entry<String, ResourceSpans>> result = new ArrayList<>();
for (final InstrumentationLibrarySpans ils : rs.getInstrumentationLibrarySpansList()) {
final Map<String, ResourceSpans.Builder> batches = new HashMap<>();
for (final Span span : ils.getSpansList()) {
final String sTraceId = Hex.toHexString(span.getTraceId().toByteArray());
if (!batches.containsKey(sTraceId)) {
final ResourceSpans.Builder newRSBuilder = ResourceSpans.newBuilder().setResource(rs.getResource());
newRSBuilder.addInstrumentationLibrarySpansBuilder().setInstrumentationLibrary(ils.getInstrumentationLibrary());
batches.put(sTraceId, newRSBuilder);
}
// there is only one instrumentation library per batch
batches.get(sTraceId).getInstrumentationLibrarySpansBuilder(0).addSpans(span);
}
batches.forEach((traceId, rsBuilder) -> result.add(new AbstractMap.SimpleEntry<>(traceId, rsBuilder.build())));
}
return result;
}
Aggregations