use of io.opentelemetry.proto.trace.v1.ResourceSpans in project besu by hyperledger.
the class OpenTelemetryAcceptanceTest method traceReporting.
@Test
public void traceReporting() {
WaitUtils.waitFor(30, () -> {
// call the json RPC endpoint to generate a trace.
net.netVersion().verify(metricsNode);
List<ResourceSpans> spans = fakeTracesCollector.getReceivedSpans();
assertThat(spans.isEmpty()).isFalse();
Span internalSpan = spans.get(0).getInstrumentationLibrarySpans(0).getSpans(0);
assertThat(internalSpan.getKind()).isEqualTo(Span.SpanKind.SPAN_KIND_INTERNAL);
ByteString parent = internalSpan.getParentSpanId();
assertThat(parent.isEmpty()).isFalse();
Span serverSpan = spans.get(0).getInstrumentationLibrarySpans(0).getSpans(1);
assertThat(serverSpan.getKind()).isEqualTo(Span.SpanKind.SPAN_KIND_SERVER);
ByteString rootSpanId = serverSpan.getParentSpanId();
assertThat(rootSpanId.isEmpty()).isTrue();
});
}
use of io.opentelemetry.proto.trace.v1.ResourceSpans in project inspectit-ocelot by inspectIT.
the class OpenTelemetryProtoConverter method convert.
/**
* Converts open-telemetry proto data to the open-telemetry SDK span data.
*
* @param data data to convert
*
* @return Non-null collection of {@link SpanData}
*/
public Collection<SpanData> convert(ExportTraceServiceRequest data) {
List<SpanData> result = new ArrayList<>();
for (ResourceSpans resourceSpans : data.getResourceSpansList()) {
// create general span resources, e.g. sdk-version, service-name, ...
Attributes attributes = OcelotSpanUtils.toAttributes(resourceSpans.getResource().getAttributesList());
final Resource resource = Resource.create(attributes);
Map<String, String> customSpanAttributes = getCustomSpanAttributes();
resourceSpans.getInstrumentationLibrarySpansList().stream().flatMap(librarySpans -> toSpanData(librarySpans, resource, customSpanAttributes)).forEach(result::add);
}
return result;
}
use of io.opentelemetry.proto.trace.v1.ResourceSpans in project opentelemetry-java by open-telemetry.
the class TraceRequestMarshalerTest method toProtoResourceSpans.
@Test
void toProtoResourceSpans() {
ResourceSpansMarshaler[] resourceSpansMarshalers = ResourceSpansMarshaler.create(Collections.singleton(TestSpanData.builder().setHasEnded(true).setSpanContext(SPAN_CONTEXT).setParentSpanContext(SpanContext.getInvalid()).setName("GET /api/endpoint").setKind(SpanKind.SERVER).setStartEpochNanos(12345).setEndEpochNanos(12349).setStatus(StatusData.unset()).setInstrumentationLibraryInfo(InstrumentationLibraryInfo.create("testLib", "1.0", "http://url")).setResource(Resource.builder().put("one", 1).setSchemaUrl("http://url").build()).build()));
assertThat(resourceSpansMarshalers).hasSize(1);
ResourceSpans onlyResourceSpans = parse(ResourceSpans.getDefaultInstance(), resourceSpansMarshalers[0]);
assertThat(onlyResourceSpans.getSchemaUrl()).isEqualTo("http://url");
assertThat(onlyResourceSpans.getInstrumentationLibrarySpansCount()).isEqualTo(1);
InstrumentationLibrarySpans instrumentationLibrarySpans = onlyResourceSpans.getInstrumentationLibrarySpans(0);
assertThat(instrumentationLibrarySpans.getSchemaUrl()).isEqualTo("http://url");
assertThat(instrumentationLibrarySpans.getInstrumentationLibrary()).isEqualTo(InstrumentationLibrary.newBuilder().setName("testLib").setVersion("1.0").build());
}
use of io.opentelemetry.proto.trace.v1.ResourceSpans in project opentelemetry-java by open-telemetry.
the class TraceRequestMarshalerTest method parse.
@SuppressWarnings("unchecked")
private static <T extends Message> T parse(T prototype, Marshaler marshaler) {
byte[] serialized = toByteArray(marshaler);
T result;
try {
result = (T) prototype.newBuilderForType().mergeFrom(serialized).build();
} catch (InvalidProtocolBufferException e) {
throw new UncheckedIOException(e);
}
// Our marshaler should produce the exact same length of serialized output (for example, field
// default values are not outputted), so we check that here. The output itself may have slightly
// different ordering, mostly due to the way we don't output oneof values in field order all the
// tieme. If the lengths are equal and the resulting protos are equal, the marshaling is
// guaranteed to be valid.
assertThat(result.getSerializedSize()).isEqualTo(serialized.length);
// We don't compare JSON strings due to some differences (particularly serializing enums as
// numbers instead of names). This may improve in the future but what matters is what we produce
// can be parsed.
String json = toJson(marshaler);
Message.Builder builder = prototype.newBuilderForType();
try {
JsonFormat.parser().merge(json, builder);
} catch (InvalidProtocolBufferException e) {
throw new UncheckedIOException(e);
}
// libraries currently support customizing on the parse side.
if (result instanceof Span) {
fixSpanJsonIds((Span.Builder) builder);
}
// libraries currently support customizing on the parse side.
if (result instanceof Span.Link) {
fixLinkJsonIds((Span.Link.Builder) builder);
}
if (result instanceof ResourceSpans) {
ResourceSpans.Builder fixed = (ResourceSpans.Builder) builder;
for (InstrumentationLibrarySpans.Builder ils : fixed.getInstrumentationLibrarySpansBuilderList()) {
for (Span.Builder span : ils.getSpansBuilderList()) {
fixSpanJsonIds(span);
}
}
}
assertThat(builder.build()).isEqualTo(result);
return result;
}
use of io.opentelemetry.proto.trace.v1.ResourceSpans in project opentelemetry-java by open-telemetry.
the class OtlpExporterIntegrationTest method testTraceExport.
private static void testTraceExport(SpanExporter spanExporter) {
SdkTracerProvider tracerProvider = SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(spanExporter)).setResource(RESOURCE).build();
SpanContext linkContext = SpanContext.create(IdGenerator.random().generateTraceId(), IdGenerator.random().generateSpanId(), TraceFlags.getDefault(), TraceState.getDefault());
Span span = tracerProvider.get(OtlpExporterIntegrationTest.class.getName()).spanBuilder("my span name").addLink(linkContext).startSpan();
span.setAttribute("key", "value");
span.addEvent("event");
span.end();
await().atMost(Duration.ofSeconds(30)).untilAsserted(() -> assertThat(grpcServer.traceRequests).hasSize(1));
ExportTraceServiceRequest request = grpcServer.traceRequests.get(0);
assertThat(request.getResourceSpansCount()).isEqualTo(1);
ResourceSpans resourceSpans = request.getResourceSpans(0);
assertThat(resourceSpans.getResource().getAttributesList()).contains(KeyValue.newBuilder().setKey(ResourceAttributes.SERVICE_NAME.getKey()).setValue(AnyValue.newBuilder().setStringValue("integration test").build()).build());
assertThat(resourceSpans.getInstrumentationLibrarySpansCount()).isEqualTo(1);
InstrumentationLibrarySpans ilSpans = resourceSpans.getInstrumentationLibrarySpans(0);
assertThat(ilSpans.getInstrumentationLibrary().getName()).isEqualTo(OtlpExporterIntegrationTest.class.getName());
assertThat(ilSpans.getSpansCount()).isEqualTo(1);
io.opentelemetry.proto.trace.v1.Span protoSpan = ilSpans.getSpans(0);
assertThat(protoSpan.getTraceId().toByteArray()).isEqualTo(span.getSpanContext().getTraceIdBytes());
assertThat(protoSpan.getSpanId().toByteArray()).isEqualTo(span.getSpanContext().getSpanIdBytes());
assertThat(protoSpan.getName()).isEqualTo("my span name");
assertThat(protoSpan.getAttributesList()).isEqualTo(Collections.singletonList(KeyValue.newBuilder().setKey("key").setValue(AnyValue.newBuilder().setStringValue("value").build()).build()));
assertThat(protoSpan.getEventsCount()).isEqualTo(1);
assertThat(protoSpan.getEvents(0).getName()).isEqualTo("event");
assertThat(protoSpan.getLinksCount()).isEqualTo(1);
Link link = protoSpan.getLinks(0);
assertThat(link.getTraceId().toByteArray()).isEqualTo(linkContext.getTraceIdBytes());
assertThat(link.getSpanId().toByteArray()).isEqualTo(linkContext.getSpanIdBytes());
}
Aggregations