BaggageSetter { public JaegerSpanContext setBaggage(JaegerSpan jaegerSpan, String key, String value) { Restriction restriction = restrictionManager.getRestriction(jaegerSpan.getServiceName(), key); boolean truncated = false; String prevItem = null; if (!restriction.isKeyAllowed()) { metrics.baggageUpdateFailure.inc(1); logFields(jaegerSpan, key, value, prevItem, truncated, restriction.isKeyAllowed()); return jaegerSpan.context(); } if (value != null && value.length() > restriction.getMaxValueLength()) { truncated = true; value = value.substring(0, restriction.getMaxValueLength()); metrics.baggageTruncate.inc(1); } prevItem = jaegerSpan.getBaggageItem(key); logFields(jaegerSpan, key, value, prevItem, truncated, restriction.isKeyAllowed()); metrics.baggageUpdateSuccess.inc(1); return jaegerSpan.context().withBaggageItem(key, value); } BaggageSetter(BaggageRestrictionManager restrictionManager, Metrics metrics); JaegerSpanContext setBaggage(JaegerSpan jaegerSpan, String key, String value);  }
@Test public void testInvalidBaggage() { when(mgr.getRestriction(SERVICE, KEY)).thenReturn(Restriction.of(false, 0)); final String value = "value"; JaegerSpanContext ctx = setter.setBaggage(jaegerSpan, KEY, value); assertBaggageLogs(jaegerSpan, KEY, value, false, false, true); assertNull(ctx.getBaggageItem(KEY)); assertEquals(1, metricsFactory.getCounter("jaeger_tracer_baggage_updates", "result=err")); }
@Test public void testTruncatedBaggage() { when(mgr.getRestriction(SERVICE, KEY)).thenReturn(Restriction.of(true, 5)); final String value = "0123456789"; final String expected = "01234"; JaegerSpanContext ctx = setter.setBaggage(jaegerSpan, KEY, value); assertBaggageLogs(jaegerSpan, KEY, expected, true, false, false); assertEquals(expected, ctx.getBaggageItem(KEY)); assertEquals(1, metricsFactory.getCounter("jaeger_tracer_baggage_truncations", "")); assertEquals(1, metricsFactory.getCounter("jaeger_tracer_baggage_updates", "result=ok")); }
@Test public void testOverrideBaggage() { when(mgr.getRestriction(SERVICE, KEY)).thenReturn(Restriction.of(true, 5)); final String value = "value"; JaegerSpanContext ctx = setter.setBaggage(jaegerSpan, KEY, value); JaegerSpan child = tracer.buildSpan("some-operation").asChildOf(ctx).start(); ctx = setter.setBaggage(child, KEY, value); assertBaggageLogs(child, KEY, value, false, true, false); assertEquals(value, ctx.getBaggageItem(KEY)); assertEquals(2, metricsFactory.getCounter("jaeger_tracer_baggage_updates", "result=ok")); }
@Test public void testUnsampledSpan() { tracer = new JaegerTracer.Builder(SERVICE) .withReporter(reporter) .withSampler(new ConstSampler(false)) .withMetrics(metrics) .build(); jaegerSpan = tracer.buildSpan("some-operation").start(); when(mgr.getRestriction(SERVICE, KEY)).thenReturn(Restriction.of(true, 5)); final String value = "value"; JaegerSpanContext ctx = setter.setBaggage(jaegerSpan, KEY, value); assertEquals(value, ctx.getBaggageItem(KEY)); assertNull(jaegerSpan.getLogs()); }
@Test public void testBaggageNullValueTolerated() { when(mgr.getRestriction(SERVICE, KEY)).thenReturn(Restriction.of(true, 5)); final String value = null; JaegerSpanContext ctx = setter.setBaggage(jaegerSpan, KEY, value); assertBaggageLogs(jaegerSpan, KEY, null, false, false, false); assertNull(ctx.getBaggageItem(KEY)); }