github.com/Rookout/GoSDK@v0.1.48/pkg/information/serverless.go (about)

     1  package information
     2  
     3  import (
     4  	"os"
     5  )
     6  
     7  func isAWSLambda() bool {
     8  	if _, ok := os.LookupEnv("AWS_LAMBDA_FUNCTION_NAME"); !ok {
     9  		return false
    10  	}
    11  
    12  	return true
    13  }
    14  
    15  func isGoogleCloudFunction() bool {
    16  	if _, ok := os.LookupEnv("FUNCTION_TARGET"); !ok {
    17  		return false
    18  	}
    19  
    20  	if _, ok := os.LookupEnv("FUNCTION_SIGNATURE_TYPE"); !ok {
    21  		return false
    22  	}
    23  
    24  	return true
    25  }
    26  
    27  func isCloudRunOrFireBase() bool {
    28  	if _, ok := os.LookupEnv("K_SERVICE"); !ok {
    29  		return false
    30  	}
    31  
    32  	if _, ok := os.LookupEnv("K_REVISION"); !ok {
    33  		return false
    34  	}
    35  
    36  	if _, ok := os.LookupEnv("K_CONFIGURATION"); !ok {
    37  		return false
    38  	}
    39  
    40  	if _, ok := os.LookupEnv("PORT"); !ok {
    41  		return false
    42  	}
    43  
    44  	return true
    45  }
    46  
    47  func isAzureFunction() bool {
    48  	if _, ok := os.LookupEnv("FUNCTIONS_WORKER_RUNTIME"); !ok {
    49  		return false
    50  	}
    51  
    52  	if _, ok := os.LookupEnv("WEBSITE_SITE_NAME"); !ok {
    53  		return false
    54  	}
    55  
    56  	return true
    57  }
    58  
    59  func addKeyToLabels(key string, value string, info *AgentInformation) {
    60  	if info.Labels == nil {
    61  		info.Labels = make(map[string]string)
    62  	}
    63  
    64  	if value == "" {
    65  		return
    66  	}
    67  
    68  	if _, ok := info.Labels[key]; !ok {
    69  		info.Labels[key] = value
    70  	}
    71  }
    72  
    73  func collectServerless(info *AgentInformation) error {
    74  	var functionName string
    75  
    76  	if isAWSLambda() {
    77  		functionName, _ = os.LookupEnv("AWS_LAMBDA_FUNCTION_NAME")
    78  		awsRegion, _ := os.LookupEnv("AWS_REGION")
    79  		addKeyToLabels("aws_region", awsRegion, info)
    80  	} else if isGoogleCloudFunction() || isCloudRunOrFireBase() {
    81  		var ok bool
    82  		functionName, ok = os.LookupEnv("FUNCTION_NAME")
    83  		if !ok {
    84  			functionName, _ = os.LookupEnv("K_SERVICE")
    85  		}
    86  	} else if isAzureFunction() {
    87  		functionName, _ = os.LookupEnv("WEBSITE_SITE_NAME")
    88  		azureRegion, _ := os.LookupEnv("REGION_NAME")
    89  		addKeyToLabels("azure_region", azureRegion, info)
    90  	}
    91  
    92  	if functionName != "" {
    93  		addKeyToLabels("rookout_serverless", "true", info)
    94  		addKeyToLabels("function_name", functionName, info)
    95  	}
    96  
    97  	return nil
    98  }