Skip to content

Commit

Permalink
Merge pull request wso2#103 from wso2/yasas-master
Browse files Browse the repository at this point in the history
Add custom analytics data provider sample
  • Loading branch information
AnuGayan authored Jul 29, 2022
2 parents af64359 + 2da8f88 commit b09e343
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 0 deletions.
78 changes: 78 additions & 0 deletions analytics-custom-data-provider/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Custom Data Provider for API-M 4.2.0

This sample will allow you to add custom analytics data to the existing event schema.

__Steps to add custom analytics data:__

Add the necessary component versions for `carbon.apimgt.version`, and `synapse.version` in pom file in the root directory.

Build the project using Maven:

mvn clean install


Copy the generated JAR file from the target folder and place it in `<WSO2AM-4.2.0-HOME>/repository/components/lib`.

Edit apim.analytics configurations in the `deployment.toml` located inside `<WSO2AM-4.2.0-HOME>/repository/conf` with the
following configuration.

[apim.analytics]
enable = true
properties."publisher.custom.data.provider.class" = "org.wso2.carbon.apimgt.gateway.sample.publisher.CustomDataProvider"



Once this is successfully deployed you can check whether this is working by following the below steps:
1. [Enable trace logs](https://apim.docs.wso2.com/en/4.2.0/administer/logging-and-monitoring/logging/configuring-logging/#enabling-logs-for-a-component) for the component: `org.wso2.am.analytics.publisher`
1. Follow this sample configurations,

logger.org-wso2-analytics-publisher.name = org.wso2.am.analytics.publisher
logger.org-wso2-analytics-publisher.level = TRACE
logger.org-wso2-analytics-publisher.appenderRef.CARBON_TRACE_LOGFILE.ref = CARBON_TRACE_LOGFILE

1. Then append logger name to the `loggers`

loggers = org-wso2-analytics-publisher, trace-messages,

1. Now you can trigger an event and check the` <WSO2AM-4.2.0-HOME>/repository/logs/wso2carbon-trace-messages.log` to find the event object passed out from API Manager.

TRACE {org.wso2.am.analytics.publisher.client.EventHubClient} - [{ Cloud-Analytics-Queue-Worker-pool-2-thread-1 }] -
Adding event:
{
"apiName":"API1",
"proxyResponseCode":200,
"destination":"https://run.mocky.io/v3/d14fad1d-d57b-41bc-8be3-146b6aaddfaf",
"apiCreatorTenantDomain":"carbon.super",
"platform":"Linux",
"apiMethod":"GET",
"apiVersion":"2.0.0",
"gatewayType":"SYNAPSE",
"apiCreator":"admin",
"responseCacheHit":false,
"backendLatency":1866,
"correlationId":"00a94b54-5579-4f5d-95c1-4bd909fd4c20",
"requestMediationLatency":578,
"keyType":"SANDBOX",
"apiId":"fd5a22ee-144f-4fc1-9f22-ce5ff0382023",
"applicationName":"AppUser",
"targetResponseCode":200,
"requestTimestamp":"2022-07-18T06:49:19Z",
"applicationOwner":"admin",
"userAgent":"Chrome",
"eventType":"response",
"apiResourceTemplate":"/test",
"properties":{
"tokenIssuer":"https://localhost:9443/oauth2/token",
"apiContext":"/api1/2.0.0",
"userName":"[email protected]"
},
"responseLatency":2446,
"regionId":"default",
"responseMediationLatency":2,
"userIp":"127.0.0.1",
"applicationId":"5d6d0135-810a-4b4e-8f9a-45187e943fff",
"apiType":"HTTP"
}



113 changes: 113 additions & 0 deletions analytics-custom-data-provider/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
~
~ WSO2 Inc. licenses this file to you under the Apache License,
~ Version 2.0 (the "License"); you may not use this file except
~ in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>org.wso2.carbon.apimgt.gateway.sample.publisher</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>bundle</packaging>

<dependencies>
<dependency>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>org.wso2.carbon.apimgt.gateway</artifactId>
<version>${carbon.apimgt.version}</version>
</dependency>
<dependency>
<groupId>org.wso2.carbon.apimgt</groupId>
<artifactId>org.wso2.carbon.apimgt.common.analytics</artifactId>
<version>${carbon.apimgt.version}</version>
</dependency>
<dependency>
<groupId>org.apache.synapse</groupId>
<artifactId>synapse-extensions</artifactId>
<version>${synapse.version}</version>
</dependency>
<dependency>
<groupId>org.apache.synapse</groupId>
<artifactId>synapse-core</artifactId>
<version>${synapse.version}</version>
<exclusions>
<exclusion>
<groupId>org.apache.axis2</groupId>
<artifactId>axis2-codegen</artifactId>
</exclusion>
<exclusion>
<groupId>org.wso2.orbit.com.github.fge</groupId>
<artifactId>json-schema-validator-all</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

<repositories>
<repository>
<id>wso2-nexus</id>
<name>WSO2 internal Repository</name>
<url>https://maven.wso2.org/nexus/content/groups/wso2-public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
<checksumPolicy>ignore</checksumPolicy>
</releases>
</repository>
</repositories>

<build>
<plugins>
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<extensions>true</extensions>
<configuration>
<instructions>
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
<Bundle-Name>${project.artifactId}</Bundle-Name>
<Export-Package>
org.wso2.carbon.apimgt.gateway.sample.publisher.*;version="${project.version}"
</Export-Package>
<Private-Package>
</Private-Package>
<Import-Package>
org.apache.synapse.*,
org.apache.commons.logging.*,
org.wso2.carbon.apimgt.common.analytics.*;version=${carbon.apimgt.version},
org.wso2.carbon.apimgt.gateway.*;version=${carbon.apimgt.version},
*;resolution:=optional
</Import-Package>
<DynamicImport-Package>*</DynamicImport-Package>
</instructions>
</configuration>
</plugin>
</plugins>
</build>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<!-- org.wso2.carbon.apimgt component version-->
<carbon.apimgt.version>9.22.27-SNAPSHOT</carbon.apimgt.version>
<!-- synapse-core version-->
<synapse.version>2.1.7-wso2v289</synapse.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright (c) 2022, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package org.wso2.carbon.apimgt.gateway.sample.publisher;

import io.netty.channel.ChannelHandlerContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.synapse.MessageContext;
import org.wso2.carbon.apimgt.common.analytics.collectors.AnalyticsCustomDataProvider;
import org.wso2.carbon.apimgt.gateway.handlers.security.AuthenticationContext;
import org.wso2.carbon.apimgt.gateway.handlers.streaming.websocket.WebSocketUtils;

import java.util.HashMap;
import java.util.Map;

public class CustomDataProvider implements AnalyticsCustomDataProvider {

private static final Log log = LogFactory.getLog(CustomDataProvider.class);
private static final String SYNAPSE_TOKEN_ISSUER = "issuer";
private static final String ASYNC_AUTH_CONTEXT = "__API_AUTH_CONTEXT";

public CustomDataProvider() {
log.info("Successfully initialized");
}

@Override public Map<String, Object> getCustomProperties(Object context) {
if (context instanceof MessageContext) {
Map<String, Object> customProperties = new HashMap<>();
customProperties.put("tokenIssuer", getSynapseTokenIssuer((MessageContext) context));
return customProperties;
}
if (context instanceof ChannelHandlerContext) {
Map<String, Object> customProperties = new HashMap<>();
customProperties.put("tokenIssuer", getAsyncTokenIssuer((ChannelHandlerContext) context));
return customProperties;
}
return null;
}

private String getSynapseTokenIssuer(MessageContext context) {

if (context.getPropertyKeySet().contains(SYNAPSE_TOKEN_ISSUER)) {
return (String) context.getProperty(SYNAPSE_TOKEN_ISSUER);
}
return null;
}

private String getAsyncTokenIssuer(ChannelHandlerContext context) {

Object authContext = WebSocketUtils.getPropertyFromChannel(ASYNC_AUTH_CONTEXT, context);
if (authContext != null && authContext instanceof AuthenticationContext) {
return ((AuthenticationContext)authContext).getIssuer();
}
return null;
}

}

0 comments on commit b09e343

Please sign in to comment.