Skip to content

Commit

Permalink
chore: add localization and adapter names to telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
denolfe committed Apr 29, 2024
1 parent 873e698 commit cff5a25
Show file tree
Hide file tree
Showing 14 changed files with 60 additions and 19 deletions.
1 change: 1 addition & 0 deletions packages/email-nodemailer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export const nodemailerAdapter = async (
const { defaultFromAddress, defaultFromName, transport } = await buildEmail(args)

const adapter: NodemailerAdapter = () => ({
name: 'nodemailer',
defaultFromAddress,
defaultFromName,
sendEmail: async (message) => {
Expand Down
1 change: 1 addition & 0 deletions packages/payload/src/email/consoleEmailAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { emailDefaults } from './defaults.js'
import { getStringifiedToAddress } from './getStringifiedToAddress.js'

export const consoleEmailAdapter: EmailAdapter<void> = ({ payload }) => ({
name: 'console',
defaultFromAddress: emailDefaults.defaultFromAddress,
defaultFromName: emailDefaults.defaultFromName,
sendEmail: async (message) => {
Expand Down
1 change: 1 addition & 0 deletions packages/payload/src/email/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@ export type InitializedEmailAdapter<TSendEmailResponse = unknown> = ReturnType<
export type EmailAdapter<TSendEmailResponse = unknown> = ({ payload }: { payload: Payload }) => {
defaultFromAddress: string
defaultFromName: string
name: string
sendEmail: (message: SendEmailOptions) => Promise<TSendEmailResponse>
}
59 changes: 44 additions & 15 deletions packages/payload/src/utilities/telemetry/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ const Conf = (ConfImport.default || ConfImport) as unknown as typeof ConfImport.

export type BaseEvent = {
ciName: null | string
dbAdapter: string
emailAdapter: null | string
envID: string
isCI: boolean
locales: string[]
localizationDefaultLocale: null | string
localizationEnabled: boolean
nodeEnv: string
nodeVersion: string
payloadPackages: Record<string, string>
payloadVersion: string
projectID: string
}
Expand All @@ -42,7 +46,7 @@ let baseEvent: BaseEvent | null = null

export const sendEvent = async ({ event, payload }: Args): Promise<void> => {
try {
const packageJSON = await getPackageJSON()
const { packageJSON, packageJSONPath } = await getPackageJSON()

// Only generate the base event once
if (!baseEvent) {
Expand All @@ -52,15 +56,17 @@ export const sendEvent = async ({ event, payload }: Args): Promise<void> => {
isCI: ciInfo.isCI,
nodeEnv: process.env.NODE_ENV || 'development',
nodeVersion: process.version,
payloadPackages: getPayloadPackages(packageJSON),
payloadVersion: getPayloadVersion(packageJSON),
projectID: getProjectID(payload, packageJSON),
...getLocalizationInfo(payload),
dbAdapter: payload.db.name,
emailAdapter: payload.email?.name || null,
}
}

if (process.env.PAYLOAD_TELEMETRY_DEBUG) {
payload.logger.info({
event: { ...baseEvent, ...event },
event: { ...baseEvent, ...event, packageJSONPath },
msg: 'Telemetry Event',
})
return
Expand Down Expand Up @@ -120,18 +126,23 @@ const getGitID = (payload: Payload) => {
}
}

const getPayloadPackages = (packageJSON: PackageJSON): Record<string, string> => {
return Object.keys(packageJSON.dependencies || {}).reduce((acc, key) => {
return key.startsWith('@payloadcms/') ? { ...acc, [key]: packageJSON.dependencies[key] } : acc
}, {})
}
const getPackageJSON = async (): Promise<{
packageJSON?: PackageJSON
packageJSONPath: string
}> => {
let packageJSONPath = path.resolve(process.cwd(), 'package.json')

if (!fs.existsSync(packageJSONPath)) {
// Old logic
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
packageJSONPath = await findUp('package.json', { cwd: dirname })
const jsonContent: PackageJSON = JSON.parse(fs.readFileSync(packageJSONPath, 'utf-8'))
return { packageJSON: jsonContent, packageJSONPath }
}

const getPackageJSON = async (): Promise<PackageJSON> => {
const filename = fileURLToPath(import.meta.url)
const dirname = path.dirname(filename)
const packageJsonPath = await findUp('package.json', { cwd: dirname })
const jsonContent: PackageJSON = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
return jsonContent
const packageJSON: PackageJSON = JSON.parse(fs.readFileSync(packageJSONPath, 'utf-8'))
return { packageJSON, packageJSONPath }
}

const getPackageJSONID = (payload: Payload, packageJSON: PackageJSON): string => {
Expand All @@ -141,3 +152,21 @@ const getPackageJSONID = (payload: Payload, packageJSON: PackageJSON): string =>
export const getPayloadVersion = (packageJSON: PackageJSON): string => {
return packageJSON?.dependencies?.payload ?? ''
}

export const getLocalizationInfo = (
payload: Payload,
): Pick<BaseEvent, 'locales' | 'localizationDefaultLocale' | 'localizationEnabled'> => {
if (!payload.config.localization) {
return {
locales: [],
localizationDefaultLocale: null,
localizationEnabled: false,
}
}

return {
locales: payload.config.localization.localeCodes,
localizationDefaultLocale: payload.config.localization.defaultLocale,
localizationEnabled: true,
}
}
1 change: 1 addition & 0 deletions packages/plugin-cloud-storage/src/adapters/azure/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export const azureBlobStorageAdapter = ({

return ({ collection, prefix }): GeneratedAdapter => {
return {
name: 'azure',
generateURL: getGenerateURL({ baseURL, containerName }),
handleDelete: getHandleDelete({ collection, getStorageClient }),
handleUpload: getHandleUpload({
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-cloud-storage/src/adapters/gcs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export const gcsAdapter =
}

return {
name: 'gcs',
generateURL: getGenerateURL({ bucket, getStorageClient }),
handleDelete: getHandleDelete({ bucket, getStorageClient }),
handleUpload: getHandleUpload({
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-cloud-storage/src/adapters/s3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const s3Adapter =
}

return {
name: 's3',
generateURL: getGenerateURL({ bucket, config }),
handleDelete: getHandleDelete({ bucket, getStorageClient }),
handleUpload: getHandleUpload({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export const vercelBlobAdapter =
const baseUrl = `https://${storeId}.${access}.blob.vercel-storage.com`

return {
name: 'vercel-blob',
generateURL: getGenerateUrl({ baseUrl, prefix }),
handleDelete: getHandleDelete({ baseUrl, prefix, token }),
handleUpload: getHandleUpload({
Expand Down
8 changes: 4 additions & 4 deletions packages/plugin-cloud-storage/src/hooks/afterDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export const getAfterDeleteHook = ({

await Promise.all(promises)
} catch (err: unknown) {
req.payload.logger.error(
`There was an error while deleting files corresponding to the ${collection.labels?.singular} with ID ${doc.id}:`,
)
req.payload.logger.error(err)
req.payload.logger.error({
err,
msg: `There was an error while deleting files corresponding to the ${collection.labels?.singular} with ID ${doc.id}.`,
})
}
return doc
}
Expand Down
1 change: 1 addition & 0 deletions packages/plugin-cloud-storage/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export interface GeneratedAdapter {
generateURL: GenerateURL
handleDelete: HandleDelete
handleUpload: HandleUpload
name: string
onInit?: () => void
staticHandler: StaticHandler
}
Expand Down
1 change: 1 addition & 0 deletions packages/storage-azure/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ function azureStorageInternal({

return ({ collection, prefix }): GeneratedAdapter => {
return {
name: 'azure',
generateURL: getGenerateURL({ baseURL, containerName }),
handleDelete: getHandleDelete({ collection, getStorageClient }),
handleUpload: getHandleUpload({
Expand Down
1 change: 1 addition & 0 deletions packages/storage-gcs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ function gcsStorageInternal({ acl, bucket, options }: GcsStorageOptions): Adapte
}

return {
name: 'gcs',
generateURL: getGenerateURL({ bucket, getStorageClient }),
handleDelete: getHandleDelete({ bucket, getStorageClient }),
handleUpload: getHandleUpload({
Expand Down
1 change: 1 addition & 0 deletions packages/storage-s3/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ function s3StorageInternal({ acl, bucket, config = {} }: S3StorageOptions): Adap
}

return {
name: 's3',
generateURL: getGenerateURL({ bucket, config }),
handleDelete: getHandleDelete({ bucket, getStorageClient }),
handleUpload: getHandleUpload({
Expand Down
1 change: 1 addition & 0 deletions packages/storage-vercel-blob/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ function vercelBlobStorageInternal(
return ({ collection, prefix }): GeneratedAdapter => {
const { access, addRandomSuffix, baseUrl, cacheControlMaxAge, token } = options
return {
name: 'vercel-blob',
generateURL: getGenerateUrl({ baseUrl, prefix }),
handleDelete: getHandleDelete({ baseUrl, prefix, token: options.token }),
handleUpload: getHandleUpload({
Expand Down

0 comments on commit cff5a25

Please sign in to comment.