Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Choreo analytics for Micro Integrator. #2927

Merged
merged 10 commits into from
Jul 24, 2023

Conversation

piyumaldk
Copy link
Contributor

@piyumaldk piyumaldk commented Jul 18, 2023

Purpose

Fix wso2/api-manager#1992

Steps to enable Choreo Analytics

  1. Add the following to the deployment.toml file.

i. Enabling statistics for artifacts

[mediation]
flow.statistics.enable=true
flow.statistics.capture_all=true

ii. Enabling Choreo Analytics

[analytics]
enabled=true
publisher="choreo" 

Adding publisher is required. If not added, the default publisher will be used which is ELK flow

  1. Optionally, you can add the following to the deployment.toml file.
[analytics]
api_analytics.enabled = true
proxy_service_analytics.enabled = true
sequence_analytics.enabled = true
endpoint_analytics.enabled = true
inbound_endpoint_analytics.enabled = true

Note that default value for above 5 will be true (If not added). If you want to disable analytics for any of the above, change the value to false.

  1. Restart the server.

Special Note - This is a WIP code so publishing to Choreo is not fully implemented.

Comment on lines 40 to 53
if (publisherTypes != null && !publisherTypes.isEmpty() &&
(publisherTypes.contains(MediationDataPublisherConstants.DATABRIDGE_PUBLISHER_TYPE )
|| publisherTypes.contains(MediationDataPublisherConstants.LOG_PUBLISHER_TYPE))) {
|| publisherTypes.contains(MediationDataPublisherConstants.LOG_PUBLISHER_TYPE)
|| publisherTypes.contains(MediationDataPublisherConstants.CHOREO_PUBLISHER_TYPE))) {
if (publisherTypes.contains(MediationDataPublisherConstants.DATABRIDGE_PUBLISHER_TYPE)) {
statPublishers.add(EIStatisticsPublisher.GetInstance());
}
if (publisherTypes.contains(MediationDataPublisherConstants.LOG_PUBLISHER_TYPE)) {
statPublishers.add(ElasticStatisticsPublisher.GetInstance());
}
if (publisherTypes.contains(MediationDataPublisherConstants.CHOREO_PUBLISHER_TYPE)) {
statPublishers.add(ChoreoStatisticsPublisher.GetInstance());
}
} else {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we optimize this if condition, something like:

        boolean useDefaultPublisher = true;
        if (publisherTypes != null && !publisherTypes.isEmpty()) {
            if (publisherTypes.contains(MediationDataPublisherConstants.DATABRIDGE_PUBLISHER_TYPE)) {
                statPublishers.add(EIStatisticsPublisher.GetInstance());
                useDefaultPublisher = false;
            }
            // Other publisherTypes go here
        }

        if (useDefaultPublisher) {
            statPublishers.add(ElasticStatisticsPublisher.GetInstance());
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimised as requested by 7ce08a2

Comment on lines 20 to 55
/*
* Documentation
*
* Enable Choreo Analytics
*
* 1. Add the following to the deployment.toml file.
*
* i. Enabling statistics for artifacts
* [mediation]
* flow.statistics.enable=true
* flow.statistics.capture_all=true
*
* ii. Enabling Choreo Analytics
* [analytics]
* enabled=true
* publisher="choreo"
*
* **Adding publisher is required. If not added, the default publisher will be used which is ELK flow**
*
* 2. Optionally, you can add the following to the deployment.toml file.
*
* [analytics]
* api_analytics.enabled = true
* proxy_service_analytics.enabled = true
* sequence_analytics.enabled = true
* endpoint_analytics.enabled = true
* inbound_endpoint_analytics.enabled = true
*
* Note that default value for above 5 will be true (If not added). If you want to disable analytics for any of the
* above, change the value to false.
*
* 3. Restart the server.
*
* Special Note - This is a WIP code so publishing to Choreo is not fully implemented.
*
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to a class comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to a class comment by 7ce08a2

Comment on lines 84 to 88
private boolean analyticsEnabledForAPI;
private boolean analyticsEnabledForSequences;
private boolean analyticsEnabledForProxyServices;
private boolean analyticsEnabledForEndpoints;
private boolean analyticsEnabledForInboundEndpoints;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we rename these as isApiAnalyticsEnabled, isSequenceAnalyticsEnabled and likewise?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed by 7ce08a2

*/
@Override
public void process(PublishingFlow publishingFlow, int tenantId) {
if (!analyticsEnabled) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we aren't setting the value of analyticsEnabled anywhere. Am I missing something here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In line 115 inside loadConfigurations(), we read the configurations. This will be the value of

[analytics]
enabled=true

of deployment.toml

if (!analyticsEnabled) {
return;
}
if (publishingFlow.getEvents().toArray().length > 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will publishingFlow.getEvents() return null by any chance? If so, let's do a null check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a null check by 7ce08a2

private JsonObject choreoPayload (PublishingEvent event) {
try {
ElasticMetadata metadata = event.getElasticMetadata();
JsonObject pL = new JsonObject();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename pL to payload

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed by 7ce08a2

Comment on lines 144 to 162
if ((StatisticsConstants.FLOW_STATISTICS_API.equals(event.getComponentType()) &&
analyticsEnabledForAPI) ||
(StatisticsConstants.FLOW_STATISTICS_SEQUENCE.equals(event.getComponentType()) &&
analyticsEnabledForSequences) ||
(StatisticsConstants.FLOW_STATISTICS_ENDPOINT.equals(event.getComponentType()) &&
analyticsEnabledForEndpoints) ||
(StatisticsConstants.FLOW_STATISTICS_INBOUNDENDPOINT.equals(event.getComponentType()) &&
analyticsEnabledForInboundEndpoints) ||
(StatisticsConstants.FLOW_STATISTICS_PROXYSERVICE.equals(event.getComponentType()) &&
analyticsEnabledForProxyServices)) {
if ((StatisticsConstants.FLOW_STATISTICS_SEQUENCE.equals(event.getComponentType()) &&
event.getElasticMetadata().getSequence(event.getComponentName()) == null) ||
(StatisticsConstants.FLOW_STATISTICS_ENDPOINT.equals(event.getComponentType()) &&
event.getElasticMetadata().getEndpoint(event.getComponentName()) == null)) {
// If Endpoint or Sequence is null
return;
}
publishAnalytics(event);
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we do early exits?
Something like:
for eg:

                if ((StatisticsConstants.FLOW_STATISTICS_API.equals(event.getComponentType()) &&
                                !analyticsEnabledForAPI) {
                    return;
                }
                // Similarly for PROXY and INBOUND_ENDPOINT

                if ((StatisticsConstants. FLOW_STATISTICS_SEQUENCE.equals(event.getComponentType()) {
                    if (!analyticsEnabledForAPI || 
                             event.getElasticMetadata().getSequence(event.getComponentName()) == null)) {
                        return;
                    }
                }
                // Similarly for ENDPOINT

                publishAnalytics(event);      
                

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed by 7ce08a2.

Since we have to publishing Analytics for only 5 artefacts, publishAnalytics(event) added inside each if cases.

* @param event This is the event published by synapse.
* @return This returns a JsonObject which contains the required flat Json by Choreo.
*/
private JsonObject choreoPayload (PublishingEvent event) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's rename this to generateChoreoPayload

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@piyumaldk piyumaldk mentioned this pull request Jul 20, 2023
2 tasks
@piyumaldk piyumaldk merged commit de2af40 into wso2:choreo-analytics Jul 24, 2023
2 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants