{"id":9240,"date":"2023-02-02T13:01:10","date_gmt":"2023-02-02T05:01:10","guid":{"rendered":"http:\/\/123.57.164.21\/?p=9240"},"modified":"2023-02-02T13:01:10","modified_gmt":"2023-02-02T05:01:10","slug":"opentracing-java-library%e6%95%99%e7%a8%8b4-baggage%e4%bb%8b%e7%bb%8d","status":"publish","type":"post","link":"https:\/\/92it.top\/?p=9240","title":{"rendered":"OpenTracing Java Library\u6559\u7a0b(4)\u2014\u2014Baggage\u4ecb\u7ecd"},"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\uff1a<\/p>\n\n\n\n<ul><li>\u7406\u89e3\u5206\u5e03\u5f0f\u4e0a\u4e0b\u6587\u7684\u4f20\u9012<\/li><li>\u4f7f\u7528baggage\u6765\u4f20\u9012\u6570\u636e<\/li><\/ul>\n\n\n\n<p><strong>\u5f00\u53d1\u6d41\u7a0b<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u5728\u4e0a\u4e00\u8282\u4e2d\u770b\u5230\uff0cSpanContext\u662f\u5982\u4f55\u5728\u4e0d\u540c\u7684\u670d\u52a1\u4e4b\u95f4\u4f20\u9012\u7684\u3002\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd9\u79cd\u673a\u5236\u901a\u7528\u5316\uff0c\u76f8\u5f53\u4e8e\u5b9e\u73b0\u4e86\u4e00\u4e2a\u5728\u5206\u5e03\u5f0f\u73af\u5883\u4e2d\u8fdb\u884c\u4fe1\u606f\u4f20\u9012\u7684\u901a\u9053\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5728\u670d\u52a1\u95f4\u4f20\u9012\u4e00\u4e9b\u81ea\u5b9a\u4e49\u6570\u636e\u4e86\u3002<\/p>\n\n\n\n<p>\u8fd9\u5c31\u662fOpenTracing\u5b9a\u4e49\u7684<strong>Baggage<\/strong>\uff1abaggage\u662fSpanContext\u7684\u4e00\u90e8\u5206\uff0c\u4e5f\u662f\u4ee5key-value\u5f62\u5f0f\u5b58\u5728\u7684\u3002\u548ctags\u3001logs\u7684\u5dee\u522b\u5728\u4e8e\uff0c<strong>baggage\u662f\u5168\u5c40\u4f20\u9012\u7684<\/strong>\u3002\u7236Span\u91cc\u9762\u7684baggage\u4fe1\u606f\u4f1a\u81ea\u52a8\u88ab\u6240\u6709\u5b50span\u7ee7\u627f\uff0c\u8fd9\u5c31\u662f\u6240\u8c13\u7684\u5168\u5c40\u6027\u3002\u4e5f\u5c31\u662f\u4f60\u80fd\u5728\u8c03\u7528\u94fe\u4e0a\u4efb\u610f\u5730\u65b9\u8bfb\u53d6\u5230\u8be5\u4fe1\u606f\u3002\u4e0b\u9762\u6211\u4eec\u770b\u4e2a\u5177\u4f53\u7684\u4f8b\u5b50\u3002<\/p>\n\n\n\n<p><strong>\u5728Client\u4e2d\u589e\u52a0Baggage<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u4ee3\u7801\u8fd8\u662f\u5728\u4e0a\u4e00\u7bc7\u6587\u7ae0\u7684\u4ee3\u7801\u4e4b\u4e0a\u4fee\u6539\u3002\u6211\u4eec\u518d\u589e\u52a0\u4e00\u4e2a\u547d\u4ee4\u884c\u53c2\u6570\uff0c\u7136\u540e\u5c06\u8fd9\u4e2a\u53c2\u6570\u653e\u5230baggage\u91cc\u9762\u3002\u4fee\u6539Hello\u7684main\u65b9\u6cd5\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=\"\">public static void main(String[] args) {\n    if (args.length != 2) {\n        throw new IllegalArgumentException(\"Expecting two arguments, helloTo and greeting\");\n    }\n    String helloTo = args[0];\n    String greeting = args[1];\n    Tracer tracer = Tracing.init(\"hello-world\");\n    new Hello(tracer).sayHello(helloTo, greeting);\n}<\/pre>\n\n\n\n<p>\u7136\u540e\u5c06\u7b2c\u4e8c\u4e2a\u53c2\u6570\u653e\u5230baggage\u91cc\u9762\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 void sayHello(String helloTo, String greeting) {\n    Span span = tracer.buildSpan(\"say-hello\").start();\n    try (Scope scope = tracer.scopeManager().activate(span)) {\n        span.setTag(\"hello-to\", helloTo);\n        \/\/ \u628a\u7b2c\u4e8c\u4e2a\u53c2\u6570\u4fe1\u606f\u653e\u5230baggage\u91cc\u9762\n        span.setBaggageItem(\"greeting\", greeting);\n\n        String helloStr = formatString(helloTo);\n        printHello(helloStr);\n    } finally {\n        span.finish();\n    }\n}<\/pre>\n\n\n\n<p>\u5176\u5b83\u5730\u65b9\u90fd\u4e0d\u7528\u4fee\u6539\u3002<\/p>\n\n\n\n<p><strong>\u5728Formatter\u670d\u52a1\u4e2d\u8bfb\u53d6Baggage<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\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=\"\">@GET\npublic String format(@QueryParam(\"helloTo\") String helloTo, @Context HttpHeaders httpHeaders) {\n    Span span = Tracing.startServerSpan(tracer, httpHeaders, \"format\");\n    try (Scope scope = tracer.scopeManager().activate(span)) {\n        \/\/ \u8bfb\u53d6Baggage\n        String greeting = span.getBaggageItem(\"greeting\");\n        if (greeting == null) {\n            greeting = \"Hello\";\n        }\n        String helloStr = String.format(\"%s, %s!\", greeting, helloTo);\n        span.log(ImmutableMap.of(\"event\", \"string-format\", \"value\", helloStr));\n        return helloStr;\n    } finally {\n        span.finish();\n    }\n}<\/pre>\n\n\n\n<p><strong>Baggage\u6ce8\u610f\u70b9<\/strong><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>\u9996\u5148\uff0c\u4e0a\u9762\u5b9e\u73b0\u7684\u529f\u80fd\u4e5f\u53ef\u4ee5\u901a\u8fc7\u5728\u63a5\u53e3\u4e2d\u589e\u52a0\u53c2\u6570\u6765\u5b9e\u73b0\u3002\u4f46\u670d\u52a1\u6bd4\u8f83\u591a\u7684\u65f6\u5019\u6bd4\u8f83\u9ebb\u70e6\uff0c\u800c\u4e14\u63a5\u53e3\u6539\u52a8\u662f\u4e00\u4e2a\u6bd4\u8f83\u5927\u7684\u6539\u52a8\uff0c\u4e00\u822c\u5b9a\u4e86\u4ee5\u540e\u4e0d\u80fd\u968f\u610f\u66f4\u6539\u3002\u800cBaggage\u662f\u4e0d\u9700\u8981\u6539\u52a8\u63a5\u53e3\u7684\uff0c\u57fa\u672c\u5bf9\u4e8e\u670d\u52a1\u81ea\u8eab\u662f\u900f\u660e\u7684\u3002\u8fd9\u4e5f\u662fBaggage\u7684\u610f\u4e49\u6240\u5728\u3002\u8fd9\u91cc\u5217\u4e3e\u4e00\u4e9bBaggage\u7684\u4f7f\u7528\u573a\u666f\uff1a<\/p>\n\n\n\n<ul><li>\u591a\u79df\u6237\u7cfb\u7edf\u7684\u79df\u6237\u4fe1\u606f<\/li><li>\u5e95\u5c42\u8c03\u7528\u8005\u7684\u6807\u8bc6\u4fe1\u606f<\/li><li>\u6df7\u6c8c\u5de5\u7a0b\u4e2d\u4f20\u9012\u4e00\u4e9b\u9519\u8bef\u6ce8\u5165\u6307\u4ee4<\/li><li>passing request-scoped dimensions for other monitoring data, like separating metrics for prod vs. test traffic<\/li><\/ul>\n\n\n\n<p><strong>\u4f46\u9700\u8981\u4e3b\u8981\u7684\u662fBaggage\u662f\u5168\u5c40\u4f20\u9012\u7684\uff0c\u6240\u4ee5\u6570\u636e\u91cf\u4e0d\u80fd\u592a\u5927\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u4ea7\u751f\u6027\u80fd\u95ee\u9898<\/strong>\u3002\u4e00\u4e9b\u5e93\/\u6846\u67b6\u4f1a\u5728\u5b9e\u73b0\u5c42\u9650\u5236\u8fd9\u4e2a\u5927\u5c0f<\/p>\n\n\n\n<p>Baggage\u662f\u94fe\u8def\u8ddf\u8e2a\u7ed9\u51fa\u7684\u4e00\u4e2a\u901a\u7528\u7684\u5206\u5e03\u5f0f\u6570\u636e\u4f20\u8f93\u673a\u5236\uff0c\u53ef\u4ee5\u6839\u636e\u573a\u666f\u5408\u7406\u5229\u7528\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u76ee\u6807 \u5b66\u4e60\uff1a \u7406\u89e3\u5206\u5e03\u5f0f\u4e0a\u4e0b\u6587\u7684\u4f20\u9012 \u4f7f\u7528baggage\u6765\u4f20\u9012\u6570\u636e \u5f00\u53d1\u6d41\u7a0b \u5728\u4e0a\u4e00\u8282\u4e2d\u770b\u5230\uff0cSpanCont [&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\/9240"}],"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=9240"}],"version-history":[{"count":1,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9240\/revisions"}],"predecessor-version":[{"id":9241,"href":"https:\/\/92it.top\/index.php?rest_route=\/wp\/v2\/posts\/9240\/revisions\/9241"}],"wp:attachment":[{"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=9240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=9240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/92it.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=9240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}