Open sinmetal opened 7 months ago
Go and OpenTelemetry の通りに設定すれば、otelでtraceを出力することができる。
otelにするとCloud Traceへの送信もspanとして出力されるので、合わせて WithTelemetryDisabled
を設定しておくと良いかもしれない。
https://github.com/open-telemetry/opentelemetry-go/issues/1928#issuecomment-843644237
更に $GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING=opentelemetry
を設定するとCloud Client Libraryもotelで出力するようになる。
OpenCensusからOpenTelemetryに試しに置き換えたProjectで以下のエラーが出ている。 シンプルなTraceの設定ではエラーが出ないので、何か送れない値を設定してのではないかと思っているが、どういう条件で出るエラーかは分かっていない。
failed to export to Google Cloud Trace: rpc error: code = Internal desc = grpc: error while marshaling: string field contains invalid UTF-8
rpc error: code = InvalidArgument desc = One or more TimeSeries could not be written: Points must be written in order. One or more of the points specified had an older start time than the most recent point.: generic_node{node_id:,namespace:,location:global} timeSeries[0-31]: workload.googleapis.com/rpc.client.duration{service_name:xxxxx,rpc_system:grpc,rpc_method:BatchCreateSessions,rpc_service:google.spanner.v1.Spanner,rpc_grpc_status_code:0}
App EngineやCloud RunなどはRequestごとに自動でTraceを出力する
TraceIDはRequestHeaderに X-Cloud-Trace-Context
として入っている。
OpenCensusは自動でこのHeaderを見る機能が入っていたが、OpenTelemetryにはないので、RequestのTraceと紐付かない
httpHandlerで X-Cloud-Trace-Context
を処理するのは gcppropagator.CloudTraceOneWayPropagator{}
をotel.SetTextMapPropagatorに追加する
https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/blob/87124ac8635698cc7cdc9f8f2dfca94a8aa13606/example/trace/http/server/server.go#L61-L70
FYI: otelのpropagatorについて https://zenn.dev/google_cloud_jp/articles/20230626-pubsub-trace
X-Cloud-Trace-Context
を直接取り出したい場合は、Request Headerから取り出せる
以下はloggingのために取り出している例
https://github.com/GoogleCloudPlatform/golang-samples/blob/511a4d6fded945e6599b87e5df7eda7a1f8ca550/run/logging-manual/main.go#L99
https://cloud.google.com/spanner/docs/capture-visualize-latency?hl=en
OpenCensusからOpenTelemetryに変更するとmetricsのNameが変わるので、アラートやダッシュボードは修正が必要かも
Name: OpenCensus/cloud.google.com/go/spanner/max_allowed_sessions
Metric: custom.googleapis.com/opencensus/cloud.google.com/go/spanner/max_allowed_sessions
Name: spanner/max_allowed_sessions
Metric: workload.googleapis.com/spanner/max_allowed_sessions
Cloud Client Library for Goのtraceのコメントに2024/05/29を過ぎたら、OpenTelemetryがdefaultで使われるようになると書いてある。 引き続きOpenCensusを使い続けたい場合は
$GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING=opencensus
を指定するhttps://github.com/googleapis/google-cloud-go/blob/e5d0c2fc2182174b9307363b48c0a0e4056cb3f4/internal/trace/trace.go#L186C1-L197C71
現状、Javaなど他の言語ではotelへの以降は入っていない? https://github.com/googleapis/java-spanner/pull/2593 はcloseされている。