github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/agent/clib/clib.go (about) 1 //go:build clib 2 // +build clib 3 4 // Package main deals with ruby / python / php integrations 5 package main 6 7 import ( 8 "os" 9 "sync" 10 "time" 11 12 "C" 13 14 "github.com/pyroscope-io/pyroscope/pkg/agent" 15 "github.com/pyroscope-io/pyroscope/pkg/agent/spy" 16 "github.com/pyroscope-io/pyroscope/pkg/agent/upstream/remote" 17 "github.com/pyroscope-io/pyroscope/pkg/build" 18 //"github.com/pyroscope-io/pyroscope/pkg/agent/pyspy" 19 ) 20 21 var sessionMutex sync.Mutex 22 var session *agent.ProfileSession 23 24 //export Start 25 func Start(applicationName *C.char, spyName *C.char, serverAddress *C.char, authToken *C.char, sampleRate C.int, withSubprocesses C.int, logLevel *C.char) int { 26 sessionMutex.Lock() 27 defer sessionMutex.Unlock() 28 29 pyspy.Blocking = false 30 31 if err := performOSChecks(); pyspy.Blocking && err != nil { 32 logger.Errorf("error happened when starting profiling session: %v", err) 33 return -1 34 } 35 36 address := C.GoString(serverAddress) 37 // Override the address to use when the environment variable is defined. 38 // This is useful to support adhoc push ingestion. 39 if addr, ok := os.LookupEnv("PYROSCOPE_ADHOC_SERVER_ADDRESS"); ok { 40 address = addr 41 } 42 43 rc := remote.RemoteConfig{ 44 AuthToken: C.GoString(authToken), 45 UpstreamAddress: address, 46 UpstreamThreads: 4, 47 UpstreamRequestTimeout: 10 * time.Second, 48 } 49 u, err := remote.New(rc, logger) 50 if err != nil { 51 logger.Errorf("error happened when starting profiling session: %v", err) 52 return -1 53 } 54 55 sc := agent.SessionConfig{ 56 Upstream: u, 57 AppName: C.GoString(applicationName), 58 ProfilingTypes: []spy.ProfileType{spy.ProfileCPU}, 59 SpyName: C.GoString(spyName), 60 SampleRate: uint32(sampleRate), 61 UploadRate: 10 * time.Second, 62 Pid: os.Getpid(), 63 WithSubprocesses: withSubprocesses != 0, 64 ClibIntegration: true, 65 Logger: logger, 66 } 67 session, err = agent.NewSession(sc) 68 if err != nil { 69 logger.Errorf("error happened when starting profiling session: %v", err) 70 return -1 71 } 72 u.Start() 73 if err = session.Start(); err != nil { 74 logger.Errorf("error happened when starting profiling session: %v", err) 75 return -1 76 } 77 78 return 0 79 } 80 81 //export Stop 82 func Stop() int { 83 sessionMutex.Lock() 84 defer sessionMutex.Unlock() 85 86 session.Stop() 87 return 0 88 } 89 90 //export ChangeName 91 func ChangeName(newName *C.char) int { 92 sessionMutex.Lock() 93 defer sessionMutex.Unlock() 94 95 session.ChangeName(C.GoString(newName)) 96 return 0 97 } 98 99 //export SetTag 100 func SetTag(key *C.char, value *C.char) int { 101 sessionMutex.Lock() 102 defer sessionMutex.Unlock() 103 104 session.SetTag(C.GoString(key), C.GoString(value)) 105 return 0 106 } 107 108 //export BuildSummary 109 func BuildSummary() *C.char { 110 return C.CString(build.Summary()) 111 } 112 113 func main() { 114 }