github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/env_config.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2020
     3  
     4  package instana
     5  
     6  import (
     7  	"fmt"
     8  	"strconv"
     9  	"strings"
    10  	"time"
    11  )
    12  
    13  // parseInstanaTags parses the tags string passed via INSTANA_TAGS.
    14  // The tag string is a comma-separated list of keys optionally followed by an '=' character and a string value:
    15  //
    16  //     INSTANA_TAGS := key1[=value1][,key2[=value2],...]
    17  //
    18  // The leading and trailing space is truncated from key names, values are used as-is. If a key does not have
    19  // value associated, it's considered to be nil.
    20  func parseInstanaTags(s string) map[string]interface{} {
    21  	tags := make(map[string]interface{})
    22  
    23  	for _, tag := range strings.Split(s, ",") {
    24  		kv := strings.SplitN(tag, "=", 2)
    25  
    26  		k := strings.TrimSpace(kv[0])
    27  		if k == "" {
    28  			continue
    29  		}
    30  
    31  		var v interface{}
    32  		if len(kv) > 1 {
    33  			v = kv[1]
    34  		}
    35  
    36  		tags[k] = v
    37  	}
    38  
    39  	if len(tags) == 0 {
    40  		return nil
    41  	}
    42  
    43  	return tags
    44  }
    45  
    46  // parseInstanaSecrets parses the tags string passed via INSTANA_SECRETS.
    47  // The secrets matcher configuration string is expected to have the following format:
    48  //
    49  //     INSTANA_SECRETS := <matcher>:<secret>[,<secret>]
    50  //
    51  // Where `matcher` is one of:
    52  // * `equals` - matches a string if it's contained in the secrets list
    53  // * `equals-ignore-case` is a case-insensitive version of `equals`
    54  // * `contains` matches a string if it contains any of the secrets list values
    55  // * `contains-ignore-case` is a case-insensitive version of `contains`
    56  // * `regex` matches a string if it fully matches any of the regular expressions provided in the secrets list
    57  //
    58  // This function returns DefaultSecretsMatcher() if there is no matcher configuration provided.
    59  func parseInstanaSecrets(s string) (Matcher, error) {
    60  	if s == "" {
    61  		return DefaultSecretsMatcher(), nil
    62  	}
    63  
    64  	ind := strings.Index(s, ":")
    65  	if ind < 0 {
    66  		return nil, fmt.Errorf("malformed secret matcher configuration: %q", s)
    67  	}
    68  
    69  	matcher, config := strings.TrimSpace(s[:ind]), strings.Split(s[ind+1:], ",")
    70  
    71  	return NamedMatcher(matcher, config)
    72  }
    73  
    74  // parseInstanaExtraHTTPHeaders parses the tags string passed via INSTANA_EXTRA_HTTP_HEADERS.
    75  // The header names are expected to come in a semicolon-separated list:
    76  //
    77  //     INSTANA_EXTRA_HTTP_HEADERS := header1[;header2;...]
    78  //
    79  // Any leading and trailing whitespace characters will be trimmed from header names.
    80  func parseInstanaExtraHTTPHeaders(s string) []string {
    81  	var headers []string
    82  	for _, h := range strings.Split(s, ";") {
    83  		h = strings.TrimSpace(h)
    84  		if h == "" {
    85  			continue
    86  		}
    87  
    88  		headers = append(headers, h)
    89  	}
    90  
    91  	return headers
    92  }
    93  
    94  // parseInstanaTimeout parses the Instana backend connection timeout passed via INSTANA_TIMEOUT.
    95  // The value is expected to be an integer number of milliseconds, greate than 0.
    96  // This function returns the default timeout 500ms if provided with an empty string.
    97  func parseInstanaTimeout(s string) (time.Duration, error) {
    98  	if s == "" {
    99  		return defaultServerlessTimeout, nil
   100  	}
   101  
   102  	ms, err := strconv.ParseUint(s, 10, 64)
   103  	if err != nil || ms < 1 {
   104  		return 0, fmt.Errorf("invalid timeout value: %q", s)
   105  	}
   106  
   107  	return time.Duration(ms) * time.Millisecond, nil
   108  }