gitlab.com/gitlab-org/labkit@v1.21.0/tracing/connstr/connection_string_parser.go (about)

     1  package connstr
     2  
     3  import (
     4  	"fmt"
     5  	"net/url"
     6  	"regexp"
     7  )
     8  
     9  // Connection strings:
    10  // * opentracing://jaeger
    11  // * opentracing://datadog
    12  // * opentracing://lightstep?access_token=12345
    13  
    14  var errInvalidConnection = fmt.Errorf("invalid connection string")
    15  
    16  // Parse parses a opentracing connection string into a driverName and options map.
    17  func Parse(connectionString string) (driverName string, options map[string]string, err error) {
    18  	if connectionString == "" {
    19  		return "", nil, errInvalidConnection
    20  	}
    21  
    22  	URL, err := url.Parse(connectionString)
    23  	if err != nil {
    24  		return "", nil, errInvalidConnection
    25  	}
    26  
    27  	if URL.Scheme != "opentracing" {
    28  		return "", nil, errInvalidConnection
    29  	}
    30  
    31  	driverName = URL.Host
    32  	if driverName == "" {
    33  		return "", nil, errInvalidConnection
    34  	}
    35  
    36  	// Connection strings should not have a path
    37  	if URL.Path != "" {
    38  		return "", nil, errInvalidConnection
    39  	}
    40  
    41  	matched, err := regexp.MatchString("^[a-z0-9_]+$", driverName)
    42  	if err != nil || !matched {
    43  		return "", nil, errInvalidConnection
    44  	}
    45  
    46  	query := URL.Query()
    47  	driverName = URL.Host
    48  	options = make(map[string]string, len(query))
    49  
    50  	for k := range query {
    51  		options[k] = query.Get(k)
    52  	}
    53  
    54  	return driverName, options, nil
    55  }