diff --git a/packages/grafana-data/src/types/config.ts b/packages/grafana-data/src/types/config.ts index b1a02c0ce95..bb003374d92 100644 --- a/packages/grafana-data/src/types/config.ts +++ b/packages/grafana-data/src/types/config.ts @@ -93,6 +93,7 @@ export type OAuthSettings = Partial { + it('should return the external user ID (gcom ID) if available', () => { + const id = getUserIdentifier(gcomUser); + expect(id).toBe('abc-123'); + }); + + it('should fall back to the email address', () => { + const id = getUserIdentifier(baseUser); + expect(id).toBe('email@example.com'); + }); +}); diff --git a/public/app/core/services/echo/utils.ts b/public/app/core/services/echo/utils.ts index 1765b0017fe..edcfe096d77 100644 --- a/public/app/core/services/echo/utils.ts +++ b/public/app/core/services/echo/utils.ts @@ -1,5 +1,20 @@ import { attachDebugger, createLogger } from '@grafana/ui'; +import { CurrentUserDTO } from '@grafana/data'; + +/** + * Returns an opaque identifier for a user, for reporting purposes. + * Because this is for use when reporting across multiple Grafana installations + * It cannot simply be user.id because that's not unique across two installations. + */ +export function getUserIdentifier(user: CurrentUserDTO) { + if (user.externalUserId.length) { + return user.externalUserId; + } + + return user.email; +} + /** @internal */ export const echoLogger = createLogger('EchoSrv'); export const echoLog = echoLogger.logger;