funccomputeSubrequests(r*http.Request,subRequestsint)error{// Add custom span representing the work done for the subrequestsctx,span:=tracer.Start(r.Context(),"subrequests")deferspan.End()// Make specified number of http requests to the /single endpoint.fori:=0;i < subRequests;i++{iferr:=callSingle(ctx);err!=nil{returnerr}}// record number of sub-requests madesubRequestsHistogram.Record(ctx,int64(subRequests))returnnil}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-03 (世界標準時間)。"],[],[],null,["# Correlate metrics and traces by using exemplars\n\nThis document describes *exemplars* , which are example data points attached to\na metric data point. One use of exemplars is to store a link to a trace,\nwhich lets you correlate your metric and trace data. This document also\ndescribes how you can use [OpenTelemetry](https://opentelemetry.io/docs/what-is-opentelemetry/)\nto generate exemplars.\n\nAbout exemplars\n---------------\n\nExemplars are a way to associate arbitrary data with metric data. You can use\nthem to attach non-metric data to measurements.\nOne use of exemplars is to associate trace data with metric data.\nIn Cloud Monitoring, exemplars can be\nattached to metric data that has the [`Distribution`](/monitoring/api/ref_v3/rest/v3/TypedValue#Distribution)\nvalue type. Distribution-valued metrics collect many measurements and group\nthem into buckets.\nEach \"point\" is a histogram, which is an array that lists bucket counts.\nThe actual measurements\naren't retained, only the bucket counts. For more information, see\n[About distribution-valued metrics](/monitoring/charts/charting-distribution-metrics).\n\nWhen a metric data point is written, an array of [`Exemplar`](/monitoring/api/ref_v3/rest/v3/TypedValue#Exemplar)\nobjects can be attached to that data. Exemplars, when used,\nattach useful information to the metric data. For example,\nwhen OpenTelemetry write an exemplar, it attaches a link to the current\n[span of a trace](https://opentelemetry.io/docs/concepts/signals/traces/#spans).\n\nHistogram data is typically displayed by using a heatmap. When a \"point\"\ncontains an exemplar, Cloud Monitoring annotates the heatmap with a dot:\n\n- To view summary information about the metric point, activate the tooltip for the annotation.\n- To access the trace, select the annotation.\n\nFor example, the\n[Go instrumentation sample](/stackdriver/docs/instrumentation/setup/go) uses OpenTelemetry to\nwrites metric data, trace data, and exemplars.\nThe following screenshot shows the value of the\n`prometheus/http_server_duration_milliseconds/histogram` metric after\nseveral executions of the application:\n\nThe previous screenshot shows multiple exemplars. The tooltip for one exemplar\nis expanded, and it shows the date, the trace name, percentiles,\nand a latency value.\n\nWhen the annotation is selected, the trace linked in the exemplar is displayed:\n\nWhen the displayed information isn't sufficiently detailed, you can select\n**View in Trace** , which opens the **Trace Explorer** page.\nOn that page, you can view trace attributes and access links to log entries.\n\nHow to enable OpenTelemetry exemplars\n-------------------------------------\n\nAs the previous example illustrates, you can configure OpenTelemetry to\nwrite exemplars that link the current [span](https://opentelemetry.io/docs/concepts/signals/traces/#spans) of a trace to a\nmetric measurement. However, for exemplars to be written, you must do the\nfollowing:\n\n1. Configure both the OpenTelemetry metric and trace SDKs.\n2. To include exemplars on your custom instrumentation,\n ensure that the metric is recorded within a span,\n and that you pass the context from the span with the metric measurement.\n\n For example, the `computeSubrequests` function follows the previous\n guidance. The function `subRequestsHistogram.Record` is invoked after\n `tracer.Start` and before `span.End`, which is deferred.\n Also notice that the context, `ctx`, is passed from `tracer.Start` to\n the `Record` function: \n\n func computeSubrequests(r *http.Request, subRequests int) error {\n \t// Add custom span representing the work done for the subrequests\n \tctx, span := tracer.Start(r.Context(), \"subrequests\")\n \tdefer span.End()\n\n \t// Make specified number of http requests to the /single endpoint.\n \tfor i := 0; i \u003c subRequests; i++ {\n \t\tif err := callSingle(ctx); err != nil {\n \t\t\treturn err\n \t\t}\n \t}\n \t// record number of sub-requests made\n \tsubRequestsHistogram.Record(ctx, int64(subRequests))\n \treturn nil\n }\n\n For more information about this sample, see\n [Add custom traces and metrics to your app](/stackdriver/docs/instrumentation/advanced-topics/custom-instrumentation).\n\nWhat's next\n-----------\n\n- [OpenTelemetry](https://opentelemetry.io/docs/what-is-opentelemetry/)\n- [OpenTelemetry Exemplars](https://opentelemetry.io/docs/specs/otel/metrics/data-model/#exemplars)\n- [OpenTelemetry Metrics Data Model](https://opentelemetry.io/docs/specs/otel/metrics/data-model/)\n- [OpenTelemetry Histogram](https://opentelemetry.io/docs/specs/otel/metrics/data-model/#histogram)"]]