{"id":9230,"date":"2023-02-02T12:49:32","date_gmt":"2023-02-02T04:49:32","guid":{"rendered":"http:\/\/123.57.164.21\/?p=9230"},"modified":"2023-02-02T12:49:32","modified_gmt":"2023-02-02T04:49:32","slug":"opentracing-java-library%e6%95%99%e7%a8%8b2-%e8%bf%9b%e7%a8%8b%e9%97%b4%e4%bc%a0%e9%80%92spancontext","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=9230","title":{"rendered":"OpenTracing Java Library\u6559\u7a0b(2)\u2014\u2014\u8fdb\u7a0b\u95f4\u4f20\u9012SpanContext"},"content":{"rendered":"\n<p><strong>\u76ee\u6807<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5b66\u4e60\u5982\u4f55\uff1a<\/p>\n\n\n\n<ul><li>\u8ddf\u8e2a\u4e0d\u540c\u7684\u51fd\u6570<\/li><li>\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2aspan\u7684\u94fe\u8def<\/li><li>\u5728\u8fdb\u7a0b\uff08\u5176\u5b9e\u662f\u7ebf\u7a0b\uff09\u5185\u90e8\u4e0a\u4e0b\u6587\u4e4b\u95f4\u4f20\u9012\u94fe\u8def\u4fe1\u606f<\/li><\/ul>\n\n\n\n<p><strong>\u5f00\u53d1\u6b65\u9aa4<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p><strong>\u8ddf\u8e2a\u4e0d\u540c\u7684\u51fd\u6570<\/strong><\/p>\n\n\n\n<p>\u4e0a\u7bc7\u6587\u7ae0\u7684<code>sayHello(String helloTo)<\/code>\u65b9\u6cd5\u505a\u4e86\u4e24\u4ef6\u4e8b\u60c5\uff1a<em>\u683c\u5f0f\u5316\u8f93\u51fa\u5b57\u7b26\u4e32<\/em>\u548c<em>\u6253\u5370\u8f93\u51fa<\/em>\u3002\u56e0\u4e3a\u5728\u4e00\u4e2a\u65b9\u6cd5\u91cc\u9762\uff0c\u6240\u4ee5\u53ea\u4ea7\u751f\u4e86\u4e00\u4e2aspan\uff0c\u4e3a\u4e86\u6f14\u793a\u5982\u4f55\u4ea7\u751f\u591a\u4e2asan\uff0c\u8fd9\u91cc\u6211\u4eec\u5c06\u8fd9\u4e2a\u65b9\u6cd5\u62c6\u5206\u6210\u4e24\u4e2a\u5355\u72ec\u7684\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">String helloStr = formatString(span, helloTo);\nprintHello(span, helloStr);<\/pre>\n\n\n\n<p>\u51fd\u6570\u4f53\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u683c\u5f0f\u5316\u5b57\u7b26\u4e32\nprivate String formatString(Span span, String helloTo) {\n    String helloStr = String.format(\"Hello, %s!\", helloTo);\n    span.log(ImmutableMap.of(\"event\", \"string-format\", \"value\", helloStr));\n    return helloStr;\n}\n\/\/ \u6253\u5370\u8f93\u51fa\nprivate void printHello(Span span, String helloStr) {\n    System.out.println(helloStr);\n    span.log(ImmutableMap.of(\"event\", \"println\"));\n}<\/pre>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u518d\u7ed9\u6bcf\u4e2a\u51fd\u6570\u91cc\u9762\u52a0\u4e0aspan\u4fe1\u606f\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">private  String formatString(Span rootSpan, String helloTo) {\n    Span span = tracer.buildSpan(\"formatString\").start();\n    try {\n        String helloStr = String.format(\"Hello, %s!\", helloTo);\n        span.log(ImmutableMap.of(\"event\", \"string-format\", \"value\", helloStr));\n        return helloStr;\n    } finally {\n        span.finish();\n    }\n}\n\nprivate void printHello(Span rootSpan, String helloStr) {\n    Span span = tracer.buildSpan(\"printHello\").start();\n    try {\n        System.out.println(helloStr);\n        span.log(ImmutableMap.of(\"event\", \"println\"));\n    } finally {\n        span.finish();\n    }\n}<\/pre>\n\n\n\n<p>\u8fd0\u884c\uff08\u8fd9\u622a\u53d6\u4e86span\u76f8\u5173\u7684\u65e5\u5fd7\u4fe1\u606f\uff09\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">-> % .\/run.sh lesson02.solution.HelloManual Bryan\n08:07:28.078 [main] INFO io.jaegertracing.internal.reporters.LoggingReporter - Span reported: 7e2c775eb9a9ac55:7e2c775eb9a9ac55:0:1 - formatString\nHello, Bryan!\n08:07:28.087 [main] INFO io.jaegertracing.internal.reporters.LoggingReporter - Span reported: ab294a35b8deb4c3:ab294a35b8deb4c3:0:1 - printHello\n08:07:28.087 [main] INFO io.jaegertracing.internal.reporters.LoggingReporter - Span reported: 45dcde709e09015d:45dcde709e09015d:0:1 - say-hello\n<\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7b80\u5355\u4ecb\u7ecd\u4e00\u4e0b<code>Span reported:<\/code>\u540e\u9762\u7684\u8f93\u51fa\uff0c\u8fd9\u91cc\u6709\u51e0\u4e2a\u4f7f\u7528\u5192\u53f7\u5206\u9694\u7684ID\uff1a<\/p>\n\n\n\n<ul><li>\u7b2c\u4e00\u4e2aID\u4ee3\u8868traceID\uff0c\u552f\u4e00\u6807\u8bc6\u4e00\u4e2a\u94fe\u8def\uff1b<\/li><li>\u7b2c\u4e8c\u4e2a\u662fspanID\uff0c\u552f\u4e00\u6807\u8bc6\u4e00\u4e2aspan\uff1b<\/li><li>\u7b2c\u4e09\u4e2a\u662f\u5f53\u524dspan\u7684\u7236spanID\uff0c\u5982\u679c\u6ca1\u6709\uff08\u5373\u81ea\u5df1\u5c31\u662f\u6839\u8282\u70b9\uff09\uff0c\u5219\u4e3a0\u3002<\/li><\/ul>\n\n\n\n<p>\u8fd9\u91cc\u7684\u786e\u4ea7\u751f\u4e86\u4e09\u4e2aspan\uff0c\u4f46\u8fd9\u4e09\u4e2aspan\u7684TraceID\u5404\u4e0d\u4e00\u6837\uff0c\u4e5f\u5c31\u662f\u8bf4\u8fd9\u4e09\u4e2aspan\u5206\u522b\u4ee3\u8868\u4e00\u4e2a\u94fe\u8def\uff0c\u5373\u6211\u4eec\u521b\u5efa\u4e86\u4e09\u4e2atrace\uff0c\u6bcf\u4e2atrace\u5305\u542b1\u4e2aspan\uff0c\u8fd9\u663e\u7136\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\uff0c\u56e0\u4e3a\u8fd9\u91cc\u4e09\u4e2aspan\u662f\u6709\u8c03\u7528\u5173\u7cfb\u7684\uff0c\u5b83\u4eec\u6700\u7ec8\u5e94\u8be5\u5f62\u6210\u4e00\u4e2atrace\uff0c\u8fd9\u4e2atrace\u5305\u542b3\u4e2aspan\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"514\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/02\/image-25-1024x514.png\" alt=\"\" class=\"wp-image-9231\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-25-1024x514.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-25-300x150.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-25-768x385.png 768w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-25-830x416.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-25-230x115.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-25-350x176.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-25-480x241.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-25.png 1248w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>\u4ea7\u751f\u8fd9\u4e2a\u7ed3\u679c\u7684\u539f\u56e0\u662f<code>tracer.buildSpan(\"\u64cd\u4f5c\u540d\").start();<\/code>\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u7684span\uff0c\u800c\u4e14\u9ed8\u8ba4\u8fd9\u4e2aspan\u5c31\u662f\u6839\u8282\u70b9\uff0c\u5982\u679c\u9700\u8981\u5c06\u5176\u4f5c\u4e3a\u5b50\u8282\u70b9\u7684\u8bdd\uff0c\u9700\u8981\u4f7f\u7528<code>ChildOf<\/code>\u660e\u786e\u6307\u51fa\u5176\u7236\u8282\u70b9\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ formatString\nSpan span = tracer.buildSpan(\"formatString\").asChildOf(rootSpan).start();\n\/\/ printHello\nSpan span = tracer.buildSpan(\"printHello\").asChildOf(rootSpan).start();<\/pre>\n\n\n\n<p>\u4fee\u6539\u4ee3\u7801\u540e\uff0c\u91cd\u65b0\u8fd0\u884c\u51fd\u6570\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">.\/run.sh lesson02.solution.HelloManual Bryan\n08:20:56.204 [main] INFO io.jaegertracing.internal.reporters.LoggingReporter - Span reported: 836db6b9e1cb6e87:159e8c7d440d171a:836db6b9e1cb6e87:1 - formatString\nHello, Bryan!\n08:20:56.209 [main] INFO io.jaegertracing.internal.reporters.LoggingReporter - Span reported: 836db6b9e1cb6e87:149c8f7aeadb5319:836db6b9e1cb6e87:1 - printHello\n08:20:56.209 [main] INFO io.jaegertracing.internal.reporters.LoggingReporter - Span reported: 836db6b9e1cb6e87:836db6b9e1cb6e87:0:1 - say-hello<\/pre>\n\n\n\n<p>\u8fd9\u6b21\u53ef\u4ee5\u770b\u5230\uff0c\u4e09\u4e2aspan\u7684traceID\u662f\u4e00\u6837\u7684\uff0c\u8bf4\u660e\u5b83\u4eec\u4e09\u4e2a\u90fd\u5c5e\u4e8e\u4e00\u4e2atrace\uff0c\u4e14\u524d\u4e24\u4e2aspanID\u7684\u7236spanID\u90fd\u662f\u6700\u540e\u4e00\u4e2a\uff0c\u8fd9\u4e2a\u7ed3\u679c\u662f\u6bd4\u8f83\u7b26\u5408\u9884\u671f\u7684\u3002\u6211\u4eec\u518d\u770b\u4e0b\u94fe\u8def\u56fe\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/02\/image-26-1024x519.png\" alt=\"\" class=\"wp-image-9232\" width=\"590\" height=\"298\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-26-1024x519.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-26-300x152.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-26-830x421.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-26-230x117.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-26-350x178.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-26-480x243.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-26.png 1246w\" sizes=\"(max-width: 590px) 100vw, 590px\" \/><\/figure><\/div>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230\u53ea\u65b0\u4ea7\u751f\u4e86\u4e00\u4e2atrace\uff0c\u4e14\u91cc\u9762\u5305\u542b\u4e863\u4e2aspan\u3002\u70b9\u5f00\u8fd9\u4e2atrace\uff1a<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/123.57.164.21\/wp-content\/uploads\/2023\/02\/image-27-1024x302.png\" alt=\"\" class=\"wp-image-9233\" width=\"594\" height=\"174\" srcset=\"https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-27-1024x302.png 1024w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-27-300x89.png 300w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-27-830x245.png 830w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-27-230x68.png 230w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-27-350x103.png 350w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-27-480x142.png 480w, https:\/\/92it.top\/wp-content\/uploads\/2023\/02\/image-27.png 1240w\" sizes=\"(max-width: 594px) 100vw, 594px\" \/><\/figure><\/div>\n\n\n\n<p>\u4ece\u8fd9\u4e2a\u94fe\u8def\u56fe\u5c31\u53ef\u4ee5\u770b\u51fa\u6765\u51fd\u6570\u95f4\u7684\u8c03\u7528\u5173\u7cfb\uff0c\u4ee5\u53ca\u5404\u4e2a\u8c03\u7528\u7684\u8017\u65f6\u3002\u5982\u679c\u70b9\u51fb\u6bcf\u4e2aspan\uff0c\u8fd8\u53ef\u4ee5\u67e5\u770b\u9644\u52a0\u7684\u4e00\u4e9b\u57fa\u672c\u4fe1\u606f\u3002<\/p>\n\n\n\n<p>\u81f3\u6b64\uff0c\u529f\u80fd\u57fa\u672c\u5c31\u5b8c\u6210\u4e86\u3002\u7f8e\u4e2d\u4e0d\u8db3\u7684\u662f\u6bcf\u4e2a\u51fd\u6570\u8c03\u7528\u90fd\u8981\u4f20\u9012\u7236span\uff0c\u975e\u5e38\u4e0d\u65b9\u4fbf\uff0c\u5e78\u8fd0\u7684\u662fjava\u6709thread local\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528\u8fd9\u4e2a\u673a\u5236\u7701\u7565\u6389\u540c\u4e00\u4e2a\u7ebf\u7a0b\u91cc\u9762\u51fd\u6570\u95f4\u7684\u8fd9\u4e2a\u53c2\u6570\u4f20\u9012\u3002\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import io.opentracing.Scope;\n\nprivate void sayHello(String helloTo) {\n    Span span = tracer.buildSpan(\"say-hello\").start();\n    try (Scope scope = tracer.scopeManager().activate(span)) {\n        span.setTag(\"hello-to\", helloTo);\n        \n        String helloStr = formatString(helloTo);\n        printHello(helloStr);\n    } finally {\n        span.finish();\n    }\n}\n\nprivate  String formatString(String helloTo) {\n    Span span = tracer.buildSpan(\"formatString\").start();\n    try (Scope scope = tracer.scopeManager().activate(span)) {\n        String helloStr = String.format(\"Hello, %s!\", helloTo);\n        span.log(ImmutableMap.of(\"event\", \"string-format\", \"value\", helloStr));\n        return helloStr;\n    } finally {\n        span.finish();\n    }\n}\n\nprivate void printHello(String helloStr) {\n    Span span = tracer.buildSpan(\"printHello\").start();\n    try (Scope scope = tracer.scopeManager().activate(span)) {\n        System.out.println(helloStr);\n        span.log(ImmutableMap.of(\"event\", \"println\"));\n    } finally {\n        span.finish();\n    }\n}<\/pre>\n\n\n\n<p>\u4ee3\u7801\u8bf4\u660e\uff1a<\/p>\n\n\n\n<ul><li><code>trace.scopeManager().active(span)<\/code> \u4f7f\u5f53\u524dspan\u53d8\u4e3aactive span\uff08\u6bcf\u4e2a\u7ebf\u7a0b\u5185\u53ea\u80fd\u6709\u4e00\u4e2aactive span\uff09\u3002\u5f53\u8fd9\u4e2aspan\u5173\u95ed\u540e\uff0c\u4e0a\u4e00\u4e2aspan\u4f1a\u81ea\u52a8\u53c8\u53d8\u4e3aactive span\u3002<\/li><li><code>Scope<\/code> \u5b9e\u73b0\u4e86AutoCloseable\u63a5\u53e3, \u6240\u4ee5\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528try-with-resource\u8bed\u6cd5.<\/li><li>\u5982\u679c\u5f53\u524d\uff08\u5f53\u524d\u7ebf\u7a0b\u5185\uff09\u5df2\u7ecf\u6709\u4e00\u4e2aactive span\u7684\u8bdd\uff0c\u4f7f\u7528<code>buildSpan()<\/code>\u521b\u5efa\u65b0span\u7684\u65f6\u5019\uff0c\u4f1a\u81ea\u52a8\u5c06\u5f53\u524dactive span\u8bbe\u7f6e\u4e3a\u65b0span\u7684\u7236span\u3002<\/li><\/ul>\n\n\n\n<p>\u4fee\u6539\u5b8c\u540e\u8fd0\u884c\u7a0b\u5e8f\uff0c\u6548\u679c\u548c\u524d\u9762\u6211\u4eec\u81ea\u5df1\u5728\u51fd\u6570\u95f4\u4f20\u9012span\u4fe1\u606f\u662f\u4e00\u6837\u7684\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u6807 \u5b66\u4e60\u5982\u4f55\uff1a \u8ddf\u8e2a\u4e0d\u540c\u7684\u51fd\u6570 \u521b\u5efa\u4e00\u4e2a\u5305\u542b\u591a\u4e2aspan\u7684\u94fe\u8def \u5728\u8fdb\u7a0b\uff08\u5176\u5b9e\u662f\u7ebf\u7a0b\uff09\u5185\u90e8\u4e0a\u4e0b\u6587\u4e4b\u95f4\u4f20\u9012\u94fe\u8def\u4fe1 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"_links":{"self":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9230"}],"collection":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=9230"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9230\/revisions"}],"predecessor-version":[{"id":9234,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9230\/revisions\/9234"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9230"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9230"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9230"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}