Skip to content

Commit f87e29f

Browse files
adding logger
1 parent 087bbeb commit f87e29f

File tree

2 files changed

+99
-44
lines changed
  • packages
    • core/src/destination-kit
    • destination-actions/src/destinations/dotdigital-audiences

2 files changed

+99
-44
lines changed

packages/core/src/destination-kit/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ export type CreateAudienceInput<Settings = unknown, AudienceSettings = unknown>
117117
statsContext?: StatsContext
118118

119119
features?: Features
120+
121+
logger?: Logger
120122
}
121123

122124
export type GetAudienceInput<Settings = unknown, AudienceSettings = unknown> = {
@@ -129,6 +131,8 @@ export type GetAudienceInput<Settings = unknown, AudienceSettings = unknown> = {
129131
statsContext?: StatsContext
130132

131133
features?: Features
134+
135+
logger?: Logger
132136
}
133137

134138
export interface AudienceDestinationConfiguration {

packages/destination-actions/src/destinations/dotdigital-audiences/index.ts

Lines changed: 95 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -78,66 +78,117 @@ const destination: AudienceDestinationDefinition<Settings, AudienceSettings> = {
7878
audienceName,
7979
audienceSettings: {
8080
visibility
81-
} = {}
81+
} = {},
82+
statsContext,
83+
logger
8284
} = createAudienceInput
8385

84-
console.log('createAudience: Starting audience creation',
85-
{ audienceName, visibility, api_host })
86+
const statsClient = statsContext?.statsClient
87+
const statsTags = statsContext?.tags
8688

87-
console.log('createAudience: Fetching existing Dotdigital lists')
88-
const lists = await new DDListsApi(api_host, request).getLists()
89-
console.log('createAudience: Fetched existing lists',
90-
{ count: lists.choices?.length || 0 })
89+
try {
90+
logger?.info('createAudience: Starting audience creation',
91+
`audienceName: ${audienceName}`,
92+
`visibility: ${visibility}`,
93+
`api_host: ${api_host}`)
94+
statsClient?.incr('createAudience.started', 1, statsTags)
9195

92-
const exists = lists.choices.find((list: { value: string; label: string }) => list.label === audienceName)
96+
logger?.info('createAudience: Fetching existing Dotdigital lists')
97+
const lists = await new DDListsApi(api_host, request).getLists()
98+
logger?.info('createAudience: Fetched existing lists',
99+
`count: ${lists.choices?.length || 0}`)
100+
statsClient?.incr('createAudience.lists_fetched', 1, statsTags)
93101

94-
if(exists) {
95-
console.log('createAudience: Audience already exists',
96-
{ audienceName, externalId: exists.value })
97-
return { externalId: exists.value }
98-
}
99-
100-
console.log('createAudience: Audience does not exist, creating new audience')
101-
const url = `${api_host}/v2/address-books`
102-
const json: CreateListJSON = {
103-
name: audienceName,
104-
visibility: visibility as VisibilityOption
105-
}
102+
const exists = lists.choices.find((list: { value: string; label: string }) => list.label === audienceName)
106103

107-
console.log('createAudience: Sending POST request to create audience',
108-
{ url, payload: json })
104+
if(exists) {
105+
logger?.info('createAudience: Audience already exists',
106+
`audienceName: ${audienceName}`,
107+
`externalId: ${exists.value}`)
108+
statsClient?.incr('createAudience.already_exists', 1, statsTags)
109+
return { externalId: exists.value }
110+
}
109111

110-
const response = await request<CreateListResp>(
111-
url,
112-
{
113-
method: 'POST',
114-
json
112+
logger?.info('createAudience: Audience does not exist, creating new audience')
113+
const url = `${api_host}/v2/address-books`
114+
const json: CreateListJSON = {
115+
name: audienceName,
116+
visibility: visibility as VisibilityOption
115117
}
116-
)
117118

118-
console.log('createAudience: Received response',
119-
{ status: response.status })
119+
logger?.info('createAudience: Sending POST request to create audience',
120+
`url: ${url}`,
121+
`payload: ${JSON.stringify(json)}`)
122+
123+
const response = await request<CreateListResp>(
124+
url,
125+
{
126+
method: 'POST',
127+
json
128+
}
129+
)
120130

121-
const jsonOutput = await response.json()
131+
logger?.info('createAudience: Received response',
132+
`status: ${response.status}`)
122133

123-
console.log('createAudience: Successfully created audience',
124-
{ externalId: jsonOutput.id, response: jsonOutput })
134+
const jsonOutput = await response.json()
125135

126-
return { externalId: String(jsonOutput.id) }
136+
logger?.info('createAudience: Successfully created audience',
137+
`externalId: ${jsonOutput.id}`,
138+
`response: ${JSON.stringify(jsonOutput)}`)
139+
statsClient?.incr('createAudience.success', 1, statsTags)
140+
return { externalId: String(jsonOutput.id) }
141+
} catch (error) {
142+
logger?.error('createAudience: Error occurred',
143+
`error: ${error}`)
144+
statsClient?.incr('createAudience.error', 1, statsTags)
145+
throw error
146+
}
127147
},
128148
async getAudience(request, getAudienceInput) {
129-
const {
130-
settings: {
131-
api_host
132-
},
133-
externalId
149+
const {
150+
settings: {
151+
api_host
152+
},
153+
externalId,
154+
statsContext,
155+
logger
134156
} = getAudienceInput
135-
const lists = await new DDListsApi(api_host, request).getLists()
136-
const exists = lists.choices.find((list: { value: string; label: string }) => list.value === externalId.toString())
137-
if(!exists) {
138-
throw new IntegrationError(`Audience with id ${externalId} not found`, 'Not Found', 404)
157+
158+
const statsClient = statsContext?.statsClient
159+
const statsTags = statsContext?.tags
160+
161+
try {
162+
logger?.info('getAudience: Starting audience lookup',
163+
`externalId: ${externalId}`,
164+
`api_host: ${api_host}`)
165+
statsClient?.incr('getAudience.started', 1, statsTags)
166+
167+
logger?.info('getAudience: Fetching Dotdigital lists')
168+
const lists = await new DDListsApi(api_host, request).getLists()
169+
logger?.info('getAudience: Fetched lists',
170+
`count: ${lists.choices?.length || 0}`)
171+
statsClient?.incr('getAudience.lists_fetched', 1, statsTags)
172+
173+
const exists = lists.choices.find((list: { value: string; label: string }) => list.value === externalId.toString())
174+
175+
if(!exists) {
176+
logger?.warn('getAudience: Audience not found',
177+
`externalId: ${externalId}`)
178+
statsClient?.incr('getAudience.not_found', 1, statsTags)
179+
throw new IntegrationError(`Audience with id ${externalId} not found`, 'Not Found', 404)
180+
}
181+
182+
logger?.info('getAudience: Successfully found audience',
183+
`externalId: ${externalId}`)
184+
statsClient?.incr('getAudience.success', 1, statsTags)
185+
return { externalId }
186+
} catch (error) {
187+
logger?.error('getAudience: Error occurred',
188+
`error: ${error}`)
189+
statsClient?.incr('getAudience.error', 1, statsTags)
190+
throw error
139191
}
140-
return { externalId }
141192
}
142193
},
143194
actions: {

0 commit comments

Comments
 (0)