def test_parent_based(self): sampler = sampling.ParentBased(sampling.ALWAYS_ON) # Check that the sampling decision matches the parent context if given self.assertFalse( sampler.should_sample( trace.SpanContext( 0xDEADBEF0, 0xDEADBEF1, is_remote=False, trace_flags=TO_DEFAULT, ), 0x7FFFFFFFFFFFFFFF, 0xDEADBEEF, "span name", ).decision.is_sampled()) sampler2 = sampling.ParentBased(sampling.ALWAYS_OFF) self.assertTrue( sampler2.should_sample( trace.SpanContext( 0xDEADBEF0, 0xDEADBEF1, is_remote=False, trace_flags=TO_SAMPLED, ), 0x8000000000000000, 0xDEADBEEF, "span name", ).decision.is_sampled())
def exec_parent_based(self, parent_sampling_context): trace_state = trace.TraceState({"key": "value"}) sampler = sampling.ParentBased(sampling.ALWAYS_ON) with parent_sampling_context( self._create_parent_span(trace_flags=TO_DEFAULT)) as context: # Check that the sampling decision matches the parent context if given not_sampled_result = sampler.should_sample( context, 0x7FFFFFFFFFFFFFFF, "unsampled parent, sampling on", attributes={"sampled": "false"}, trace_state=trace_state, ) self.assertFalse(not_sampled_result.decision.is_sampled()) self.assertEqual(not_sampled_result.attributes, {}) self.assertEqual(not_sampled_result.trace_state, trace_state) with parent_sampling_context( self._create_parent_span(trace_flags=TO_SAMPLED)) as context: sampler2 = sampling.ParentBased(sampling.ALWAYS_OFF) sampled_result = sampler2.should_sample( context, 0x8000000000000000, "sampled parent, sampling off", attributes={"sampled": "true"}, trace_state=trace_state, ) self.assertTrue(sampled_result.decision.is_sampled()) self.assertEqual(sampled_result.attributes, {"sampled": "true"}) self.assertEqual(sampled_result.trace_state, trace_state) # for root span follow decision of delegate sampler with parent_sampling_context(trace.INVALID_SPAN) as context: sampler3 = sampling.ParentBased(sampling.ALWAYS_OFF) not_sampled_result = sampler3.should_sample( context, 0x8000000000000000, "parent, sampling off", attributes={"sampled": "false"}, trace_state=trace_state, ) self.assertFalse(not_sampled_result.decision.is_sampled()) self.assertEqual(not_sampled_result.attributes, {}) self.assertEqual(not_sampled_result.trace_state, trace_state) with parent_sampling_context(trace.INVALID_SPAN) as context: sampler4 = sampling.ParentBased(sampling.ALWAYS_ON) sampled_result = sampler4.should_sample( context, 0x8000000000000000, "no parent, sampling on", attributes={"sampled": "true"}, trace_state=trace_state, ) self.assertTrue(sampled_result.decision.is_sampled()) self.assertEqual(sampled_result.attributes, {"sampled": "true"}) self.assertEqual(sampled_result.trace_state, trace_state)
def test_parent_based(self): sampler = sampling.ParentBased(sampling.ALWAYS_ON) context = trace.set_span_in_context( trace.DefaultSpan( trace.SpanContext( 0xDEADBEF0, 0xDEADBEF1, is_remote=False, trace_flags=TO_DEFAULT, ))) # Check that the sampling decision matches the parent context if given self.assertFalse( sampler.should_sample( context, 0x7FFFFFFFFFFFFFFF, 0xDEADBEEF, "span name", ).decision.is_sampled()) context = trace.set_span_in_context( trace.DefaultSpan( trace.SpanContext( 0xDEADBEF0, 0xDEADBEF1, is_remote=False, trace_flags=TO_SAMPLED, ))) sampler2 = sampling.ParentBased(sampling.ALWAYS_OFF) self.assertTrue( sampler2.should_sample( context, 0x8000000000000000, 0xDEADBEEF, "span name", ).decision.is_sampled()) # root span always sampled for parentbased context = trace.set_span_in_context(trace.INVALID_SPAN) sampler3 = sampling.ParentBased(sampling.ALWAYS_OFF) self.assertTrue( sampler3.should_sample( context, 0x8000000000000000, 0xDEADBEEF, "span name", ).decision.is_sampled())
def exec_parent_based(self, parent_sampling_context): trace_state = trace.TraceState([("key", "value")]) sampler = sampling.ParentBased(sampling.ALWAYS_ON) # Check that the sampling decision matches the parent context if given with parent_sampling_context( self._create_parent_span( trace_flags=TO_DEFAULT, trace_state=trace_state, )) as context: # local, not sampled not_sampled_result = sampler.should_sample( context, 0x7FFFFFFFFFFFFFFF, "unsampled parent, sampling on", trace.SpanKind.INTERNAL, attributes={"sampled": "false"}, ) self.assertFalse(not_sampled_result.decision.is_sampled()) self.assertEqual(not_sampled_result.attributes, {}) self.assertEqual(not_sampled_result.trace_state, trace_state) with parent_sampling_context( self._create_parent_span( trace_flags=TO_DEFAULT, trace_state=trace_state, )) as context: sampler = sampling.ParentBased( root=sampling.ALWAYS_OFF, local_parent_not_sampled=sampling.ALWAYS_ON, ) # local, not sampled -> opposite sampler sampled_result = sampler.should_sample( context, 0x7FFFFFFFFFFFFFFF, "unsampled parent, sampling on", trace.SpanKind.INTERNAL, attributes={"sampled": "false"}, ) self.assertTrue(sampled_result.decision.is_sampled()) self.assertEqual(sampled_result.attributes, {"sampled": "false"}) self.assertEqual(sampled_result.trace_state, trace_state) with parent_sampling_context( self._create_parent_span( trace_flags=TO_SAMPLED, trace_state=trace_state, )) as context: sampler = sampling.ParentBased(sampling.ALWAYS_OFF) # local, sampled sampled_result = sampler.should_sample( context, 0x8000000000000000, "sampled parent, sampling off", trace.SpanKind.INTERNAL, attributes={"sampled": "true"}, trace_state=trace_state, ) self.assertTrue(sampled_result.decision.is_sampled()) self.assertEqual(sampled_result.attributes, {"sampled": "true"}) self.assertEqual(sampled_result.trace_state, trace_state) with parent_sampling_context( self._create_parent_span( trace_flags=TO_SAMPLED, trace_state=trace_state, )) as context: sampler = sampling.ParentBased( root=sampling.ALWAYS_ON, local_parent_sampled=sampling.ALWAYS_OFF, ) # local, sampled -> opposite sampler not_sampled_result = sampler.should_sample( context, 0x7FFFFFFFFFFFFFFF, "unsampled parent, sampling on", trace.SpanKind.INTERNAL, attributes={"sampled": "false"}, trace_state=trace_state, ) self.assertFalse(not_sampled_result.decision.is_sampled()) self.assertEqual(not_sampled_result.attributes, {}) self.assertEqual(not_sampled_result.trace_state, trace_state) with parent_sampling_context( self._create_parent_span( trace_flags=TO_DEFAULT, is_remote=True, trace_state=trace_state, )) as context: sampler = sampling.ParentBased(sampling.ALWAYS_ON) # remote, not sampled not_sampled_result = sampler.should_sample( context, 0x7FFFFFFFFFFFFFFF, "unsampled parent, sampling on", trace.SpanKind.INTERNAL, attributes={"sampled": "false"}, trace_state=trace_state, ) self.assertFalse(not_sampled_result.decision.is_sampled()) self.assertEqual(not_sampled_result.attributes, {}) self.assertEqual(not_sampled_result.trace_state, trace_state) with parent_sampling_context( self._create_parent_span( trace_flags=TO_DEFAULT, is_remote=True, trace_state=trace_state, )) as context: sampler = sampling.ParentBased( root=sampling.ALWAYS_OFF, remote_parent_not_sampled=sampling.ALWAYS_ON, ) # remote, not sampled -> opposite sampler sampled_result = sampler.should_sample( context, 0x7FFFFFFFFFFFFFFF, "unsampled parent, sampling on", trace.SpanKind.INTERNAL, attributes={"sampled": "false"}, ) self.assertTrue(sampled_result.decision.is_sampled()) self.assertEqual(sampled_result.attributes, {"sampled": "false"}) self.assertEqual(sampled_result.trace_state, trace_state) with parent_sampling_context( self._create_parent_span( trace_flags=TO_SAMPLED, is_remote=True, trace_state=trace_state, )) as context: sampler = sampling.ParentBased(sampling.ALWAYS_OFF) # remote, sampled sampled_result = sampler.should_sample( context, 0x8000000000000000, "sampled parent, sampling off", trace.SpanKind.INTERNAL, attributes={"sampled": "true"}, ) self.assertTrue(sampled_result.decision.is_sampled()) self.assertEqual(sampled_result.attributes, {"sampled": "true"}) self.assertEqual(sampled_result.trace_state, trace_state) with parent_sampling_context( self._create_parent_span( trace_flags=TO_SAMPLED, is_remote=True, trace_state=trace_state, )) as context: sampler = sampling.ParentBased( root=sampling.ALWAYS_ON, remote_parent_sampled=sampling.ALWAYS_OFF, ) # remote, sampled -> opposite sampler not_sampled_result = sampler.should_sample( context, 0x7FFFFFFFFFFFFFFF, "unsampled parent, sampling on", trace.SpanKind.INTERNAL, attributes={"sampled": "false"}, ) self.assertFalse(not_sampled_result.decision.is_sampled()) self.assertEqual(not_sampled_result.attributes, {}) self.assertEqual(not_sampled_result.trace_state, trace_state) # for root span follow decision of root sampler with parent_sampling_context(trace.INVALID_SPAN) as context: sampler = sampling.ParentBased(sampling.ALWAYS_OFF) not_sampled_result = sampler.should_sample( context, 0x8000000000000000, "parent, sampling off", trace.SpanKind.INTERNAL, attributes={"sampled": "false"}, ) self.assertFalse(not_sampled_result.decision.is_sampled()) self.assertEqual(not_sampled_result.attributes, {}) self.assertIsNone(not_sampled_result.trace_state) with parent_sampling_context(trace.INVALID_SPAN) as context: sampler = sampling.ParentBased(sampling.ALWAYS_ON) sampled_result = sampler.should_sample( context, 0x8000000000000000, "no parent, sampling on", trace.SpanKind.INTERNAL, attributes={"sampled": "true"}, trace_state=trace_state, ) self.assertTrue(sampled_result.decision.is_sampled()) self.assertEqual(sampled_result.attributes, {"sampled": "true"}) self.assertIsNone(sampled_result.trace_state)