github.com/zchee/zap-cloudlogging@v0.0.0-20220819025602-19b026d3900e/pkg/detector/serverless.go (about)

     1  // Copyright 2022 The zap-cloudlogging Authors
     2  // SPDX-License-Identifier: BSD-3-Clause
     3  
     4  package detector
     5  
     6  // List of Cloud Run env vars:
     7  //
     8  // https://cloud.google.com/run/docs/container-contract#env-vars
     9  const (
    10  	// EnvCloudRunService is the name of the Cloud Run service being run.
    11  	EnvCloudRunService = "K_SERVICE"
    12  
    13  	// EnvCloudRunRevision is the name of the Cloud Run revision being run.
    14  	EnvCloudRunRevision = "K_REVISION"
    15  
    16  	// EnvCloudRunConfig is	the name of the Cloud Run configuration that created the revision.
    17  	EnvCloudRunConfig = "K_CONFIGURATION"
    18  )
    19  
    20  func (d *Detector) isCloudRun() bool {
    21  	config := d.attrs.EnvVar(EnvCloudRunConfig)
    22  	// note that this envvar is also present in Cloud Function environments
    23  	service := d.attrs.EnvVar(EnvCloudRunService)
    24  	revision := d.attrs.EnvVar(EnvCloudRunRevision)
    25  
    26  	return config != "" && service != "" && revision != ""
    27  }
    28  
    29  // List of Cloud Run jobs env vars:
    30  //
    31  // https://cloud.google.com/run/docs/container-contract#jobs-env-vars
    32  const (
    33  	// EnvCloudRunJobsService is the name of the Cloud Run job being run.
    34  	EnvCloudRunJobsService = "CLOUD_RUN_JOB"
    35  
    36  	// EnvCloudRunJobsRevision is the name of the Cloud Run execution being run.
    37  	EnvCloudRunJobsRevision = "CLOUD_RUN_EXECUTION"
    38  
    39  	// EnvCloudRunJobsTaskIndex for each task, this will be set to a unique value between 0 and the number of tasks minus 1.
    40  	EnvCloudRunJobsTaskIndex = "CLOUD_RUN_TASK_INDEX"
    41  
    42  	// cloudRunJobsTaskAttempt is the number of times this task has been retried.
    43  	//
    44  	// Starts at 0 for the first attempt; increments by 1 for every successive retry, up to the maximum retries value.
    45  	EnvCloudRunJobsTaskAttempt = "CLOUD_RUN_TASK_ATTEMPT"
    46  
    47  	// cloudRunJobsRevisionEnv is the number of tasks defined in the --tasks parameter.
    48  	EnvCloudRunJobsTaskCount = "CLOUD_RUN_TASK_COUNT"
    49  )
    50  
    51  func (d *Detector) isCloudRunJobs() bool {
    52  	service := d.attrs.EnvVar(EnvCloudRunJobsService)
    53  	revision := d.attrs.EnvVar(EnvCloudRunJobsRevision)
    54  
    55  	return service != "" && revision != ""
    56  }
    57  
    58  // List of Cloud Functions newer runtimes env vars:
    59  //
    60  // https://cloud.google.com/functions/docs/configuring/env-var#newer_runtimes
    61  const (
    62  	// EnvCloudFunctionsTarget is the function to be executed.
    63  	EnvCloudFunctionsTarget = "FUNCTION_TARGET"
    64  
    65  	// EnvCloudFunctionsSignatureType is the type of the function: http for HTTP functions, and event for event-driven functions.
    66  	EnvCloudFunctionsSignatureType = "FUNCTION_SIGNATURE_TYPE"
    67  
    68  	// EnvCloudFunctionsKService is the name of the function resource.
    69  	//
    70  	// Note that this envvar is also present in Cloud Run environments.
    71  	EnvCloudFunctionsKService = "K_SERVICE"
    72  
    73  	// EnvCloudFunctionsKRevision is the version identifier of the function.
    74  	//
    75  	// Note that this envvar is also present in Cloud Run environments.
    76  	EnvCloudFunctionsKRevision = "K_REVISION"
    77  )
    78  
    79  func (d *Detector) isCloudFunctions() bool {
    80  	target := d.attrs.EnvVar(EnvCloudFunctionsTarget)
    81  	signatureType := d.attrs.EnvVar(EnvCloudFunctionsSignatureType)
    82  	service := d.attrs.EnvVar(EnvCloudFunctionsKService)
    83  	revision := d.attrs.EnvVar(EnvCloudFunctionsKRevision)
    84  
    85  	return target != "" && signatureType != "" && service != "" && revision != ""
    86  }