-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
JobErrorReporter.java
297 lines (260 loc) · 15.4 KB
/
JobErrorReporter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*
* Copyright (c) 2022 Airbyte, Inc., all rights reserved.
*/
package io.airbyte.persistence.job.errorreporter;
import com.google.common.collect.ImmutableSet;
import edu.umd.cs.findbugs.annotations.Nullable;
import io.airbyte.commons.lang.Exceptions;
import io.airbyte.commons.map.MoreMaps;
import io.airbyte.config.AttemptFailureSummary;
import io.airbyte.config.Configs.DeploymentMode;
import io.airbyte.config.FailureReason;
import io.airbyte.config.FailureReason.FailureOrigin;
import io.airbyte.config.FailureReason.FailureType;
import io.airbyte.config.StandardDestinationDefinition;
import io.airbyte.config.StandardSourceDefinition;
import io.airbyte.config.StandardWorkspace;
import io.airbyte.config.persistence.ConfigNotFoundException;
import io.airbyte.config.persistence.ConfigRepository;
import io.airbyte.persistence.job.WebUrlHelper;
import io.airbyte.validation.json.JsonValidationException;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JobErrorReporter {
private static final Logger LOGGER = LoggerFactory.getLogger(JobErrorReporter.class);
private static final String FROM_TRACE_MESSAGE = "from_trace_message";
private static final String DEPLOYMENT_MODE_META_KEY = "deployment_mode";
private static final String AIRBYTE_VERSION_META_KEY = "airbyte_version";
private static final String FAILURE_ORIGIN_META_KEY = "failure_origin";
private static final String FAILURE_TYPE_META_KEY = "failure_type";
private static final String WORKSPACE_ID_META_KEY = "workspace_id";
private static final String WORKSPACE_URL_META_KEY = "workspace_url";
private static final String CONNECTION_ID_META_KEY = "connection_id";
private static final String CONNECTION_URL_META_KEY = "connection_url";
private static final String CONNECTOR_NAME_META_KEY = "connector_name";
private static final String CONNECTOR_REPOSITORY_META_KEY = "connector_repository";
private static final String CONNECTOR_DEFINITION_ID_META_KEY = "connector_definition_id";
private static final String CONNECTOR_RELEASE_STAGE_META_KEY = "connector_release_stage";
private static final String CONNECTOR_COMMAND_META_KEY = "connector_command";
private static final String NORMALIZATION_REPOSITORY_META_KEY = "normalization_repository";
private static final String JOB_ID_KEY = "job_id";
private static final ImmutableSet<FailureType> UNSUPPORTED_FAILURETYPES =
ImmutableSet.of(FailureType.CONFIG_ERROR, FailureType.MANUAL_CANCELLATION);
private final ConfigRepository configRepository;
private final DeploymentMode deploymentMode;
private final String airbyteVersion;
private final WebUrlHelper webUrlHelper;
private final JobErrorReportingClient jobErrorReportingClient;
public JobErrorReporter(final ConfigRepository configRepository,
final DeploymentMode deploymentMode,
final String airbyteVersion,
final WebUrlHelper webUrlHelper,
final JobErrorReportingClient jobErrorReportingClient) {
this.configRepository = configRepository;
this.deploymentMode = deploymentMode;
this.airbyteVersion = airbyteVersion;
this.webUrlHelper = webUrlHelper;
this.jobErrorReportingClient = jobErrorReportingClient;
}
/**
* Reports a Sync Job's connector-caused FailureReasons to the JobErrorReportingClient
*
* @param connectionId - connection that had the failure
* @param failureSummary - final attempt failure summary
* @param jobContext - sync job reporting context
*/
public void reportSyncJobFailure(final UUID connectionId, final AttemptFailureSummary failureSummary, final SyncJobReportingContext jobContext) {
Exceptions.swallow(() -> {
final List<FailureReason> traceMessageFailures = failureSummary.getFailures().stream()
.filter(failure -> failure.getMetadata() != null && failure.getMetadata().getAdditionalProperties().containsKey(FROM_TRACE_MESSAGE))
.toList();
final StandardWorkspace workspace = configRepository.getStandardWorkspaceFromConnection(connectionId, true);
final Map<String, String> commonMetadata = MoreMaps.merge(
Map.of(JOB_ID_KEY, String.valueOf(jobContext.jobId())),
getConnectionMetadata(workspace.getWorkspaceId(), connectionId));
for (final FailureReason failureReason : traceMessageFailures) {
final FailureOrigin failureOrigin = failureReason.getFailureOrigin();
if (failureOrigin == FailureOrigin.SOURCE) {
final StandardSourceDefinition sourceDefinition = configRepository.getSourceDefinitionFromConnection(connectionId);
final String dockerImage = jobContext.sourceDockerImage();
final Map<String, String> metadata = MoreMaps.merge(commonMetadata, getSourceMetadata(sourceDefinition));
reportJobFailureReason(workspace, failureReason, dockerImage, metadata);
} else if (failureOrigin == FailureOrigin.DESTINATION) {
final StandardDestinationDefinition destinationDefinition = configRepository.getDestinationDefinitionFromConnection(connectionId);
final String dockerImage = jobContext.destinationDockerImage();
final Map<String, String> metadata = MoreMaps.merge(commonMetadata, getDestinationMetadata(destinationDefinition));
reportJobFailureReason(workspace, failureReason, dockerImage, metadata);
} else if (failureOrigin == FailureOrigin.NORMALIZATION) {
final StandardSourceDefinition sourceDefinition = configRepository.getSourceDefinitionFromConnection(connectionId);
final StandardDestinationDefinition destinationDefinition = configRepository.getDestinationDefinitionFromConnection(connectionId);
// since error could be arising from source or destination or normalization itself, we want all the
// metadata
// prefixing source keys so we don't overlap (destination as 'true' keys since normalization runs on
// the destination)
final Map<String, String> metadata = MoreMaps.merge(
commonMetadata,
getNormalizationMetadata(destinationDefinition.getNormalizationConfig().getNormalizationRepository()),
prefixConnectorMetadataKeys(getSourceMetadata(sourceDefinition), "source"),
getDestinationMetadata(destinationDefinition));
final String dockerImage =
destinationDefinition.getNormalizationConfig().getNormalizationRepository() + ":" +
destinationDefinition.getNormalizationConfig().getNormalizationTag();
reportJobFailureReason(workspace, failureReason, dockerImage, metadata);
}
}
});
}
/**
* Reports a FailureReason from a connector Check job for a Source to the JobErrorReportingClient
*
* @param workspaceId - workspace for which the check failed
* @param failureReason - failure reason from the check connection job
* @param jobContext - connector job reporting context
*/
public void reportSourceCheckJobFailure(final UUID sourceDefinitionId,
@Nullable final UUID workspaceId,
final FailureReason failureReason,
final ConnectorJobReportingContext jobContext)
throws JsonValidationException, ConfigNotFoundException, IOException {
final StandardWorkspace workspace = workspaceId != null ? configRepository.getStandardWorkspaceNoSecrets(workspaceId, true) : null;
final StandardSourceDefinition sourceDefinition = configRepository.getStandardSourceDefinition(sourceDefinitionId);
final Map<String, String> metadata = MoreMaps.merge(
getSourceMetadata(sourceDefinition),
Map.of(JOB_ID_KEY, jobContext.jobId().toString()));
reportJobFailureReason(workspace, failureReason.withFailureOrigin(FailureOrigin.SOURCE), jobContext.dockerImage(), metadata);
}
/**
* Reports a FailureReason from a connector Check job for a Destination to the
* JobErrorReportingClient
*
* @param workspaceId - workspace for which the check failed
* @param failureReason - failure reason from the check connection job
* @param jobContext - connector job reporting context
*/
public void reportDestinationCheckJobFailure(final UUID destinationDefinitionId,
@Nullable final UUID workspaceId,
final FailureReason failureReason,
final ConnectorJobReportingContext jobContext)
throws JsonValidationException, ConfigNotFoundException, IOException {
final StandardWorkspace workspace = workspaceId != null ? configRepository.getStandardWorkspaceNoSecrets(workspaceId, true) : null;
final StandardDestinationDefinition destinationDefinition = configRepository.getStandardDestinationDefinition(destinationDefinitionId);
final Map<String, String> metadata = MoreMaps.merge(
getDestinationMetadata(destinationDefinition),
Map.of(JOB_ID_KEY, jobContext.jobId().toString()));
reportJobFailureReason(workspace, failureReason.withFailureOrigin(FailureOrigin.DESTINATION), jobContext.dockerImage(), metadata);
}
/**
* Reports a FailureReason from a connector Deploy job for a Source to the JobErrorReportingClient
*
* @param workspaceId - workspace for which the Discover job failed
* @param failureReason - failure reason from the Discover job
* @param jobContext - connector job reporting context
*/
public void reportDiscoverJobFailure(final UUID sourceDefinitionId,
@Nullable final UUID workspaceId,
final FailureReason failureReason,
final ConnectorJobReportingContext jobContext)
throws JsonValidationException, ConfigNotFoundException, IOException {
final StandardWorkspace workspace = workspaceId != null ? configRepository.getStandardWorkspaceNoSecrets(workspaceId, true) : null;
final StandardSourceDefinition sourceDefinition = configRepository.getStandardSourceDefinition(sourceDefinitionId);
final Map<String, String> metadata = MoreMaps.merge(
getSourceMetadata(sourceDefinition),
Map.of(JOB_ID_KEY, jobContext.jobId().toString()));
reportJobFailureReason(workspace, failureReason, jobContext.dockerImage(), metadata);
}
/**
* Reports a FailureReason from a connector Spec job to the JobErrorReportingClient
*
* @param failureReason - failure reason from the Deploy job
* @param jobContext - connector job reporting context
*/
public void reportSpecJobFailure(final FailureReason failureReason, final ConnectorJobReportingContext jobContext) {
final String dockerImage = jobContext.dockerImage();
final String connectorRepository = dockerImage.split(":")[0];
final Map<String, String> metadata = Map.of(
JOB_ID_KEY, jobContext.jobId().toString(),
CONNECTOR_REPOSITORY_META_KEY, connectorRepository);
reportJobFailureReason(null, failureReason, dockerImage, metadata);
}
private Map<String, String> getConnectionMetadata(final UUID workspaceId, final UUID connectionId) {
final String connectionUrl = webUrlHelper.getConnectionUrl(workspaceId, connectionId);
return Map.ofEntries(
Map.entry(CONNECTION_ID_META_KEY, connectionId.toString()),
Map.entry(CONNECTION_URL_META_KEY, connectionUrl));
}
private Map<String, String> getDestinationMetadata(final StandardDestinationDefinition destinationDefinition) {
return Map.ofEntries(
Map.entry(CONNECTOR_DEFINITION_ID_META_KEY, destinationDefinition.getDestinationDefinitionId().toString()),
Map.entry(CONNECTOR_NAME_META_KEY, destinationDefinition.getName()),
Map.entry(CONNECTOR_REPOSITORY_META_KEY, destinationDefinition.getDockerRepository()),
Map.entry(CONNECTOR_RELEASE_STAGE_META_KEY, destinationDefinition.getReleaseStage().value()));
}
private Map<String, String> getSourceMetadata(final StandardSourceDefinition sourceDefinition) {
return Map.ofEntries(
Map.entry(CONNECTOR_DEFINITION_ID_META_KEY, sourceDefinition.getSourceDefinitionId().toString()),
Map.entry(CONNECTOR_NAME_META_KEY, sourceDefinition.getName()),
Map.entry(CONNECTOR_REPOSITORY_META_KEY, sourceDefinition.getDockerRepository()),
Map.entry(CONNECTOR_RELEASE_STAGE_META_KEY, sourceDefinition.getReleaseStage().value()));
}
private Map<String, String> getNormalizationMetadata(final String normalizationImage) {
return Map.ofEntries(
Map.entry(NORMALIZATION_REPOSITORY_META_KEY, normalizationImage));
}
private Map<String, String> prefixConnectorMetadataKeys(final Map<String, String> connectorMetadata, final String prefix) {
final Map<String, String> prefixedMetadata = new HashMap<>();
for (final Map.Entry<String, String> entry : connectorMetadata.entrySet()) {
prefixedMetadata.put(String.format("%s_%s", prefix, entry.getKey()), entry.getValue());
}
return prefixedMetadata;
}
private Map<String, String> getFailureReasonMetadata(final FailureReason failureReason) {
final Map<String, Object> failureReasonAdditionalProps = failureReason.getMetadata().getAdditionalProperties();
final Map<String, String> outMetadata = new HashMap<>();
if (failureReasonAdditionalProps.containsKey(CONNECTOR_COMMAND_META_KEY)
&& failureReasonAdditionalProps.get(CONNECTOR_COMMAND_META_KEY) != null) {
outMetadata.put(CONNECTOR_COMMAND_META_KEY, failureReasonAdditionalProps.get(CONNECTOR_COMMAND_META_KEY).toString());
}
if (failureReason.getFailureOrigin() != null) {
outMetadata.put(FAILURE_ORIGIN_META_KEY, failureReason.getFailureOrigin().value());
}
if (failureReason.getFailureType() != null) {
outMetadata.put(FAILURE_TYPE_META_KEY, failureReason.getFailureType().value());
}
return outMetadata;
}
private Map<String, String> getWorkspaceMetadata(final UUID workspaceId) {
final String workspaceUrl = webUrlHelper.getWorkspaceUrl(workspaceId);
return Map.ofEntries(
Map.entry(WORKSPACE_ID_META_KEY, workspaceId.toString()),
Map.entry(WORKSPACE_URL_META_KEY, workspaceUrl));
}
private void reportJobFailureReason(@Nullable final StandardWorkspace workspace,
final FailureReason failureReason,
final String dockerImage,
final Map<String, String> metadata) {
// Failure types associated with a config-error or a manual-cancellation should NOT be reported.
if (UNSUPPORTED_FAILURETYPES.contains(failureReason.getFailureType())) {
return;
}
final Map<String, String> commonMetadata = new HashMap<>(Map.ofEntries(
Map.entry(AIRBYTE_VERSION_META_KEY, airbyteVersion),
Map.entry(DEPLOYMENT_MODE_META_KEY, deploymentMode.name())));
if (workspace != null) {
commonMetadata.putAll(getWorkspaceMetadata(workspace.getWorkspaceId()));
}
final Map<String, String> allMetadata = MoreMaps.merge(
commonMetadata,
getFailureReasonMetadata(failureReason),
metadata);
try {
jobErrorReportingClient.reportJobFailureReason(workspace, failureReason, dockerImage, allMetadata);
} catch (final Exception e) {
LOGGER.error("Error when reporting job failure reason: {}", failureReason, e);
}
}
}