gcpug / nouhau

Google Cloud Platformのノウハウを共有するRepository
https://gcpug.jp
MIT License
655 stars 23 forks source link

Cloud Client Library with OpenTelemetry 調査隊 #150

Open sinmetal opened 7 months ago

sinmetal commented 7 months ago

Cloud Client Library for Goのtraceのコメントに2024/05/29を過ぎたら、OpenTelemetryがdefaultで使われるようになると書いてある。 引き続きOpenCensusを使い続けたい場合は $GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING=opencensus を指定する

// TracePrintf retrieves the current OpenCensus or OpenTelemetry span from context, then:
// * calls Span.Annotatef if OpenCensus is enabled; or
// * calls Span.AddEvent if OpenTelemetry is enabled.
//
// If IsOpenCensusTracingEnabled returns true, the expected span must be an
// OpenCensus span. If IsOpenTelemetryTracingEnabled returns true, the expected
// span must be an OpenTelemetry span. Set the environment variable
// GOOGLE_API_GO_EXPERIMENTAL_TELEMETRY_PLATFORM_TRACING to the case-insensitive
// value "opentelemetry" before loading the package to use OpenTelemetry tracing.
// The default will remain OpenCensus until May 29, 2024, at which time the default will
// switch to "opentelemetry" and explicitly setting the environment variable to
// "opencensus" will be required to continue using OpenCensus tracing.

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されている。

sinmetal commented 7 months ago

OpenTelemetryを使う

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で出力するようになる。

Error

OpenCensusからOpenTelemetryに試しに置き換えたProjectで以下のエラーが出ている。 シンプルなTraceの設定ではエラーが出ないので、何か送れない値を設定してのではないかと思っているが、どういう条件で出るエラーかは分かっていない。

Trace

failed to export to Google Cloud Trace: rpc error: code = Internal desc = grpc: error while marshaling: string field contains invalid UTF-8

Monitoring

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}
sinmetal commented 7 months ago

X-Cloud-Trace-Context

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

sinmetal commented 7 months ago

Spanner Session Pool Metrics

https://cloud.google.com/spanner/docs/capture-visualize-latency?hl=en

OpenCensusからOpenTelemetryに変更するとmetricsのNameが変わるので、アラートやダッシュボードは修正が必要かも

OpenCensus

Name: OpenCensus/cloud.google.com/go/spanner/max_allowed_sessions Metric: custom.googleapis.com/opencensus/cloud.google.com/go/spanner/max_allowed_sessions

OpenTelemetry

Name: spanner/max_allowed_sessions Metric: workload.googleapis.com/spanner/max_allowed_sessions