|
25 | 25 | import io.opentelemetry.context.Context; |
26 | 26 | import io.opentelemetry.context.ContextKey; |
27 | 27 | import io.opentelemetry.context.propagation.TextMapGetter; |
| 28 | +import io.opentelemetry.instrumentation.api.internal.SchemaUrlProvider; |
28 | 29 | import io.opentelemetry.instrumentation.api.internal.SpanKey; |
29 | 30 | import io.opentelemetry.instrumentation.api.internal.SpanKeyProvider; |
30 | 31 | import io.opentelemetry.sdk.common.InstrumentationScopeInfo; |
@@ -113,6 +114,30 @@ public void onEnd( |
113 | 114 | } |
114 | 115 | } |
115 | 116 |
|
| 117 | + static class AttributesExtractorWithSchemaUrl |
| 118 | + implements AttributesExtractor<Map<String, String>, Map<String, String>>, SchemaUrlProvider { |
| 119 | + |
| 120 | + @Override |
| 121 | + public void onStart( |
| 122 | + AttributesBuilder attributes, Context parentContext, Map<String, String> request) { |
| 123 | + attributes.put("key", "value"); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void onEnd( |
| 128 | + AttributesBuilder attributes, |
| 129 | + Context context, |
| 130 | + Map<String, String> request, |
| 131 | + @Nullable Map<String, String> response, |
| 132 | + @Nullable Throwable error) {} |
| 133 | + |
| 134 | + @Nullable |
| 135 | + @Override |
| 136 | + public String internalGetSchemaUrl() { |
| 137 | + return "schemaUrl from extractor"; |
| 138 | + } |
| 139 | + } |
| 140 | + |
116 | 141 | static class LinksExtractor implements SpanLinksExtractor<Map<String, String>> { |
117 | 142 |
|
118 | 143 | @Override |
@@ -583,11 +608,60 @@ void instrumentationVersion_custom() { |
583 | 608 | } |
584 | 609 |
|
585 | 610 | @Test |
586 | | - void schemaUrl() { |
| 611 | + void schemaUrl_setExplicitly() { |
| 612 | + Instrumenter<Map<String, String>, Map<String, String>> instrumenter = |
| 613 | + Instrumenter.<Map<String, String>, Map<String, String>>builder( |
| 614 | + otelTesting.getOpenTelemetry(), "test", name -> "span") |
| 615 | + .setSchemaUrl("https://opentelemetry.io/schemas/1.0.0") |
| 616 | + .buildInstrumenter(); |
| 617 | + |
| 618 | + Context context = instrumenter.start(Context.root(), emptyMap()); |
| 619 | + assertThat(Span.fromContext(context)).isNotNull(); |
| 620 | + |
| 621 | + instrumenter.end(context, emptyMap(), emptyMap(), null); |
| 622 | + |
| 623 | + InstrumentationScopeInfo expectedLibraryInfo = |
| 624 | + InstrumentationScopeInfo.builder("test") |
| 625 | + .setSchemaUrl("https://opentelemetry.io/schemas/1.0.0") |
| 626 | + .build(); |
| 627 | + otelTesting |
| 628 | + .assertTraces() |
| 629 | + .hasTracesSatisfyingExactly( |
| 630 | + trace -> |
| 631 | + trace.hasSpansSatisfyingExactly( |
| 632 | + span -> span.hasName("span").hasInstrumentationScopeInfo(expectedLibraryInfo))); |
| 633 | + } |
| 634 | + |
| 635 | + @Test |
| 636 | + void schemaUrl_computedFromExtractors() { |
| 637 | + Instrumenter<Map<String, String>, Map<String, String>> instrumenter = |
| 638 | + Instrumenter.<Map<String, String>, Map<String, String>>builder( |
| 639 | + otelTesting.getOpenTelemetry(), "test", name -> "span") |
| 640 | + .addAttributesExtractor(new AttributesExtractorWithSchemaUrl()) |
| 641 | + .buildInstrumenter(); |
| 642 | + |
| 643 | + Context context = instrumenter.start(Context.root(), emptyMap()); |
| 644 | + assertThat(Span.fromContext(context)).isNotNull(); |
| 645 | + |
| 646 | + instrumenter.end(context, emptyMap(), emptyMap(), null); |
| 647 | + |
| 648 | + InstrumentationScopeInfo expectedLibraryInfo = |
| 649 | + InstrumentationScopeInfo.builder("test").setSchemaUrl("schemaUrl from extractor").build(); |
| 650 | + otelTesting |
| 651 | + .assertTraces() |
| 652 | + .hasTracesSatisfyingExactly( |
| 653 | + trace -> |
| 654 | + trace.hasSpansSatisfyingExactly( |
| 655 | + span -> span.hasName("span").hasInstrumentationScopeInfo(expectedLibraryInfo))); |
| 656 | + } |
| 657 | + |
| 658 | + @Test |
| 659 | + void schemaUrl_schemaSetExplicitlyOverridesSchemaComputedFromExtractors() { |
587 | 660 | Instrumenter<Map<String, String>, Map<String, String>> instrumenter = |
588 | 661 | Instrumenter.<Map<String, String>, Map<String, String>>builder( |
589 | 662 | otelTesting.getOpenTelemetry(), "test", name -> "span") |
590 | 663 | .setSchemaUrl("https://opentelemetry.io/schemas/1.0.0") |
| 664 | + .addAttributesExtractor(new AttributesExtractorWithSchemaUrl()) |
591 | 665 | .buildInstrumenter(); |
592 | 666 |
|
593 | 667 | Context context = instrumenter.start(Context.root(), emptyMap()); |
|
0 commit comments