Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of ShardingTracer in sharding-jdbc

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the role of ShardingTracer in sharding-jdbc, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

ShardingTracer

Incubator-shardingsphere-4.0.0-RC1/sharding-opentracing/src/main/java/org/apache/shardingsphere/opentracing/ShardingTracer.java

@ NoArgsConstructor (access = AccessLevel.PRIVATE) public final class ShardingTracer {private static final String OPENTRACING_TRACER_CLASS_NAME = "org.apache.shardingsphere.opentracing.tracer.class"; / * * Initialize sharding tracer. * / public static void init () {String tracerClassName = System.getProperty (OPENTRACING_TRACER_CLASS_NAME); Preconditions.checkNotNull (tracerClassName, "Can not find opentracing tracer implementation class via system property `% s`", OPENTRACING_TRACER_CLASS_NAME); try {init ((Tracer) Class.forName (tracerClassName). NewInstance ());} catch (final ReflectiveOperationException ex) {throw new ShardingException ("Initialize opentracing tracer class failure.", ex) }} / * Initialize sharding tracer. * * @ param tracer opentracing tracer * / public static void init (final Tracer tracer) {if (! GlobalTracer.isRegistered ()) {GlobalTracer.register (tracer);} / * Get tracer. * * @ return tracer * / public static Tracer get () {return GlobalTracer.get ();}}

The init method of ShardingTracer reads the value of OPENTRACING_TRACER_CLASS_NAME in the system properties, then loads and creates the instance using Class.forName, and then registers in the GlobalTracer.register through opentracing

ShardingTracerTest

Incubator-shardingsphere-4.0.0-RC1/sharding-opentracing/src/test/java/org/apache/shardingsphere/opentracing/ShardingTracerTest.java

Public final class ShardingTracerTest {@ Before public void setUp () {System.setProperty ("org.apache.shardingsphere.opentracing.tracer.class", FooTracer.class.getName ()); clearGlobalTracer ();} @ After public void tearDown () {System.getProperties () .remove ("org.apache.shardingsphere.opentracing.tracer.class") } @ Test public void assertDuplicatedLoading () {ShardingTracer.init (mock (Tracer.class)); Tracer T1 = ShardingTracer.get (); ShardingTracer.init (); assertEquals (T1, ShardingTracer.get ()); ShardingTracer.init (mock (Tracer.class)); assertEquals (T1, ShardingTracer.get ());} @ Test public void assertTracer () {ShardingTracer.init () AssertThat ((GlobalTracer) ShardingTracer.get (), isA (GlobalTracer.class)); assertTrue (GlobalTracer.isRegistered ()); assertThat (ShardingTracer.get (), is (ShardingTracer.get ());} @ Test (expected = ShardingException.class) public void assertTracerClassError () {System.setProperty ("org.apache.shardingsphere.opentracing.tracer.class", "com.foo.FooTracer"); ShardingTracer.init () @ SneakyThrows private static void clearGlobalTracer () {Field tracerField = GlobalTracer.class.getDeclaredField ("tracer"); tracerField.setAccessible (true); tracerField.set (GlobalTracer.class, NoopTracerFactory.create ());}}

Here the OPENTRACING_TRACER_CLASS_ name value is specified as FooTracer

FooTracer

Incubator-shardingsphere-4.0.0-RC1/sharding-opentracing/src/test/java/org/apache/shardingsphere/opentracing/fixture/FooTracer.java

Public final class FooTracer implements Tracer {@ Override public SpanBuilder buildSpan (final String operationName) {return null;} @ Override public void inject (final SpanContext spanContext, final Format format, final C carrier) {} @ Override public SpanContext extract (final Format format, final C carrier) {return null;} @ Override public ActiveSpan activeSpan () {return null } @ Override public ActiveSpan makeActive (final Span span) {return null;}}

FooTracer implements the Tracer interface, here is a null implementation

Summary

The init method of ShardingTracer reads the value of OPENTRACING_TRACER_CLASS_NAME in the system properties, then loads and creates the instance using Class.forName, and then registers in the GlobalTracer.register through opentracing

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report