Merge pull request #591 from mautrix/segment_user_id

Allow setting segment_user_id in config
This commit is contained in:
Scott Weber 2023-01-31 21:48:44 -05:00 committed by GitHub
commit ff6531a5e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 2 deletions

View file

@ -24,7 +24,8 @@ import (
type Config struct {
*bridgeconfig.BaseConfig `yaml:",inline"`
SegmentKey string `yaml:"segment_key"`
SegmentKey string `yaml:"segment_key"`
SegmentUserID string `yaml:"segment_user_id"`
Metrics struct {
Enabled bool `yaml:"enabled"`

View file

@ -28,6 +28,7 @@ func DoUpgrade(helper *up.Helper) {
bridgeconfig.Upgrader.DoUpgrade(helper)
helper.Copy(up.Str|up.Null, "segment_key")
helper.Copy(up.Str|up.Null, "segment_user_id")
helper.Copy(up.Bool, "metrics", "enabled")
helper.Copy(up.Str, "metrics", "listen")

View file

@ -72,6 +72,8 @@ appservice:
# Segment API key to track some events, like provisioning API login and encryption errors.
segment_key: null
# Optional user_id to use when sending Segment events. If null, defaults to using mxID.
segment_user_id: null
# Prometheus config.
metrics:

View file

@ -93,8 +93,12 @@ func (br *WABridge) Init() {
Segment.log = br.Log.Sub("Segment")
Segment.key = br.Config.SegmentKey
Segment.userID = br.Config.SegmentUserID
if Segment.IsEnabled() {
Segment.log.Infoln("Segment metrics are enabled")
if Segment.userID != "" {
Segment.log.Infoln("Overriding Segment user_id with %v", Segment.userID)
}
}
br.DB = database.New(br.Bridge.DB, br.Log.Sub("Database"))

View file

@ -30,6 +30,7 @@ const SegmentURL = "https://api.segment.io/v1/track"
type SegmentClient struct {
key string
userID string
log log.Logger
client http.Client
}
@ -38,8 +39,14 @@ var Segment SegmentClient
func (sc *SegmentClient) trackSync(userID id.UserID, event string, properties map[string]interface{}) error {
var buf bytes.Buffer
var segmentUserID string
if Segment.userID != "" {
segmentUserID = Segment.userID
} else {
segmentUserID = userID.String()
}
err := json.NewEncoder(&buf).Encode(map[string]interface{}{
"userId": userID,
"userId": segmentUserID,
"event": event,
"properties": properties,
})