{"id":38319,"date":"2025-04-23T21:07:53","date_gmt":"2025-04-23T13:07:53","guid":{"rendered":"https:\/\/www.yunshan.net\/blog\/?p=38319"},"modified":"2025-04-23T21:07:53","modified_gmt":"2025-04-23T13:07:53","slug":"opentelemetry%e7%9a%84%e6%95%b0%e6%8d%ae%e9%94%99%e8%af%af%e5%a4%84%e7%90%86%e5%92%8c%e5%bc%82%e5%b8%b8%e7%ae%a1%e7%90%86%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0","status":"publish","type":"post","link":"https:\/\/www.yunshan.net\/blog\/archives\/38319.html","title":{"rendered":"OpenTelemetry\u7684\u6570\u636e\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u7ba1\u7406\u5982\u4f55\u5b9e\u73b0"},"content":{"rendered":"<p style=\"text-align: center;\">\n<p><img decoding=\"async\" src=\"https:\/\/pic.zadbj010.com\/2025\/0c3261d0f846463.webp\" alt=\"OpenTelemetry\u7684\u6570\u636e\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u7ba1\u7406\u5982\u4f55\u5b9e\u73b0\" ><\/p>\n<\/p>\n<h2>1. \u9519\u8bef\u8bb0\u5f55\u57fa\u7840<\/h2>\n<p>OpenTelemetry\u901a\u8fc7\u4e24\u79cd\u4e3b\u8981\u65b9\u5f0f\u5904\u7406\u9519\u8bef\uff1a<\/p>\n<li><strong>Span\u72b6\u6001\u6807\u8bb0<\/strong>\uff1a\u5c06Span\u6807\u8bb0\u4e3a\u9519\u8bef\u72b6\u6001\u3002<\/li>\n<li><strong>\u4e8b\u4ef6\u8bb0\u5f55<\/strong>\uff1a\u5728Span\u4e2d\u6dfb\u52a0\u9519\u8bef\u4e8b\u4ef6\u8be6\u60c5\u3002<\/li>\n<h2>\u57fa\u672c\u9519\u8bef\u6807\u8bb0\u793a\u4f8b<\/h2>\n<p>javascript<\/p>\n<p>const { trace } = require(&#8216;@opentelemetry\/api&#8217;);<\/p>\n<p>function riskyOperation {<\/p>\n<p>const tracer = trace.getTracer(&#8216;error-demo&#8217;);<\/p>\n<p>const span = tracer.startSpan(&#8216;riskyOperation&#8217;);<\/p>\n<p>try {<\/p>\n<p>throw new Error(&#8216;Database connection failed&#8217;);<\/p>\n<p>} catch (err) {<\/p>\n<p>span.setStatus({<\/p>\n<p>code: trace.StatusCode.ERROR,<\/p>\n<p>message: err.message<\/p>\n<p>});<\/p>\n<p>span.recordException(err);<\/p>\n<p>throw err;<\/p>\n<p>} finally {<\/p>\n<p>span.end;<\/p>\n<p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c`riskyOperation` \u51fd\u6570\u5185\u90e8\u7684 `try-catch` \u5757\u6355\u83b7\u4e86\u4e00\u4e2a\u9519\u8bef\u3002\u4e00\u65e6\u6355\u83b7\u5230\u9519\u8bef\uff0c\u901a\u8fc7 `span.setStatus` \u5c06Span\u6807\u8bb0\u4e3a\u9519\u8bef\u72b6\u6001\uff0c\u5e76\u4f7f\u7528 `span.recordException` \u8bb0\u5f55\u9519\u8bef\u7684\u8be6\u7ec6\u4fe1\u606f\u3002<\/p>\n<h2>\u9519\u8bef\u5904\u7406\u8fdb\u9636\u6280\u5de7<\/h2>\n<p>1. <strong>\u9519\u8bef\u5c5e\u6027\u589e\u5f3a<\/strong>\uff1a\u4e3a\u9519\u8bef\u6dfb\u52a0\u81ea\u5b9a\u4e49\u5c5e\u6027\uff0c\u4fbf\u4e8e\u540e\u7eed\u5206\u6790\u3002<\/p>\n<p>javascript<\/p>\n<p>span.recordException(err, {<\/p>\n<p>&#8216;error.type&#8217;: err.constructor.name,<\/p>\n<p>&#8216;error.stack&#8217;: err.stack,<\/p>\n<p>&#8216;service.version&#8217;: &#8216;1.0.1&#8217;<\/p>\n<p>});<\/p>\n<p>\u901a\u8fc7\u589e\u5f3a\u9519\u8bef\u5c5e\u6027\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u9519\u8bef\u53d1\u751f\u7684\u4e0a\u4e0b\u6587\u3002<\/p>\n<p>2. <strong>\u9519\u8bef\u4e25\u91cd\u5ea6\u5206\u7ea7<\/strong>\uff1a\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u5bf9\u9519\u8bef\u8fdb\u884c\u4e25\u91cd\u5ea6\u5206\u7ea7\uff0c\u4ee5\u4fbf\u66f4\u6709\u9488\u5bf9\u6027\u5730\u5904\u7406\u548c\u54cd\u5e94\u3002<\/p>\n<p>3. <strong>\u8de8\u670d\u52a1\u9519\u8bef\u4f20\u64ad<\/strong>\uff1a\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\uff0c\u9519\u8bef\u9700\u8981\u8de8\u670d\u52a1\u8fb9\u754c\u4f20\u64ad\u3002<\/p>\n<p>javascript<\/p>\n<p>const span = tracer.startSpan(&#8216;clientCall&#8217;);<\/p>\n<p>try {<\/p>\n<p>await service.call;<\/p>\n<p>} catch (err) {<\/p>\n<p>span.setStatus({ code: trace.StatusCode.ERROR });<\/p>\n<p>span.recordException(err);<\/p>\n<p>throw err;<\/p>\n<p>} finally {<\/p>\n<p>span.end;<\/p>\n<p>const ctx = propagation.extract(context.active, carrier);<\/p>\n<p>const span = tracer.startSpan(&#8216;serverProcess&#8217;, { kind: SpanKind.SERVER }, ctx);<\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c55\u793a\u4e86\u5982\u4f55\u5728\u5ba2\u6237\u7aef\u548c\u670d\u52a1\u5668\u4e4b\u95f4\u4f20\u64ad\u9519\u8bef\uff0c\u786e\u4fdd\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u9519\u8bef\u7684\u4e00\u81f4\u6027\u548c\u53ef\u8ffd\u6eaf\u6027\u3002<\/p>\n<h2>2. \u5b9e\u9645\u5e94\u7528\u6848\u4f8b<\/h2>\n<p>\u4ee5\u7535\u5546\u8ba2\u5355\u5904\u7406\u4e3a\u4f8b\uff0c\u5c55\u793aOpenTelemetry\u5728\u5b9e\u9645\u4e1a\u52a1\u4e2d\u7684\u9519\u8bef\u5904\u7406\u80fd\u529b\u3002<\/p>\n<p>javascript<\/p>\n<p>async function processOrder(orderId) {<\/p>\n<p>const span = tracer.startSpan(&#8216;processOrder&#8217;);<\/p>\n<p>try {<\/p>\n<p>const payment = await chargeCreditCard(orderId);<\/p>\n<p>span.addEvent(&#8216;payment_processed&#8217;, { amount: payment.amount });<\/p>\n<p>const inventory = await updateInventory(orderId);<\/p>\n<p>span.addEvent(&#8216;inventory_updated&#8217;);<\/p>\n<p>await sendConfirmationEmail(orderId);<\/p>\n<p>} catch (err) {<\/p>\n<p>span.setStatus({ code: trace.StatusCode.ERROR });<\/p>\n<p>const attributes = {<\/p>\n<p>&#8216;order.id&#8217;: orderId,<\/p>\n<p>&#8216;error.retryable&#8217;: isRetryableError(err)<\/p>\n<p>};<\/p>\n<p>span.recordException(err, attributes);<\/p>\n<p>logger.error(`Order ${orderId} failed`, { error: err });<\/p>\n<p>if (isRetryableError(err)) {<\/p>\n<p>await retryOrder(orderId);<\/p>\n<p>} finally {<\/p>\n<p>span.end;<\/p>\n<p>\u5728\u8fd9\u4e2a\u6848\u4f8b\u4e2d\uff0c`processOrder` \u51fd\u6570\u5904\u7406\u7535\u5546\u8ba2\u5355\u7684\u6574\u4e2a\u6d41\u7a0b\u3002\u5982\u679c\u5728\u652f\u4ed8\u3001\u5e93\u5b58\u66f4\u65b0\u6216\u90ae\u4ef6\u53d1\u9001\u8fc7\u7a0b\u4e2d\u53d1\u751f\u9519\u8bef\uff0cOpenTelemetry\u4f1a\u6355\u83b7\u5e76\u8bb0\u5f55\u8fd9\u4e9b\u9519\u8bef\uff0c\u540c\u65f6\u63d0\u4f9b\u8be6\u7ec6\u7684\u8ba2\u5355\u548c\u9519\u8bef\u4fe1\u606f\uff0c\u4ee5\u4fbf\u8fdb\u884c\u540e\u7eed\u7684\u5206\u6790\u548c\u5904\u7406\u3002<\/p>\n<h2>3. \u6700\u4f73\u5b9e\u8df5<\/h2>\n<li><strong>\u59cb\u7ec8\u5728\u9519\u8bef\u5904\u7406\u4e2d\u8c03\u7528 `span.recordException`<\/strong>\uff1a\u786e\u4fdd\u6240\u6709\u6355\u83b7\u7684\u9519\u8bef\u90fd\u88ab\u8bb0\u5f55\u4e0b\u6765\u3002<\/li>\n<li><strong>\u4e3a\u9519\u8bef\u6dfb\u52a0\u4e1a\u52a1\u4e0a\u4e0b\u6587\u5c5e\u6027<\/strong>\uff1a\u5e2e\u52a9\u7406\u89e3\u9519\u8bef\u53d1\u751f\u7684\u4e1a\u52a1\u573a\u666f\u3002<\/li>\n<li><strong>\u4fdd\u6301\u9519\u8bef\u6d88\u606f\u7b80\u6d01\u4f46\u4fe1\u606f\u4e30\u5bcc<\/strong>\uff1a\u907f\u514d\u5197\u957f\u7684\u9519\u8bef\u6d88\u606f\uff0c\u540c\u65f6\u786e\u4fdd\u5305\u542b\u5173\u952e\u4fe1\u606f\u3002<\/li>\n<li><strong>\u907f\u514d\u8bb0\u5f55\u654f\u611f\u4fe1\u606f<\/strong>\uff1a\u4fdd\u62a4\u7528\u6237\u9690\u79c1\u548c\u5b89\u5168\u3002<\/li>\n<h2>\u4e8c\u3001OpenTelemetry\u7684\u5f02\u5e38\u7ba1\u7406<\/h2>\n<h2>1. \u5f02\u5e38\u5206\u6790<\/h2>\n<p>\u5f02\u5e38\u5206\u6790\u662f\u8bc6\u522b\u3001\u8bca\u65ad\u548c\u89e3\u51b3\u7cfb\u7edf\u6216\u6570\u636e\u4e2d\u5f02\u5e38\u60c5\u51b5\u7684\u91cd\u8981\u73af\u8282\u3002\u901a\u8fc7\u5bf9\u5f02\u5e38\u73b0\u8c61\u7684\u6df1\u5165\u7814\u7a76\uff0c\u53ef\u4ee5\u5e2e\u52a9\u4f01\u4e1a\u6216\u7ec4\u7ec7\u53ca\u65f6\u53d1\u73b0\u6f5c\u5728\u95ee\u9898\uff0c\u907f\u514d\u98ce\u9669\u6269\u5927\uff0c\u5e76\u91c7\u53d6\u6709\u6548\u7684\u5e94\u5bf9\u63aa\u65bd\u3002<\/p>\n<h2>\u64cd\u4f5c\u6b65\u9aa4<\/h2>\n<p>1. <strong>\u767b\u5f55\u53ef\u89c2\u6d4b\u94fe\u8defOpenTelemetry\u7248\u63a7\u5236\u53f0<\/strong>\u3002<\/p>\n<p>2. <strong>\u9009\u62e9\u5de6\u4fa7\u5bfc\u822a\u83dc\u5355\u573a\u666f\u5316\u5206\u6790>\u5f02\u5e38\u5206\u6790<\/strong>\u3002<\/p>\n<p>3. <strong>\u5728\u9875\u9762\u53f3\u4e0a\u89d2\uff0c\u6839\u636e\u9700\u6c42\u9009\u62e9\u7279\u5b9a\u7684\u65f6\u95f4\u8303\u56f4\u6570\u636e\u8fdb\u884c\u5c55\u793a\u5206\u6790<\/strong>\u3002<\/p>\n<p>4. <strong>\u5728\u5feb\u6377\u7b5b\u9009\u533a\u57df\uff0c\u6309\u5f02\u5e38\u540d\u79f0\u3001\u63a5\u53e3\u540d\u79f0\u3001\u5e94\u7528\u540d\u79f0\u3001\u5b9e\u4f8b\u5bf9\u5f02\u5e38\u8fdb\u884c\u7b5b\u9009\u8fc7\u6ee4<\/strong>\u3002<\/p>\n<p>5. <strong>\u5728\u8d8b\u52bf\u56fe\u533a\u57df\uff0c\u67e5\u770b\u6307\u5b9a\u65f6\u95f4\u8303\u56f4\u5185\u7684\u5f02\u5e38\u6b21\u6570\uff0c\u6309\u7167\u5f02\u5e38\u540d\u79f0\u505a\u5806\u53e0\u5c55\u793a<\/strong>\u3002<\/p>\n<p>6. <strong>\u5728\u5f02\u5e38\u5217\u8868\u533a\u57df\uff0c\u67e5\u770b\u5f02\u5e38\u540d\u79f0\u3001\u5bf9\u5e94\u629b\u51fa\u5f02\u5e38\u7684\u5e94\u7528\u3001\u5f02\u5e38\u6b21\u6570\u3001\u5360\u6bd4\u3001\u5f02\u5e38\u4fe1\u606f\u6458\u8981\u7b49<\/strong>\u3002<\/p>\n<h2>\u5f02\u5e38\u5217\u8868\u64cd\u4f5c<\/h2>\n<li><strong>\u5355\u51fb\u5e94\u7528\u540d\u79f0<\/strong>\uff1a\u67e5\u770b\u76ee\u6807\u5e94\u7528\u7684\u76d1\u63a7\u8be6\u60c5\u3002<\/li>\n<li><strong>\u5355\u51fb\u64cd\u4f5c\u5217\u7684\u6982\u89c8<\/strong>\uff1a\u5728\u53f3\u4fa7\u9762\u677f\u4e2d\u67e5\u770b\u8be5\u5f02\u5e38\u7684\u5f02\u5e38\u6570\u8d8b\u52bf\u3001\u63a5\u53e3\u548c\u5b9e\u4f8b\u7ef4\u5ea6\u7684\u5f02\u5e38\u5206\u5e03\u3001\u5f02\u5e38\u5806\u6808\u7b49\u6982\u89c8\u4fe1\u606f\u3002<\/li>\n<li><strong>\u5355\u51fb\u64cd\u4f5c\u5217\u7684\u8c03\u7528\u94fe<\/strong>\uff1a\u67e5\u770b\u5bf9\u5e94\u8c03\u7528\u7684\u94fe\u8def\u8be6\u60c5\u3002<\/li>\n<h2>2. \u57fa\u4e8e\u52a8\u6001\u9608\u503c\u7684\u6307\u6807\u5f02\u5e38\u68c0\u6d4b<\/h2>\n<p>\u5bf9\u4e8e\u5728\u6b63\u5e38\u72b6\u6001\u4e0b\u8d77\u4f0f\u4e0d\u5b9a\u7684\u6307\u6807\uff08\u5982RT\u548cQPS\uff09\uff0c\u53ef\u4ee5\u4f7f\u7528\u533a\u95f4\u68c0\u6d4b\u529f\u80fd\uff0c\u901a\u8fc7\u52a8\u6001\u9608\u503c\u5bf9\u6307\u6807\u6570\u636e\u8fdb\u884c\u5f02\u5e38\u68c0\u6d4b\u3002<\/p>\n<h2>\u5e94\u7528\u573a\u666f<\/h2>\n<li><strong>\u5e94\u7528\u6027\u80fd\u76d1\u63a7<\/strong>\uff1a\u7f51\u7ad9\u7ba1\u7406\u5458\u53ef\u4ee5\u4f7f\u7528\u6b64\u529f\u80fd\u6765\u76d1\u6d4b\u7ad9\u70b9\u7684\u54cd\u5e94\u65f6\u95f4\u3001\u8bf7\u6c42\u901f\u5ea6\u7b49\u9ec4\u91d1\u6307\u6807\u3002<\/li>\n<li><strong>\u670d\u52a1\u5668\u8d44\u6e90\u4f18\u5316<\/strong>\uff1a\u53ef\u4ee5\u76d1\u6d4b\u670d\u52a1\u5668\u7684CPU\u548c\u5185\u5b58\u4f7f\u7528\u60c5\u51b5\u3002<\/li>\n<li><strong>\u5e94\u7528\u8fde\u63a5\u6c60\u5206\u6790<\/strong>\uff1a\u53ef\u4ee5\u5229\u7528\u6b64\u529f\u80fd\u6765\u76d1\u63a7\u67e5\u8be2\u901f\u5ea6\u3001\u5e76\u53d1\u8fde\u63a5\u6570\u7b49\u5173\u952e\u6307\u6807\u3002<\/li>\n<li><strong>\u5fae\u670d\u52a1\u67b6\u6784\u76d1\u6d4b<\/strong>\uff1a\u5728\u5fae\u670d\u52a1\u67b6\u6784\u4e2d\uff0c\u5404\u4e2a\u670d\u52a1\u4e4b\u95f4\u7684\u4ea4\u4e92\u548c\u4f9d\u8d56\u5173\u7cfb\u590d\u6742\uff0c\u901a\u8fc7\u6b64\u529f\u80fd\uff0c\u56e2\u961f\u53ef\u4ee5\u76d1\u6d4b\u6bcf\u4e2a\u670d\u52a1\u7684\u8d44\u6e90\u4f7f\u7528\u548c\u54cd\u5e94\u6027\u80fd\u3002<\/li>\n<h2>\u914d\u7f6e\u533a\u95f4\u68c0\u6d4b<\/h2>\n<p>1. <strong>\u767b\u5f55\u53ef\u89c2\u6d4b\u94fe\u8defOpenTelemetry\u7248\u63a7\u5236\u53f0<\/strong>\u3002<\/p>\n<p>2. <strong>\u5728\u5de6\u4fa7\u5bfc\u822a\u680f\u4e2d\u9009\u62e9\u544a\u8b66\u7ba1\u7406>\u544a\u8b66\u89c4\u5219<\/strong>\u3002<\/p>\n<p>3. <strong>\u5728\u544a\u8b66\u89c4\u5219\u9875\u9762\u5355\u51fb\u521b\u5efa\u544a\u8b66\u89c4\u5219<\/strong>\u3002<\/p>\n<p>4. <strong>\u5728\u521b\u5efa\u544a\u8b66\u89c4\u5219\u9875\u9762\u81ea\u5b9a\u4e49\u544a\u8b66\u540d\u79f0\uff0c\u5e76\u9009\u62e9\u544a\u8b66\u68c0\u6d4b\u7c7b\u578b\u4e3a\u533a\u95f4\u68c0\u6d4b<\/strong>\u3002<\/p>\n<p>5. <strong>\u5728\u544a\u8b66\u5bf9\u8c61\u533a\u57df\u6839\u636e\u9700\u6c42\u9009\u62e9\u5bf9\u5e94\u7684\u544a\u8b66\u5e94\u7528\u3001\u6307\u6807\u7c7b\u578b\u548c\u7b5b\u9009\u6761\u4ef6<\/strong>\u3002<\/p>\n<p>6. <strong>\u5728\u544a\u8b66\u89c4\u5219\u533a\u57df\u8bbe\u7f6e\u544a\u8b66\u6761\u4ef6<\/strong>\u3002<\/p>\n<p>7. <strong>\u8bbe\u7f6e\u544a\u8b66\u901a\u77e5\u548c\u9ad8\u7ea7\u544a\u8b66\u8bbe\u7f6e<\/strong>\u3002<\/p>\n<h2>3. \u5206\u6790SLS\u65e5\u5fd7\u5b9a\u4f4d\u4e1a\u52a1\u5f02\u5e38<\/h2>\n<p>\u53ef\u89c2\u6d4b\u94fe\u8defOpenTelemetry\u7248\u7684\u65e5\u5fd7\u5206\u6790\u529f\u80fd\u4f9d\u8d56\u65e5\u5fd7\u670d\u52a1\uff08SLS\uff09\uff0c\u53ef\u4ee5\u5728\u63a7\u5236\u53f0\u76f4\u63a5\u5c55\u793a\u65e5\u5fd7\u4e2d\u5fc3\u9875\u9762\u3002\u5f53\u5e94\u7528\u51fa\u73b0\u4e1a\u52a1\u5f02\u5e38\u95ee\u9898\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u67e5\u770b\u548c\u5206\u6790\u65e5\u5fd7\uff0c\u7cbe\u51c6\u5b9a\u4f4d\u5f02\u5e38\u3002<\/p>\n<h2>\u524d\u63d0\u6761\u4ef6<\/h2>\n<li>\u5df2\u63a5\u5165\u53ef\u89c2\u6d4b\u94fe\u8defOpenTelemetry\u7248\u3002<\/li>\n<li>\u5df2\u5f00\u901a\u65e5\u5fd7\u670d\u52a1SLS\u3002<\/li>\n<li>\u5df2\u521b\u5efaProject\u548cLogstore\u3002<\/li>\n<li>\u5df2\u7ecf\u5c06\u5e94\u7528\u7684\u4e1a\u52a1\u65e5\u5fd7\u4e0a\u62a5\u5230\u65e5\u5fd7\u670d\u52a1\u3002<\/li>\n<h2>\u6b65\u9aa4\u4e00\uff1a\u5173\u8054\u4e1a\u52a1\u65e5\u5fd7<\/h2>\n<p>1. <strong>\u767b\u5f55\u53ef\u89c2\u6d4b\u94fe\u8defOpenTelemetry\u7248\u63a7\u5236\u53f0\uff0c\u5728\u5de6\u4fa7\u5bfc\u822a\u680f\u5355\u51fb\u5e94\u7528\u5217\u8868<\/strong>\u3002<\/p>\n<p>2. <strong>\u5728\u5e94\u7528\u5217\u8868\u9875\u9762\u9876\u90e8\u9009\u62e9\u76ee\u6807\u5730\u57df\uff0c\u7136\u540e\u5355\u51fb\u76ee\u6807\u5e94\u7528\u540d\u79f0<\/strong>\u3002<\/p>\n<p>3. <strong>\u5728\u5de6\u4fa7\u5bfc\u822a\u680f\u5355\u51fb\u5e94\u7528\u8bbe\u7f6e<\/strong>\u3002<\/p>\n<p>4. <strong>\u5728\u81ea\u5b9a\u4e49\u914d\u7f6e\u9875\u7b7e\u7684\u4e1a\u52a1\u65e5\u5fd7\u5173\u8054\u8bbe\u7f6e\u533a\u57df\uff0c\u7ed1\u5b9aProject\u548cLogstore\uff0c\u8bbe\u7f6e\u5173\u8054\u7d22\u5f15<\/strong>\u3002<\/p>\n<p>5. <strong>\u5728\u81ea\u5b9a\u4e49\u914d\u7f6e\u9875\u7b7e\u5de6\u4e0b\u89d2\u5355\u51fb\u4fdd\u5b58<\/strong>\u3002<\/p>\n<h2>\u6b65\u9aa4\u4e8c\uff1a\u67e5\u8be2\u5e76\u5206\u6790\u65e5\u5fd7<\/h2>\n<p>1. <strong>\u5728\u5de6\u4fa7\u5bfc\u822a\u680f\uff0c\u5355\u51fb\u65e5\u5fd7\u5206\u6790<\/strong>\u3002<\/p>\n<p>2. <strong>\u5728\u53f3\u4fa7\u9875\u9762\uff0c\u6267\u884c\u4ee5\u4e0b\u64cd\u4f5c\uff1a<\/strong><\/p>\n<li><strong>\u5728\u641c\u7d22\u6846\u4e2d\u8f93\u5165\u67e5\u8be2\u5206\u6790\u8bed\u53e5<\/strong>\uff1a\u67e5\u8be2\u5206\u6790\u8bed\u53e5\u7531\u67e5\u8be2\u8bed\u53e5\u548c\u5206\u6790\u8bed\u53e5\u6784\u6210\uff0c\u683c\u5f0f\u4e3a\u67e5\u8be2\u8bed\u53e5|\u5206\u6790\u8bed\u53e5\uff0c\u67e5\u8be2\u5206\u6790\u8bed\u53e5\u8bed\u6cd5\u8bf7\u53c2\u89c1\u67e5\u8be2\u8bed\u6cd5\u3001SQL\u5206\u6790\u8bed\u6cd5\u3002<\/li>\n<li><strong>\u8bbe\u7f6e\u67e5\u8be2\u5206\u6790\u7684\u65f6\u95f4\u8303\u56f4<\/strong>\uff1a\u53ef\u4ee5\u8bbe\u7f6e\u76f8\u5bf9\u65f6\u95f4\u3001\u6574\u70b9\u65f6\u95f4\u548c\u81ea\u5b9a\u4e49\u65f6\u95f4\u3002<\/li>\n<li><strong>\u5355\u51fb\u67e5\u8be2\/\u5206\u6790\uff0c\u67e5\u770b\u67e5\u8be2\u5206\u6790\u7ed3\u679c<\/strong>\u3002<\/li>\n<p>OpenTelemetry\u63d0\u4f9b\u4e86\u5f3a\u5927\u800c\u7075\u6d3b\u7684\u6570\u636e\u9519\u8bef\u5904\u7406\u548c\u5f02\u5e38\u7ba1\u7406\u673a\u5236\u3002\u901a\u8fc7\u6807\u51c6\u5316\u7684API\u548c\u4e30\u5bcc\u7684\u5de5\u5177\u96c6\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u65b9\u4fbf\u5730\u5728\u5206\u5e03\u5f0f\u7cfb\u7edf\u4e2d\u5b9e\u73b0\u5168\u9762\u7684\u9519\u8bef\u8ffd\u8e2a\u3001\u8be6\u7ec6\u7684\u5f02\u5e38\u5206\u6790\u4ee5\u53ca\u9ad8\u6548\u7684\u95ee\u9898\u89e3\u51b3\u3002\u65e0\u8bba\u662f\u5728\u5f00\u53d1\u65b0\u5e94\u7528\u8fd8\u662f\u4f18\u5316\u73b0\u6709\u7cfb\u7edf\uff0cOpenTelemetry\u90fd\u80fd\u5e2e\u52a9\u63d0\u5347\u7cfb\u7edf\u7684\u53ef\u9760\u6027\u548c\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u9519\u8bef\u8bb0\u5f55\u57fa\u7840 OpenTelemetry\u901a\u8fc7\u4e24\u79cd\u4e3b\u8981\u65b9\u5f0f\u5904\u7406\u9519\u8bef\uff1a Span\u72b6\u6001\u6807\u8bb0\uff1a\u5c06Span\u6807\u8bb0\u4e3a\u9519\u8bef\u72b6\u6001\u3002 \u4e8b\u4ef6\u8bb0\u5f55\uff1a\u5728Span\u4e2d\u6dfb\u52a0\u9519\u8bef\u4e8b\u4ef6\u8be6\u60c5\u3002 \u57fa\u672c\u9519\u8bef\u6807\u8bb0\u793a\u4f8b javascript const { trace } = require(&#8216;@opentelemetry\/api&#8217;); function riskyOperation { const tracer = trace.getTracer(&#8216;error-demo&#8217;); const span = tracer.startSpan(&#8216;riskyOperation&#8217;); try { throw new Error(&#8216;Database connection failed&#8217;); } catch (err) { span.setStatus({ code: trace.StatusCode.ERROR, message: err.message }); span.recordException(err); throw err; } finally { span.end; \u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c`riskyOperation` \u51fd\u6570\u5185\u90e8\u7684 `try-catch` \u5757\u6355\u83b7\u4e86\u4e00\u4e2a\u9519\u8bef\u3002\u4e00\u65e6\u6355\u83b7\u5230\u9519\u8bef\uff0c\u901a\u8fc7 `span.setStatus` \u5c06Span\u6807\u8bb0\u4e3a\u9519\u8bef\u72b6\u6001\uff0c\u5e76\u4f7f\u7528 `span.recordException` [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-38319","post","type-post","status-publish","format-standard","hentry","category-discussion"],"_links":{"self":[{"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/posts\/38319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/comments?post=38319"}],"version-history":[{"count":1,"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/posts\/38319\/revisions"}],"predecessor-version":[{"id":38320,"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/posts\/38319\/revisions\/38320"}],"wp:attachment":[{"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/media?parent=38319"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/categories?post=38319"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.yunshan.net\/blog\/wp-json\/wp\/v2\/tags?post=38319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}