...

Source file src/github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gcp/profiler/profiler.go

Documentation: github.com/GoogleCloudPlatform/k8s-config-connector/pkg/gcp/profiler

     1  // Copyright 2022 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package profiler
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"cloud.google.com/go/profiler"
    21  	flag "github.com/spf13/pflag"
    22  )
    23  
    24  var profilerServiceName string
    25  
    26  func AddFlag(flagSet *flag.FlagSet) {
    27  	flagSet.StringVar(&profilerServiceName, "profiler-service-name", "", "when specified, the profiler agent is enabled with a service name equal to the value. See https://cloud.google.com/profiler/docs/profiling-go#gke for details.")
    28  }
    29  
    30  // Starts the Cloud Profiler agent if the 'profile' option is true
    31  func StartIfEnabled() error {
    32  	if profilerServiceName == "" {
    33  		return nil
    34  	}
    35  	return start()
    36  }
    37  
    38  // Starts the Cloud Profiler agent, should be called as soon as possible
    39  func start() error {
    40  	cfg := profiler.Config{
    41  		Service:      profilerServiceName,
    42  		DebugLogging: true,
    43  	}
    44  	// Profiler initialization, best done as early as possible.
    45  	if err := profiler.Start(cfg); err != nil {
    46  		return fmt.Errorf("error starting profiler: %v", err)
    47  	}
    48  	return nil
    49  }
    50  

View as plain text