Skip to content

Commit 5a95f96

Browse files
committed
chore: add additional fields to license telemetry
1 parent dba0dfa commit 5a95f96

File tree

6 files changed

+34
-4
lines changed

6 files changed

+34
-4
lines changed

cli/server.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -788,6 +788,17 @@ func (r *RootCmd) Server(newAPI func(context.Context, *coderd.Options) (*coderd.
788788
Prometheus: vals.Prometheus.Enable.Value(),
789789
STUN: len(vals.DERP.Server.STUNAddresses) != 0,
790790
Tunnel: tunnel != nil,
791+
ParseLicenseJWT: func(lic *telemetry.License) error {
792+
email, trial, err := options.ParseLicenseClaims(lic.JWT)
793+
if err != nil {
794+
return err
795+
}
796+
if email != "" {
797+
lic.Email = &email
798+
}
799+
lic.Trial = &trial
800+
return nil
801+
},
791802
})
792803
if err != nil {
793804
return xerrors.Errorf("create telemetry reporter: %w", err)

coderd/coderd.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ type Options struct {
172172
StatsBatcher *batchstats.Batcher
173173

174174
WorkspaceAppsStatsCollectorOptions workspaceapps.StatsCollectorOptions
175+
176+
ParseLicenseClaims func(rawJWT string) (email string, trial bool, err error)
175177
}
176178

177179
// @title Coder API

coderd/httpmw/workspaceagentparam_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ import (
66
"net/http/httptest"
77
"testing"
88

9-
"cdr.dev/slog"
109
"github.com/go-chi/chi/v5"
1110
"github.com/google/uuid"
1211
"github.com/stretchr/testify/require"
1312
"golang.org/x/xerrors"
1413

14+
"cdr.dev/slog"
1515
"github.com/coder/coder/v2/coderd/coderdtest"
1616
"github.com/coder/coder/v2/coderd/database"
1717
"github.com/coder/coder/v2/coderd/database/dbauthz"

coderd/telemetry/telemetry.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ type Options struct {
5252
STUN bool
5353
SnapshotFrequency time.Duration
5454
Tunnel bool
55+
ParseLicenseJWT func(lic *License) error
5556
}
5657

5758
// New constructs a reporter for telemetry data.
@@ -446,7 +447,13 @@ func (r *remoteReporter) createSnapshot() (*Snapshot, error) {
446447
}
447448
snapshot.Licenses = make([]License, 0, len(licenses))
448449
for _, license := range licenses {
449-
snapshot.Licenses = append(snapshot.Licenses, ConvertLicense(license))
450+
tl := ConvertLicense(license)
451+
if r.options.ParseLicenseJWT != nil {
452+
if err := r.options.ParseLicenseJWT(&tl); err != nil {
453+
r.options.Logger.Warn(ctx, "parse license JWT", slog.Error(err))
454+
}
455+
}
456+
snapshot.Licenses = append(snapshot.Licenses, tl)
450457
}
451458
return nil
452459
})
@@ -904,6 +911,10 @@ type License struct {
904911
UploadedAt time.Time `json:"uploaded_at"`
905912
Exp time.Time `json:"exp"`
906913
UUID uuid.UUID `json:"uuid"`
914+
// These two fields are set by decoding the JWT. If the signing keys aren't
915+
// passed in, these will always be nil.
916+
Email *string `json:"email"`
917+
Trial *bool `json:"trial"`
907918
}
908919

909920
type WorkspaceProxy struct {

enterprise/coderd/coderd.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ func New(ctx context.Context, options *Options) (_ *API, err error) {
109109
}
110110
}()
111111

112+
api.AGPL.Options.ParseLicenseClaims = func(rawJWT string) (email string, trial bool, err error) {
113+
c, err := license.ParseClaims(rawJWT, Keys)
114+
if err != nil {
115+
return "", false, err
116+
}
117+
return c.Subject, c.Trial, nil
118+
}
112119
api.AGPL.Options.SetUserGroups = api.setUserGroups
113120
api.AGPL.Options.SetUserSiteRoles = api.setUserSiteRoles
114121
api.AGPL.SiteHandler.AppearanceFetcher = api.fetchAppearanceConfig

enterprise/trialer/trialer.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ import (
99
"net/http"
1010
"time"
1111

12-
"golang.org/x/xerrors"
13-
1412
"github.com/google/uuid"
13+
"golang.org/x/xerrors"
1514

1615
"github.com/coder/coder/v2/coderd/database"
1716
"github.com/coder/coder/v2/coderd/database/dbtime"

0 commit comments

Comments
 (0)