26
26
import org .apache .logging .log4j .ThreadContext ;
27
27
import org .wso2 .carbon .apimgt .common .analytics .collectors .impl .GenericRequestDataCollector ;
28
28
import org .wso2 .carbon .apimgt .common .analytics .exceptions .AnalyticsException ;
29
+ import org .wso2 .carbon .apimgt .common .analytics .publishers .dto .Error ;
30
+ import org .wso2 .carbon .apimgt .common .analytics .publishers .dto .enums .FaultCategory ;
29
31
import org .wso2 .choreo .connect .enforcer .commons .model .AuthenticationContext ;
30
32
import org .wso2 .choreo .connect .enforcer .commons .model .RequestContext ;
31
33
import org .wso2 .choreo .connect .enforcer .config .ConfigHolder ;
@@ -108,59 +110,7 @@ public void handleSuccessRequest(RequestContext requestContext) {
108
110
Utils .setTag (analyticsSpan , APIConstants .LOG_TRACE_ID ,
109
111
ThreadContext .get (APIConstants .LOG_TRACE_ID ));
110
112
}
111
- String apiName = requestContext .getMatchedAPI ().getName ();
112
- String apiVersion = requestContext .getMatchedAPI ().getVersion ();
113
- String apiType = requestContext .getMatchedAPI ().getApiType ();
114
- AuthenticationContext authContext = AnalyticsUtils .getAuthenticationContext (requestContext );
115
-
116
- requestContext .addMetadataToMap (MetadataConstants .API_ID_KEY , AnalyticsUtils .getAPIId (requestContext ));
117
- requestContext .addMetadataToMap (MetadataConstants .API_CREATOR_KEY ,
118
- AnalyticsUtils .setDefaultIfNull (authContext .getApiPublisher ()));
119
- requestContext .addMetadataToMap (MetadataConstants .API_NAME_KEY , apiName );
120
- requestContext .addMetadataToMap (MetadataConstants .API_VERSION_KEY , apiVersion );
121
- requestContext .addMetadataToMap (MetadataConstants .API_TYPE_KEY , apiType );
122
-
123
- requestContext .addMetadataToMap (MetadataConstants .API_CREATOR_TENANT_DOMAIN_KEY ,
124
- APIConstants .SUPER_TENANT_DOMAIN_NAME );
125
- requestContext .addMetadataToMap (MetadataConstants .API_ENVIRONMENT_ID ,
126
- requestContext .getMatchedAPI ().getEnvironmentId () == null ? APIConstants .DEFAULT_ENVIRONMENT_NAME :
127
- requestContext .getMatchedAPI ().getEnvironmentId ());
128
-
129
- // Default Value would be PRODUCTION
130
- requestContext .addMetadataToMap (MetadataConstants .APP_KEY_TYPE_KEY ,
131
- authContext .getKeyType () == null ? APIConstants .API_KEY_TYPE_PRODUCTION : authContext .getKeyType ());
132
- requestContext .addMetadataToMap (MetadataConstants .APP_UUID_KEY ,
133
- AnalyticsUtils .setDefaultIfNull (authContext .getApplicationUUID ()));
134
- requestContext .addMetadataToMap (MetadataConstants .APP_NAME_KEY ,
135
- AnalyticsUtils .setDefaultIfNull (authContext .getApplicationName ()));
136
- requestContext .addMetadataToMap (MetadataConstants .APP_OWNER_KEY ,
137
- AnalyticsUtils .setDefaultIfNull (authContext .getSubscriber ()));
138
-
139
- requestContext .addMetadataToMap (MetadataConstants .CORRELATION_ID_KEY , requestContext .getRequestID ());
140
- requestContext .addMetadataToMap (MetadataConstants .REGION_KEY ,
141
- ConfigHolder .getInstance ().getEnvVarConfig ().getEnforcerRegionId ());
142
-
143
- // As in the matched API, only the resources under the matched resource template are selected.
144
- requestContext .addMetadataToMap (MetadataConstants .API_RESOURCE_TEMPLATE_KEY ,
145
- requestContext .getMatchedResourcePath ().getPath ());
146
-
147
- requestContext .addMetadataToMap (MetadataConstants .DESTINATION , resolveEndpoint (requestContext ));
148
-
149
- requestContext .addMetadataToMap (MetadataConstants .API_ORGANIZATION_ID ,
150
- requestContext .getMatchedAPI ().getOrganizationId ());
151
- // Adding UserName and the APIContext
152
- String endUserName = requestContext .getAuthenticationContext ().getUsername ();
153
- requestContext .addMetadataToMap (MetadataConstants .API_USER_NAME_KEY ,
154
- endUserName == null ? APIConstants .END_USER_UNKNOWN : endUserName );
155
- requestContext .addMetadataToMap (MetadataConstants .API_CONTEXT_KEY ,
156
- requestContext .getMatchedAPI ().getBasePath ());
157
- requestContext .addMetadataToMap (MetadataConstants .DEPLOYMENT_TYPE ,
158
- requestContext .getMatchedAPI ().getDeploymentType ());
159
-
160
- // Adding Gateway URL
161
- String gatewayUrl = requestContext .getHeaders ().get (GATEWAY_URL );
162
- requestContext .addMetadataToMap (MetadataConstants .GATEWAY_URL ,
163
- gatewayUrl );
113
+ this .addInsightsMetaData (requestContext );
164
114
} finally {
165
115
if (Utils .tracingEnabled ()) {
166
116
analyticsSpanScope .close ();
@@ -188,11 +138,13 @@ public void handleFailureRequest(RequestContext requestContext) {
188
138
ThreadContext .get (APIConstants .LOG_TRACE_ID ));
189
139
190
140
}
141
+ this .addInsightsMetaData (requestContext );
191
142
if (publisher == null ) {
192
143
logger .error ("Cannot publish the failure event as analytics publisher is null." );
193
144
return ;
194
145
}
195
146
ChoreoFaultAnalyticsProvider provider = new ChoreoFaultAnalyticsProvider (requestContext );
147
+ this .addFailureDetailsToInsightsMetaData (requestContext , provider );
196
148
// To avoid incrementing counter for options call
197
149
if (provider .getProxyResponseCode () == 200 || provider .getProxyResponseCode () == 204 ) {
198
150
return ;
@@ -212,6 +164,80 @@ public void handleFailureRequest(RequestContext requestContext) {
212
164
}
213
165
}
214
166
167
+ private void addInsightsMetaData (RequestContext requestContext ) {
168
+ String apiName = requestContext .getMatchedAPI ().getName ();
169
+ String apiVersion = requestContext .getMatchedAPI ().getVersion ();
170
+ String apiType = requestContext .getMatchedAPI ().getApiType ();
171
+ String projectId = requestContext .getMatchedAPI ().getChoreoComponentInfo ().getProjectID ();
172
+ String componentId = requestContext .getMatchedAPI ().getChoreoComponentInfo ().getComponentID ();
173
+ String versionId = requestContext .getMatchedAPI ().getChoreoComponentInfo ().getVersionID ();
174
+
175
+ AuthenticationContext authContext = AnalyticsUtils .getAuthenticationContext (requestContext );
176
+
177
+ requestContext .addMetadataToMap (MetadataConstants .API_PROJECT_ID_KEY , projectId );
178
+ requestContext .addMetadataToMap (MetadataConstants .API_COMPONENT_ID_KEY , componentId );
179
+ requestContext .addMetadataToMap (MetadataConstants .API_VERSION_ID_KEY , versionId );
180
+
181
+ requestContext .addMetadataToMap (MetadataConstants .API_ID_KEY , AnalyticsUtils .getAPIId (requestContext ));
182
+ requestContext .addMetadataToMap (MetadataConstants .API_CREATOR_KEY ,
183
+ AnalyticsUtils .setDefaultIfNull (authContext .getApiPublisher ()));
184
+ requestContext .addMetadataToMap (MetadataConstants .API_NAME_KEY , apiName );
185
+ requestContext .addMetadataToMap (MetadataConstants .API_VERSION_KEY , apiVersion );
186
+ requestContext .addMetadataToMap (MetadataConstants .API_TYPE_KEY , apiType );
187
+
188
+ requestContext .addMetadataToMap (MetadataConstants .API_CREATOR_TENANT_DOMAIN_KEY ,
189
+ APIConstants .SUPER_TENANT_DOMAIN_NAME );
190
+ requestContext .addMetadataToMap (MetadataConstants .API_ENVIRONMENT_ID ,
191
+ requestContext .getMatchedAPI ().getEnvironmentId () == null ? APIConstants .DEFAULT_ENVIRONMENT_NAME :
192
+ requestContext .getMatchedAPI ().getEnvironmentId ());
193
+
194
+ // Default Value would be PRODUCTION
195
+ requestContext .addMetadataToMap (MetadataConstants .APP_KEY_TYPE_KEY ,
196
+ authContext .getKeyType () == null ? APIConstants .API_KEY_TYPE_PRODUCTION : authContext .getKeyType ());
197
+ requestContext .addMetadataToMap (MetadataConstants .APP_UUID_KEY ,
198
+ AnalyticsUtils .setDefaultIfNull (authContext .getApplicationUUID ()));
199
+ requestContext .addMetadataToMap (MetadataConstants .APP_NAME_KEY ,
200
+ AnalyticsUtils .setDefaultIfNull (authContext .getApplicationName ()));
201
+ requestContext .addMetadataToMap (MetadataConstants .APP_OWNER_KEY ,
202
+ AnalyticsUtils .setDefaultIfNull (authContext .getSubscriber ()));
203
+
204
+ requestContext .addMetadataToMap (MetadataConstants .CORRELATION_ID_KEY , requestContext .getRequestID ());
205
+ requestContext .addMetadataToMap (MetadataConstants .REGION_KEY ,
206
+ ConfigHolder .getInstance ().getEnvVarConfig ().getEnforcerRegionId ());
207
+
208
+ // As in the matched API, only the resources under the matched resource template are selected.
209
+ requestContext .addMetadataToMap (MetadataConstants .API_RESOURCE_TEMPLATE_KEY ,
210
+ requestContext .getMatchedResourcePath ().getPath ());
211
+
212
+ requestContext .addMetadataToMap (MetadataConstants .DESTINATION , resolveEndpoint (requestContext ));
213
+
214
+ requestContext .addMetadataToMap (MetadataConstants .API_ORGANIZATION_ID ,
215
+ requestContext .getMatchedAPI ().getOrganizationId ());
216
+ // Adding UserName and the APIContext
217
+ String endUserName = requestContext .getAuthenticationContext ().getUsername ();
218
+ requestContext .addMetadataToMap (MetadataConstants .API_USER_NAME_KEY ,
219
+ endUserName == null ? APIConstants .END_USER_UNKNOWN : endUserName );
220
+ requestContext .addMetadataToMap (MetadataConstants .API_CONTEXT_KEY ,
221
+ requestContext .getMatchedAPI ().getBasePath ());
222
+ requestContext .addMetadataToMap (MetadataConstants .DEPLOYMENT_TYPE ,
223
+ requestContext .getMatchedAPI ().getDeploymentType ());
224
+
225
+ // Adding Gateway URL
226
+ String gatewayUrl = requestContext .getHeaders ().get (GATEWAY_URL );
227
+ requestContext .addMetadataToMap (MetadataConstants .GATEWAY_URL ,
228
+ gatewayUrl );
229
+ }
230
+
231
+ private void addFailureDetailsToInsightsMetaData (RequestContext requestContext ,
232
+ ChoreoFaultAnalyticsProvider provider ) {
233
+ FaultCategory faultCategory = provider .getFaultType ();
234
+ Error error = provider .getError (faultCategory );
235
+
236
+ requestContext .addMetadataToMap (MetadataConstants .INSIGHTS_ERROR_TYPE , faultCategory .toString ());
237
+ requestContext .addMetadataToMap (MetadataConstants .INSIGHTS_ERROR_CODE , String .valueOf (error .getErrorCode ()));
238
+ requestContext .addMetadataToMap (MetadataConstants .INSIGHTS_ERROR_MESSAGE , error .getErrorMessage ().toString ());
239
+ }
240
+
215
241
private static AnalyticsEventPublisher loadAnalyticsPublisher (String className , boolean isChoreoDeployment ) {
216
242
217
243
// For the choreo deployment, class name need not to be provided.
0 commit comments