...

Source file src/go.mongodb.org/mongo-driver/internal/driverutil/hello.go

Documentation: go.mongodb.org/mongo-driver/internal/driverutil

     1  // Copyright (C) MongoDB, Inc. 2023-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package driverutil
     8  
     9  import (
    10  	"os"
    11  	"strings"
    12  )
    13  
    14  const AwsLambdaPrefix = "AWS_Lambda_"
    15  
    16  const (
    17  	// FaaS environment variable names
    18  
    19  	// EnvVarAWSExecutionEnv is the AWS Execution environment variable.
    20  	EnvVarAWSExecutionEnv = "AWS_EXECUTION_ENV"
    21  	// EnvVarAWSLambdaRuntimeAPI is the AWS Lambda runtime API variable.
    22  	EnvVarAWSLambdaRuntimeAPI = "AWS_LAMBDA_RUNTIME_API"
    23  	// EnvVarFunctionsWorkerRuntime is the functions worker runtime variable.
    24  	EnvVarFunctionsWorkerRuntime = "FUNCTIONS_WORKER_RUNTIME"
    25  	// EnvVarKService is the K Service variable.
    26  	EnvVarKService = "K_SERVICE"
    27  	// EnvVarFunctionName is the function name variable.
    28  	EnvVarFunctionName = "FUNCTION_NAME"
    29  	// EnvVarVercel is the Vercel variable.
    30  	EnvVarVercel = "VERCEL"
    31  	// EnvVarK8s is the K8s variable.
    32  	EnvVarK8s = "KUBERNETES_SERVICE_HOST"
    33  )
    34  
    35  const (
    36  	// FaaS environment variable names
    37  
    38  	// EnvVarAWSRegion is the AWS region variable.
    39  	EnvVarAWSRegion = "AWS_REGION"
    40  	// EnvVarAWSLambdaFunctionMemorySize is the AWS Lambda function memory size variable.
    41  	EnvVarAWSLambdaFunctionMemorySize = "AWS_LAMBDA_FUNCTION_MEMORY_SIZE"
    42  	// EnvVarFunctionMemoryMB is the function memory in megabytes variable.
    43  	EnvVarFunctionMemoryMB = "FUNCTION_MEMORY_MB"
    44  	// EnvVarFunctionTimeoutSec is the function timeout in seconds variable.
    45  	EnvVarFunctionTimeoutSec = "FUNCTION_TIMEOUT_SEC"
    46  	// EnvVarFunctionRegion is the function region variable.
    47  	EnvVarFunctionRegion = "FUNCTION_REGION"
    48  	// EnvVarVercelRegion is the Vercel region variable.
    49  	EnvVarVercelRegion = "VERCEL_REGION"
    50  )
    51  
    52  const (
    53  	// FaaS environment names used by the client
    54  
    55  	// EnvNameAWSLambda is the AWS Lambda environment name.
    56  	EnvNameAWSLambda = "aws.lambda"
    57  	// EnvNameAzureFunc is the Azure Function environment name.
    58  	EnvNameAzureFunc = "azure.func"
    59  	// EnvNameGCPFunc is the Google Cloud Function environment name.
    60  	EnvNameGCPFunc = "gcp.func"
    61  	// EnvNameVercel is the Vercel environment name.
    62  	EnvNameVercel = "vercel"
    63  )
    64  
    65  // GetFaasEnvName parses the FaaS environment variable name and returns the
    66  // corresponding name used by the client. If none of the variables or variables
    67  // for multiple names are populated the client.env value MUST be entirely
    68  // omitted. When variables for multiple "client.env.name" values are present,
    69  // "vercel" takes precedence over "aws.lambda"; any other combination MUST cause
    70  // "client.env" to be entirely omitted.
    71  func GetFaasEnvName() string {
    72  	envVars := []string{
    73  		EnvVarAWSExecutionEnv,
    74  		EnvVarAWSLambdaRuntimeAPI,
    75  		EnvVarFunctionsWorkerRuntime,
    76  		EnvVarKService,
    77  		EnvVarFunctionName,
    78  		EnvVarVercel,
    79  	}
    80  
    81  	// If none of the variables are populated the client.env value MUST be
    82  	// entirely omitted.
    83  	names := make(map[string]struct{})
    84  
    85  	for _, envVar := range envVars {
    86  		val := os.Getenv(envVar)
    87  		if val == "" {
    88  			continue
    89  		}
    90  
    91  		var name string
    92  
    93  		switch envVar {
    94  		case EnvVarAWSExecutionEnv:
    95  			if !strings.HasPrefix(val, AwsLambdaPrefix) {
    96  				continue
    97  			}
    98  
    99  			name = EnvNameAWSLambda
   100  		case EnvVarAWSLambdaRuntimeAPI:
   101  			name = EnvNameAWSLambda
   102  		case EnvVarFunctionsWorkerRuntime:
   103  			name = EnvNameAzureFunc
   104  		case EnvVarKService, EnvVarFunctionName:
   105  			name = EnvNameGCPFunc
   106  		case EnvVarVercel:
   107  			// "vercel" takes precedence over "aws.lambda".
   108  			delete(names, EnvNameAWSLambda)
   109  
   110  			name = EnvNameVercel
   111  		}
   112  
   113  		names[name] = struct{}{}
   114  		if len(names) > 1 {
   115  			// If multiple names are populated the client.env value
   116  			// MUST be entirely omitted.
   117  			names = nil
   118  
   119  			break
   120  		}
   121  	}
   122  
   123  	for name := range names {
   124  		return name
   125  	}
   126  
   127  	return ""
   128  }
   129  

View as plain text