def test_sampling(self): """Test Sampling.""" tracer = WavefrontTracer(ConsoleReporter(), self.application_tags, samplers=[ConstantSampler(True)]) self.assertTrue(tracer.sample('test_op', 1, 0)) tracer = WavefrontTracer(ConsoleReporter(), self.application_tags, samplers=[ConstantSampler(False)]) self.assertFalse(tracer.sample('test_op', 1, 0))
def test_root_sampling(self): """Test root span with sampling.""" tracer = WavefrontTracer(ConsoleReporter(), self.application_tags, samplers=[ConstantSampler(False)]) span = tracer.start_span('test_op') self.assertIsNotNone(span) self.assertIsNotNone(span.context) self.assertEqual(0, len(span.get_parents())) self.assertEqual(0, len(span.get_follows())) self.assertIsNotNone(span.context.get_sampling_decision()) self.assertFalse(span.context.get_sampling_decision()) tracer = WavefrontTracer(ConsoleReporter(), self.application_tags, samplers=[ConstantSampler(True)]) span = tracer.start_span('test_op') self.assertIsNotNone(span) self.assertIsNotNone(span.context) self.assertEqual(0, len(span.get_parents())) self.assertEqual(0, len(span.get_follows())) self.assertIsNotNone(span.context.get_sampling_decision()) self.assertTrue(span.context.get_sampling_decision())
def test_negative_child_sampling(self): """Test child span with positive sampling.""" tracer = WavefrontTracer(ConsoleReporter(), self.application_tags, samplers=[ConstantSampler(True)]) parent_ctx = WavefrontSpanContext(trace_id=uuid.uuid1(), span_id=uuid.uuid1(), decision=False) span = tracer.start_span('test_op', child_of=parent_ctx) self.assertTrue(tracer.sample(span.operation_name, span.trace_id, 0)) self.assertIsNotNone(span) self.assertEqual(parent_ctx.trace_id, span.trace_id) self.assertTrue(span.context.is_sampled()) self.assertIsNotNone(span.context.get_sampling_decision()) self.assertFalse(span.context.get_sampling_decision())
def test_inject_extract(self): """Test Inject / Extract.""" tracer = WavefrontTracer(ConsoleReporter(), self.application_tags, samplers=[ConstantSampler(True)]) span = tracer.start_span('test_op') self.assertIsNotNone(span) span.set_baggage_item('customer', 'test_customer') span.set_baggage_item('request_type', 'mobile') carrier = {} tracer.inject(span.context, opentracing.propagation.Format.TEXT_MAP, carrier) span.finish() ctx = tracer.extract(opentracing.propagation.Format.TEXT_MAP, carrier) self.assertTrue(ctx.is_sampled()) self.assertTrue(ctx.get_sampling_decision()) self.assertEqual('test_customer', ctx.get_baggage_item('customer')) self.assertEqual('mobile', ctx.get_baggage_item('request_type'))
def test_forced_sampling(self): """Test span with forced sampling.""" tracer = WavefrontTracer(ConsoleReporter(), self.application_tags, samplers=[ConstantSampler(False)]) span = tracer.start_span('test_op') self.assertIsNotNone(span) self.assertIsNotNone(span.context) self.assertIsNotNone(span.context.get_sampling_decision()) self.assertFalse(span.context.get_sampling_decision()) span.set_tag(opentracing.ext.tags.SAMPLING_PRIORITY, 1) self.assertIsNotNone(span.context.get_sampling_decision()) self.assertTrue(span.context.get_sampling_decision()) span = tracer.start_span('test_op') self.assertIsNotNone(span) self.assertIsNotNone(span.context) self.assertIsNotNone(span.context.get_sampling_decision()) self.assertFalse(span.context.get_sampling_decision()) span.set_tag(opentracing.ext.tags.ERROR, True) self.assertIsNotNone(span.context.get_sampling_decision()) self.assertTrue(span.context.get_sampling_decision()) span = tracer.start_span('test_op') self.assertIsNotNone(span) self.assertIsNotNone(span.context) self.assertIsNotNone(span.context.get_sampling_decision()) self.assertFalse(span.context.get_sampling_decision()) span.set_tag("debug", True) self.assertIsNotNone(span.context.get_sampling_decision()) self.assertTrue(span.context.get_sampling_decision()) span = tracer.start_span('test_op') self.assertIsNotNone(span) self.assertIsNotNone(span.context) self.assertIsNotNone(span.context.get_sampling_decision()) self.assertFalse(span.context.get_sampling_decision()) span.set_tag("debug", "true") self.assertIsNotNone(span.context.get_sampling_decision()) self.assertTrue(span.context.get_sampling_decision())
def test_custom_red_metrics_tags(self, wf_sender): """Test custom RED metrics tags.""" operation_name = 'dummy_op' source = 'wavefront_source' wf_sender = wf_sender() tracer = WavefrontTracer(WavefrontSpanReporter(wf_sender, source), self.application_tags, samplers=[ConstantSampler(True)], report_frequency_millis=500, red_metrics_custom_tag_keys={'env', 'tenant'}) with freezegun.freeze_time(datetime.datetime( year=1, month=1, day=1)) as frozen_datetime: span = tracer.start_active_span(operation_name=operation_name, tags=[('tenant', 'tenant1'), ('env', 'staging')]) span.close() frozen_datetime.tick(delta=datetime.timedelta(seconds=61)) time.sleep(1) tracer.close() wf_sender.assert_has_calls([ mock.call.send_span(operation_name, mock.ANY, 0, source, mock.ANY, mock.ANY, [], [], [('tenant', 'tenant1'), ('env', 'staging'), ('application', 'app'), ('service', 'service'), ('cluster', 'us-west-1'), ('shard', 'primary'), ('custom_k', 'custom_v'), ('component', 'none')], span_logs=[]), mock.call.send_metric( name='tracing.derived.app.service.{}.invocation.' 'count'.format(operation_name), source=source, tags={ 'application': 'app', 'service': 'service', 'cluster': 'us-west-1', 'shard': 'primary', 'component': 'none', 'custom_k': 'custom_v', 'operationName': operation_name, 'tenant': 'tenant1', 'env': 'staging', 'span.kind': NULL_TAG_VAL }, timestamp=None, value=1), mock.call.send_metric( name='tracing.derived.app.service.{}.total_time.millis.' 'count'.format(operation_name), source=source, tags={ 'application': 'app', 'service': 'service', 'cluster': 'us-west-1', 'shard': 'primary', 'component': 'none', 'custom_k': 'custom_v', 'operationName': 'dummy_op', 'tenant': 'tenant1', 'env': 'staging', 'span.kind': NULL_TAG_VAL }, timestamp=None, value=mock.ANY), mock.call.send_metric( '~component.heartbeat', 1.0, mock.ANY, source, { 'application': 'app', 'cluster': 'us-west-1', 'service': 'service', 'shard': 'primary', 'custom_k': 'custom_v', 'component': 'wavefront-generated' }), mock.call.send_distribution( centroids=mock.ANY, histogram_granularities={'!M'}, name='tracing.derived.app.service.{}.duration.' 'micros'.format(operation_name), source=source, tags={ 'application': 'app', 'service': 'service', 'cluster': 'us-west-1', 'shard': 'primary', 'component': 'none', 'custom_k': 'custom_v', 'operationName': operation_name, 'tenant': 'tenant1', 'env': 'staging', 'span.kind': NULL_TAG_VAL }, timestamp=mock.ANY) ], any_order=True)