github.com/Rookout/GoSDK@v0.1.48/singleton.go (about)

     1  package rookout
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	"github.com/Rookout/GoSDK/pkg"
     8  	"github.com/Rookout/GoSDK/pkg/information"
     9  	"github.com/Rookout/GoSDK/pkg/logger"
    10  	"github.com/Rookout/GoSDK/pkg/rookoutErrors"
    11  )
    12  
    13  //go:generate go generate ./assembler/
    14  //go:generate go generate ./trampoline/
    15  
    16  func memberToString(memberName string, member interface{}) string {
    17  	if member != nil {
    18  		if member == "" {
    19  			return fmt.Sprintf("%s:'' ,", memberName)
    20  		}
    21  		return fmt.Sprintf("%s:%v ,", memberName, member)
    22  	}
    23  	return ""
    24  }
    25  
    26  func printOptions(opts *RookOptions) {
    27  	censoredToken := ""
    28  	if len(opts.Token) > 5 {
    29  		censoredToken = opts.Token[:5]
    30  	}
    31  
    32  	s := "RookOptions: " +
    33  		memberToString("token", censoredToken) +
    34  		memberToString("host", opts.Host) +
    35  		memberToString("port", opts.Port) +
    36  		memberToString("proxy", opts.Proxy) +
    37  		memberToString("log_level", opts.LogLevel) +
    38  		memberToString("log_to_stderr", opts.LogToStderr) +
    39  		memberToString("log_file", opts.LogFile) +
    40  		memberToString("git_commit", opts.GitCommit) +
    41  		memberToString("git_origin", opts.GitOrigin) +
    42  		memberToString("git_sources", opts.GitSources) +
    43  		memberToString("live_tail", opts.LiveTail) +
    44  		memberToString("labels", opts.Labels)
    45  
    46  	println(s)
    47  }
    48  
    49  func start(opts RookOptions) {
    50  	pkg.InitSingleton()
    51  	obj := pkg.GetSingleton()
    52  
    53  	err := obj.Start(&opts)
    54  	if opts.Debug {
    55  		logger.Logger().Debug("Rookout SDK for Go, Version: " + information.VERSION)
    56  		printOptions(&opts)
    57  	}
    58  	if err != nil {
    59  		logger.Logger().WithError(err).Errorln("Failed to start rook")
    60  		if rookErr, ok := err.(rookoutErrors.RookoutError); ok {
    61  			switch {
    62  			case isErrorType(rookErr, rookoutErrors.NewRookInvalidOptions("")),
    63  				isErrorType(rookErr, rookoutErrors.NewInvalidTokenError()),
    64  				isErrorType(rookErr, rookoutErrors.NewRookMissingToken()),
    65  				isErrorType(rookErr, rookoutErrors.NewInvalidLabelError("")),
    66  				isErrorType(rookErr, rookoutErrors.NewWebSocketError()):
    67  				_, _ = fmt.Fprintf(os.Stderr, "[Rookout] Failed to start rookout: %v\n", err)
    68  			case isErrorType(rookErr, rookoutErrors.NewFailedToDecode(nil, nil)),
    69  				isErrorType(rookErr, rookoutErrors.NewUnexpectedInstruction(nil, nil)):
    70  				_, _ = fmt.Fprintf(os.Stderr, "[Rookout] Failed to start rookout: error while starting instrumentation\n")
    71  			default:
    72  				_, _ = fmt.Fprintf(os.Stderr, "[Rookout] Failed to connect to the controller - will continue attempting in the background: %v\n", err)
    73  			}
    74  		} else {
    75  			_, _ = fmt.Fprintf(os.Stderr, "[Rookout] Failed to start rookout: %v\n", err)
    76  		}
    77  	}
    78  }
    79  
    80  func isErrorType(err rookoutErrors.RookoutError, errType rookoutErrors.RookoutError) bool {
    81  	return err.GetType() == errType.GetType()
    82  }
    83  
    84  func stop() {
    85  	obj := pkg.GetSingleton()
    86  	obj.Stop()
    87  }
    88  
    89  func flush() {
    90  	obj := pkg.GetSingleton()
    91  	obj.Flush()
    92  }