Skip to content

Commit

Permalink
Add toString implementation to various propagator components (open-te…
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-berg authored and dmarkwat committed Dec 30, 2022
1 parent 82c2ad3 commit e7f4f7d
Show file tree
Hide file tree
Showing 23 changed files with 130 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,9 @@ private static boolean isValidBaggageKey(String name) {
private static boolean isValidBaggageValue(String value) {
return value != null;
}

@Override
public String toString() {
return "W3CBaggagePropagator";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,9 @@ private static SpanContext extractContextFromTraceParent(String traceparent) {
OtelEncodingUtils.byteFromBase16(firstTraceFlagsChar, secondTraceFlagsChar));
return SpanContext.createFromRemoteParent(traceId, spanId, traceFlags, TraceState.getDefault());
}

@Override
public String toString() {
return "W3CTraceContextPropagator";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -473,4 +473,9 @@ void inject_nullSetter() {
W3CBaggagePropagator.getInstance().inject(context, carrier, null);
assertThat(carrier).isEmpty();
}

@Test
void toString_Valid() {
assertThat(W3CBaggagePropagator.getInstance().toString()).isEqualTo("W3CBaggagePropagator");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,11 @@ void extract_nullGetter() {
.isSameAs(context);
}

@Test
void toString_Valid() {
assertThat(w3cTraceContextPropagator.toString()).isEqualTo("W3CTraceContextPropagator");
}

// Tests transplanted from the w3c test suite

@ParameterizedTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ public TextMapPropagator getTextMapPropagator() {
DefaultContextPropagators(TextMapPropagator textMapPropagator) {
this.textMapPropagator = textMapPropagator;
}

@Override
public String toString() {
return "DefaultContextPropagators{textMapPropagator=" + textMapPropagator + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@
import javax.annotation.Nullable;

final class MultiTextMapPropagator implements TextMapPropagator {
private final TextMapPropagator[] textPropagators;
private final TextMapPropagator[] textMapPropagators;
private final Collection<String> allFields;

MultiTextMapPropagator(TextMapPropagator... textPropagators) {
this(Arrays.asList(textPropagators));
MultiTextMapPropagator(TextMapPropagator... textMapPropagators) {
this(Arrays.asList(textMapPropagators));
}

MultiTextMapPropagator(List<TextMapPropagator> textPropagators) {
this.textPropagators = new TextMapPropagator[textPropagators.size()];
textPropagators.toArray(this.textPropagators);
this.allFields = Collections.unmodifiableList(getAllFields(this.textPropagators));
MultiTextMapPropagator(List<TextMapPropagator> textMapPropagators) {
this.textMapPropagators = new TextMapPropagator[textMapPropagators.size()];
textMapPropagators.toArray(this.textMapPropagators);
this.allFields = Collections.unmodifiableList(getAllFields(this.textMapPropagators));
}

@Override
Expand All @@ -48,7 +48,7 @@ public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> se
if (context == null || setter == null) {
return;
}
for (TextMapPropagator textPropagator : textPropagators) {
for (TextMapPropagator textPropagator : textMapPropagators) {
textPropagator.inject(context, carrier, setter);
}
}
Expand All @@ -61,9 +61,14 @@ public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C
if (getter == null) {
return context;
}
for (TextMapPropagator textPropagator : textPropagators) {
for (TextMapPropagator textPropagator : textMapPropagators) {
context = textPropagator.extract(context, carrier, getter);
}
return context;
}

@Override
public String toString() {
return "MultiTextMapPropagator{textMapPropagators=" + Arrays.toString(textMapPropagators) + '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,9 @@ public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C
}
return context;
}

@Override
public String toString() {
return "NoopTextMapPropagator";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,10 @@ void inject_nullSetter() {
new MultiTextMapPropagator(propagator1, propagator2).inject(context, carrier, null);
assertThat(carrier).isEmpty();
}

@Test
void toString_Valid() {
assertThat(new MultiTextMapPropagator(propagator1, propagator2).toString())
.isEqualTo("MultiTextMapPropagator{textMapPropagators=[propagator1, propagator2]}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ void inject_nullSetter() {
assertThat(carrier).isEmpty();
}

@Test
void toString_Valid() {
assertThat(TextMapPropagator.noop().toString()).isEqualTo("NoopTextMapPropagator");
}

enum MapTextMapGetter implements TextMapGetter<Map<? extends Object, ? extends Object>> {
INSTANCE;

Expand Down
7 changes: 6 additions & 1 deletion docs/apidiffs/current_vs_latest/opentelemetry-api.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
Comparing source compatibility of against
No changes.
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.api.baggage.propagation.W3CBaggagePropagator (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) java.lang.String toString()
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.api.trace.propagation.W3CTraceContextPropagator (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) java.lang.String toString()
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
Comparing source compatibility of against
No changes.
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.extension.trace.propagation.B3Propagator (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) java.lang.String toString()
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.extension.trace.propagation.JaegerPropagator (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) java.lang.String toString()
*** MODIFIED CLASS: PUBLIC FINAL io.opentelemetry.extension.trace.propagation.OtTracePropagator (not serializable)
=== CLASS FILE FORMAT VERSION: 52.0 <- 52.0
+++ NEW METHOD: PUBLIC(+) java.lang.String toString()
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,9 @@ public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C
}
return extracted != null ? context.with(EXTRACTED_KEY_VALUES, extracted) : context;
}

@Override
public String toString() {
return "PassThroughPropagator{fields=" + fields + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,9 @@ void nullFields() {
.isInstanceOf(NullPointerException.class)
.hasMessageContaining("field");
}

@Test
void toString_Valid() {
assertThat(propagator.toString()).isEqualTo("PassThroughPropagator{fields=[animal, food]}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,9 @@ public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C
.findFirst()
.get();
}

@Override
public String toString() {
return "B3Propagator{b3PropagatorInjector=" + b3PropagatorInjector + "}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,9 @@ public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> se
public Collection<String> fields() {
return FIELDS;
}

@Override
public String toString() {
return "B3PropagatorInjectorMultipleHeaders";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ public <C> void inject(Context context, @Nullable C carrier, TextMapSetter<C> se
public Collection<String> fields() {
return FIELDS;
}

@Override
public String toString() {
return "B3PropagatorInjectorSingleHeader";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C
return context;
}

@Override
public String toString() {
return "JaegerPropagator";
}

private static <C> SpanContext getSpanContextFromHeader(
@Nullable C carrier, TextMapGetter<C> getter) {
String value = getter.get(carrier, PROPAGATION_HEADER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,9 @@ private static SpanContext buildSpanContext(
}
return Common.buildSpanContext(traceId, spanId, sampled);
}

@Override
public String toString() {
return "OtTracePropagator";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -724,4 +724,12 @@ void inject_DebugContext_SingleHeader() {
B3Propagator.COMBINED_HEADER,
TRACE_ID + "-" + SPAN_ID + "-" + B3Propagator.SINGLE_HEADER_DEBUG);
}

@Test
void toString_Valid() {
assertThat(b3Propagator.toString())
.isEqualTo("B3Propagator{b3PropagatorInjector=B3PropagatorInjectorMultipleHeaders}");
assertThat(b3PropagatorSingleHeader.toString())
.isEqualTo("B3Propagator{b3PropagatorInjector=B3PropagatorInjectorSingleHeader}");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,11 @@ void extract_nullGetter() {
assertThat(jaegerPropagator.extract(context, Collections.emptyMap(), null)).isSameAs(context);
}

@Test
void toString_Valid() {
assertThat(jaegerPropagator.toString()).isEqualTo("JaegerPropagator");
}

private static String generateTraceIdHeaderValue(
String traceId, String spanId, char parentSpan, String sampled) {
return traceId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,4 +340,9 @@ void extract_nullGetter() {
Context.current());
assertThat(propagator.extract(context, Collections.emptyMap(), null)).isSameAs(context);
}

@Test
void toString_Valid() {
assertThat(propagator.toString()).isEqualTo("OtTracePropagator");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ public ContextPropagators getPropagators() {

@Override
public String toString() {
// TODO(anuraaga): Add propagators
return "OpenTelemetrySdk{"
+ "tracerProvider="
+ tracerProvider.unobfuscate()
+ ", meterProvider="
+ meterProvider.unobfuscate()
+ ", loggerProvider="
+ loggerProvider
+ ", propagators="
+ propagators
+ "}";
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ void stringRepresentation() {
when(metricExporter.toString()).thenReturn("MockMetricExporter{}");
LogRecordExporter logRecordExporter = mock(LogRecordExporter.class);
when(logRecordExporter.toString()).thenReturn("MockLogRecordExporter{}");
TextMapPropagator propagator = mock(TextMapPropagator.class);
when(propagator.toString()).thenReturn("MockTextMapPropagator{}");
Resource resource =
Resource.builder().put(AttributeKey.stringKey("service.name"), "otel-test").build();
OpenTelemetrySdk sdk =
Expand All @@ -352,6 +354,7 @@ void stringRepresentation() {
SimpleLogRecordProcessor.create(
LogRecordExporter.composite(logRecordExporter, logRecordExporter)))
.build())
.setPropagators(ContextPropagators.create(propagator))
.build();

assertThat(sdk.toString())
Expand All @@ -376,7 +379,8 @@ void stringRepresentation() {
+ "resource=Resource{schemaUrl=null, attributes={service.name=\"otel-test\"}}, "
+ "logLimits=LogLimits{maxNumberOfAttributes=128, maxAttributeValueLength=2147483647}, "
+ "logRecordProcessor=SimpleLogRecordProcessor{logRecordExporter=MultiLogRecordExporter{logRecordExporters=[MockLogRecordExporter{}, MockLogRecordExporter{}]}}"
+ "}"
+ "}, "
+ "propagators=DefaultContextPropagators{textMapPropagator=MockTextMapPropagator{}}"
+ "}");
}
}

0 comments on commit e7f4f7d

Please sign in to comment.