github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/analytics/adhoc.go (about) 1 package analytics 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "io" 7 "net/http" 8 "os" 9 "runtime" 10 "sync" 11 "time" 12 13 "github.com/pyroscope-io/pyroscope/pkg/build" 14 "github.com/sirupsen/logrus" 15 ) 16 17 var ( 18 adhocHost = "https://adhoc.analytics.pyroscope.io" 19 adhocPath = "/api/adhoc-events" 20 adhocHTTPClient *http.Client 21 ) 22 23 func init() { 24 adhocHTTPClient = &http.Client{ 25 Transport: &http.Transport{ 26 MaxConnsPerHost: 1, 27 }, 28 Timeout: 10 * time.Second, 29 } 30 } 31 32 type AdhocEvent struct { 33 Version string `json:"version"` 34 GitSHA string `json:"git_sha"` 35 BuildTime string `json:"build_time"` 36 Timestamp time.Time `json:"timestamp"` 37 GOOS string `json:"goos"` 38 GOARCH string `json:"goarch"` 39 GoVersion string `json:"go_version"` 40 41 EventName string `json:"event_name"` 42 } 43 44 func AdhocReport(eventName string, wg *sync.WaitGroup) { 45 defer wg.Done() 46 47 logrus.Debug("sending adhoc analytics report") 48 49 ev := &AdhocEvent{ 50 Version: build.Version, 51 GitSHA: build.GitSHA, 52 BuildTime: build.Time, 53 Timestamp: time.Now(), 54 GOOS: runtime.GOOS, 55 GOARCH: runtime.GOARCH, 56 GoVersion: runtime.Version(), 57 58 EventName: eventName, 59 } 60 61 buf, err := json.Marshal(ev) 62 if err != nil { 63 logrus.WithField("err", err).Debug("Error happened when preparing JSON") 64 return 65 } 66 67 if hostOverride := os.Getenv("PYROSCOPE_ANALYTICS_HOST"); hostOverride != "" { 68 adhocHost = hostOverride 69 } 70 71 adhocURL := adhocHost + adhocPath 72 73 resp, err := adhocHTTPClient.Post(adhocURL, "application/json", bytes.NewReader(buf)) 74 if err != nil { 75 logrus.WithField("err", err).Debug("Error happened when uploading anonymized usage data") 76 } 77 if resp != nil { 78 _, err := io.ReadAll(resp.Body) 79 if err != nil { 80 logrus.WithField("err", err).Debug("Error happened when uploading reading server response") 81 return 82 } 83 } 84 }