1818package telemetry
1919
2020import (
21+ "encoding/base64"
22+ "encoding/json"
2123 "github.com/caarlos0/env"
2224 "github.com/patrickmn/go-cache"
2325 "github.com/posthog/posthog-go"
2426 "go.uber.org/zap"
27+ "io/ioutil"
28+ "net/http"
2529 "time"
2630)
2731
@@ -31,11 +35,12 @@ type PosthogClient struct {
3135}
3236
3337type PosthogConfig struct {
34- ApiKey string `env:"API_KEY" envDefault:""`
35- PosthogEndpoint string `env:"POSTHOG_ENDPOINT" envDefault:"https://app.posthog.com"`
36- SummaryInterval int `env:"SUMMARY_INTERVAL" envDefault:"24"`
37- HeartbeatInterval int `env:"HEARTBEAT_INTERVAL" envDefault:"3"`
38- CacheExpiry int `env:"CACHE_EXPIRY" envDefault:"120"`
38+ PosthogApiKey string `env:"POSTHOG_API_KEY" envDefault:""`
39+ PosthogEndpoint string `env:"POSTHOG_ENDPOINT" envDefault:"https://app.posthog.com"`
40+ SummaryInterval int `env:"SUMMARY_INTERVAL" envDefault:"24"`
41+ HeartbeatInterval int `env:"HEARTBEAT_INTERVAL" envDefault:"3"`
42+ CacheExpiry int `env:"CACHE_EXPIRY" envDefault:"120"`
43+ TelemetryApiKeyEndpoint string `env:"TELEMETRY_API_KEY_ENDPOINT" envDefault:"aHR0cHM6Ly90ZWxlbWV0cnkuZGV2dHJvbi5haS9kZXZ0cm9uL3RlbGVtZXRyeS9hcGlrZXk="`
3944}
4045
4146func GetPosthogConfig () (* PosthogConfig , error ) {
@@ -52,9 +57,17 @@ func NewPosthogClient(logger *zap.SugaredLogger) (*PosthogClient, error) {
5257 err := env .Parse (cfg )
5358 if err != nil {
5459 logger .Errorw ("exception caught while parsing posthog config" , "err" , err )
55- return & PosthogClient {}, err
60+ // return &PosthogClient{}, err
5661 }
57- client , _ := posthog .NewWithConfig (cfg .ApiKey , posthog.Config {Endpoint : cfg .PosthogEndpoint })
62+ if len (cfg .PosthogApiKey ) == 0 {
63+ apiKey , err := getPosthogApiKey (cfg .TelemetryApiKeyEndpoint )
64+ if err != nil {
65+ logger .Errorw ("exception caught while getting api key" , "err" , err )
66+ //return &PosthogClient{}, err
67+ }
68+ cfg .PosthogApiKey = apiKey
69+ }
70+ client , _ := posthog .NewWithConfig (cfg .PosthogApiKey , posthog.Config {Endpoint : cfg .PosthogEndpoint })
5871 //defer client.Close()
5972 d := time .Duration (cfg .CacheExpiry )
6073 c := cache .New (d * time .Minute , 240 * time .Minute )
@@ -64,3 +77,39 @@ func NewPosthogClient(logger *zap.SugaredLogger) (*PosthogClient, error) {
6477 }
6578 return pgClient , nil
6679}
80+
81+ func getPosthogApiKey (encodedPosthogApiKeyUrl string ) (string , error ) {
82+ dncodedPosthogApiKeyUrl , err := base64 .StdEncoding .DecodeString (encodedPosthogApiKeyUrl )
83+ if err != nil {
84+ return "" , err
85+ }
86+ apiKeyUrl := string (dncodedPosthogApiKeyUrl )
87+ req , err := http .NewRequest (http .MethodGet , apiKeyUrl , nil )
88+ if err != nil {
89+ return "" , err
90+ }
91+ //var client *http.Client
92+ client := & http.Client {}
93+ res , err := client .Do (req )
94+ if err != nil {
95+ return "" , err
96+ }
97+ if res .StatusCode >= 200 && res .StatusCode <= 299 {
98+ resBody , err := ioutil .ReadAll (res .Body )
99+ if err != nil {
100+ return "" , err
101+ }
102+ var apiRes map [string ]interface {}
103+ err = json .Unmarshal (resBody , & apiRes )
104+ if err != nil {
105+ return "" , err
106+ }
107+ encodedApiKey := apiRes ["result" ].(string )
108+ apiKey , err := base64 .StdEncoding .DecodeString (encodedApiKey )
109+ if err != nil {
110+ return "" , err
111+ }
112+ return string (apiKey ), err
113+ }
114+ return "" , err
115+ }
0 commit comments