Skip to content

Commit 733afda

Browse files
FEATURE: telemetry api key integration for oss (#536)
* env key changes for API_KEY to POSTHOG_API_KEY * base64 encoded api key and api key url handling * fix api key url
1 parent 20be7c8 commit 733afda

File tree

2 files changed

+77
-17
lines changed

2 files changed

+77
-17
lines changed

client/telemetry/PosthogClient.go

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
package telemetry
1919

2020
import (
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

3337
type 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

4146
func 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+
}

client/telemetry/TelemetryEventClient.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,17 @@ func (impl *TelemetryEventClientImpl) SummaryEventForTelemetry() {
203203
impl.logger.Errorw("SummaryEventForTelemetry, payload unmarshal error", "error", err)
204204
return
205205
}
206-
impl.PosthogClient.Client.Enqueue(posthog.Capture{
207-
DistinctId: ucid,
208-
Event: Summary.String(),
209-
Properties: prop,
210-
})
206+
207+
if impl.PosthogClient.Client != nil {
208+
err = impl.PosthogClient.Client.Enqueue(posthog.Capture{
209+
DistinctId: ucid,
210+
Event: Summary.String(),
211+
Properties: prop,
212+
})
213+
if err != nil {
214+
impl.logger.Errorw("SummaryEventForTelemetry, failed to push event", "error", err)
215+
}
216+
}
211217
}
212218

213219
func (impl *TelemetryEventClientImpl) HeartbeatEventForTelemetry() {
@@ -239,11 +245,16 @@ func (impl *TelemetryEventClientImpl) HeartbeatEventForTelemetry() {
239245
impl.logger.Errorw("HeartbeatEventForTelemetry, payload unmarshal error", "error", err)
240246
return
241247
}
242-
impl.PosthogClient.Client.Enqueue(posthog.Capture{
243-
DistinctId: ucid,
244-
Event: Heartbeat.String(),
245-
Properties: prop,
246-
})
248+
if impl.PosthogClient.Client != nil {
249+
err = impl.PosthogClient.Client.Enqueue(posthog.Capture{
250+
DistinctId: ucid,
251+
Event: Heartbeat.String(),
252+
Properties: prop,
253+
})
254+
if err != nil {
255+
impl.logger.Errorw("HeartbeatEventForTelemetry, failed to push event", "error", err)
256+
}
257+
}
247258
}
248259

249260
func (impl *TelemetryEventClientImpl) GetClientPlatformIdAndTelemetryUrl() (*PosthogData, error) {

0 commit comments

Comments
 (0)