|
| 1 | +// Copyright 2015 The Cockroach Authors. |
| 2 | +// |
| 3 | +// Use of this software is governed by the CockroachDB Software License |
| 4 | +// included in the /LICENSE file. |
| 5 | + |
| 6 | +package profiler |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + |
| 11 | + gcprofiler "cloud.google.com/go/profiler" |
| 12 | + "github.com/cockroachdb/cockroach/pkg/build" |
| 13 | + "github.com/cockroachdb/cockroach/pkg/util/cloudinfo" |
| 14 | + "github.com/cockroachdb/cockroach/pkg/util/log" |
| 15 | +) |
| 16 | + |
| 17 | +// InitGoogleProfiler initializes the Google Cloud Profiler if enabled via |
| 18 | +// environment variable, and cluster is running on GCP. |
| 19 | +// |
| 20 | +// The profiler initialization should be done as early as possible in the |
| 21 | +// server startup process for best results. |
| 22 | +func InitGoogleProfiler(ctx context.Context) { |
| 23 | + // Detect cloud provider as profiler is only supported on GCP. |
| 24 | + provider, _ := cloudinfo.GetInstanceRegion(ctx) |
| 25 | + if provider != "gcp" { |
| 26 | + log.Infof(ctx, "Google Cloud Profiler disabled (detected cloud: %s)", provider) |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + cfg := gcprofiler.Config{ |
| 31 | + Service: "cockroachdb", |
| 32 | + ServiceVersion: build.BinaryVersion(), |
| 33 | + } |
| 34 | + if err := gcprofiler.Start(cfg); err != nil { |
| 35 | + log.Warningf(ctx, "failed to start google profiler: %v", err) |
| 36 | + } else { |
| 37 | + log.Infof(ctx, "Google Cloud Profiler started successfully on %s", provider) |
| 38 | + } |
| 39 | +} |
0 commit comments