usergrid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From g...@apache.org
Subject [1/3] incubator-usergrid git commit: First cut of metrics for SSO.
Date Wed, 22 Apr 2015 20:05:20 GMT
Repository: incubator-usergrid
Updated Branches:
  refs/heads/master 0d1965d18 -> e7a543b4b


First cut of metrics for SSO.


Project: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/commit/4e42353c
Tree: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/tree/4e42353c
Diff: http://git-wip-us.apache.org/repos/asf/incubator-usergrid/diff/4e42353c

Branch: refs/heads/master
Commit: 4e42353c334665798978d8142fb0b4813e72901b
Parents: 2e90f39
Author: Dave Johnson <dmjohnson@apigee.com>
Authored: Tue Apr 21 16:31:50 2015 -0400
Committer: Dave Johnson <dmjohnson@apigee.com>
Committed: Tue Apr 21 16:31:50 2015 -0400

----------------------------------------------------------------------
 .../apache/usergrid/metrics/MetricsFactory.java | 30 +++++----
 .../rest/management/ManagementResource.java     | 64 +++++++++++++++++---
 2 files changed, 71 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4e42353c/stack/core/src/main/java/org/apache/usergrid/metrics/MetricsFactory.java
----------------------------------------------------------------------
diff --git a/stack/core/src/main/java/org/apache/usergrid/metrics/MetricsFactory.java b/stack/core/src/main/java/org/apache/usergrid/metrics/MetricsFactory.java
index e9888e9..9b98799 100644
--- a/stack/core/src/main/java/org/apache/usergrid/metrics/MetricsFactory.java
+++ b/stack/core/src/main/java/org/apache/usergrid/metrics/MetricsFactory.java
@@ -48,25 +48,29 @@ public class MetricsFactory {
 
     @PostConstruct
     void init() {
+
         registry = new MetricRegistry();
         String badHost = "badhost";
-        String metricsHost = properties.getProperty("usergrid.metrics.graphite.host", badHost);
-        Graphite graphite = new Graphite(new InetSocketAddress(metricsHost, 2003));
-        graphiteReporter = GraphiteReporter.forRegistry(registry)
-                .prefixedWith("notifications")
-                .convertRatesTo(TimeUnit.SECONDS)
-                .convertDurationsTo(TimeUnit.MILLISECONDS)
-                .filter(MetricFilter.ALL)
-                .build(graphite);
-        if(metricsHost!=badHost) {
-            graphiteReporter.start(30, TimeUnit.SECONDS);
-        }else {
-            LOG.warn("MetricsService:Logger not started.");
+        String metricsHost = properties.getProperty( "usergrid.metrics.graphite.host", badHost
);
+        Graphite graphite = new Graphite( new InetSocketAddress( metricsHost, 2003 ) );
+
+        graphiteReporter = GraphiteReporter.forRegistry( registry )
+                .prefixedWith( "notifications" )
+                .convertRatesTo( TimeUnit.SECONDS )
+                .convertDurationsTo( TimeUnit.MILLISECONDS )
+                .filter( MetricFilter.ALL )
+                .build( graphite );
+
+        if (metricsHost != badHost) {
+            graphiteReporter.start( 30, TimeUnit.SECONDS );
+            LOG.info("MetricsService: Reporter started.");
+        } else {
+            LOG.warn( "MetricsService: Reporter not started." );
             graphiteReporter.stop();
         }
         hashMap = new ConcurrentHashMap<String, Metric>();
 
-        jmxReporter = JmxReporter.forRegistry(registry).build();
+        jmxReporter = JmxReporter.forRegistry( registry ).build();
         jmxReporter.start();
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-usergrid/blob/4e42353c/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
----------------------------------------------------------------------
diff --git a/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
b/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
index 2bbcfd9..2e5f5e4 100644
--- a/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
+++ b/stack/rest/src/main/java/org/apache/usergrid/rest/management/ManagementResource.java
@@ -18,9 +18,7 @@ package org.apache.usergrid.rest.management;
 
 
 import java.net.URLEncoder;
-import java.util.Iterator;
-import java.util.Map;
-import java.util.UUID;
+import java.util.*;
 
 import javax.ws.rs.*;
 import javax.ws.rs.core.Context;
@@ -28,6 +26,8 @@ import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 
+import com.codahale.metrics.Counter;
+import com.codahale.metrics.Timer;
 import com.sun.jersey.api.client.Client;
 import com.sun.jersey.api.client.config.ClientConfig;
 import com.sun.jersey.api.client.config.DefaultClientConfig;
@@ -37,6 +37,7 @@ import org.apache.usergrid.exception.NotImplementedException;
 import org.apache.usergrid.management.ApplicationCreator;
 import org.apache.usergrid.management.OrganizationInfo;
 import org.apache.usergrid.management.OrganizationOwnerInfo;
+import org.apache.usergrid.metrics.MetricsFactory;
 import org.apache.usergrid.persistence.entities.User;
 import org.apache.usergrid.persistence.exceptions.EntityNotFoundException;
 import org.codehaus.jackson.JsonNode;
@@ -92,7 +93,9 @@ public class ManagementResource extends AbstractContextResource {
 
     /*-
      * New endpoints:
-     * 
+     *
+     * /management/externaltoken?ext_access_token=<token-from-central-usergrid>&ttl=<time-to-live>
+     *
      * /management/organizations/<organization-name>/applications
      * /management/organizations/<organization-name>/users
      * /management/organizations/<organization-name>/keys
@@ -105,10 +108,18 @@ public class ManagementResource extends AbstractContextResource {
     @Autowired
     private ApplicationCreator applicationCreator;
 
-    public static final String USERGRID_CENTRAL_URL = "usergrid.central.url";
+    @Autowired
+    MetricsFactory metricsFactory;
 
-    public static final String USERGRID_SYSADMIN_LOGIN_NAME = "usergrid.sysadmin.login.name";
+    // names for metrics to be collected
+    private static final String SSO_TOKENS_REJECTED = "sso_tokens_rejected";
+    private static final String SSO_TOKENS_VALIDATED = "sso_tokens_validated";
+    private static final String SSO_CREATED_LOCAL_ADMINS = "sso_created_local_admins";
+    private static final String SSO_PROCESSING_TIME = "sso_processing_time";
 
+    // usergrid configuration property names needed
+    public static final String USERGRID_CENTRAL_URL = "usergrid.central.url";
+    public static final String USERGRID_SYSADMIN_LOGIN_NAME = "usergrid.sysadmin.login.name";
     public static final String USERGRID_SYSADMIN_LOGIN_ALLOWED = "usergrid.sysadmin.login.allowed";
 
 
@@ -492,7 +503,7 @@ public class ManagementResource extends AbstractContextResource {
         }
         String extAccessToken = json.get("ext_access_token").toString();
 
-        Object ttlObj = json.get("ttl");
+        Object ttlObj = json.get( "ttl" );
         if ( ttlObj == null ) {
             throw new IllegalArgumentException("ttl must be specified");
         }
@@ -551,6 +562,11 @@ public class ManagementResource extends AbstractContextResource {
         }
         AccessInfo accessInfo = null;
 
+        Timer processingTimer = metricsFactory.getTimer(
+                ManagementResource.class, SSO_PROCESSING_TIME );
+
+        Timer.Context timerContext = processingTimer.time();
+
         try {
             // look up user via UG Central's /management/me endpoint.
 
@@ -573,7 +589,12 @@ public class ManagementResource extends AbstractContextResource {
                 String dummyPassword = RandomStringUtils.randomAlphanumeric( 40 );
 
                 JsonNode orgsNode = userNode.get( "organizations" );
-                final Iterator<String> fieldNames = orgsNode.getFieldNames();
+                Iterator<String> fieldNames = orgsNode.getFieldNames();
+                if ( !fieldNames.hasNext() ) {
+                    // no organizations for user exist in response from central Usergrid
SSO
+                    // so create user's personal organization and use username as organization
name
+                    fieldNames = Collections.singletonList( username ).iterator();
+                }
 
                 // create user and any organizations that user is supposed to have
 
@@ -593,6 +614,10 @@ public class ManagementResource extends AbstractContextResource {
                         userId = ownerOrgInfo.getOwner().getUuid();
                         userInfo = ownerOrgInfo.getOwner();
 
+                        Counter createdAdminsCounter = metricsFactory.getCounter(
+                                ManagementResource.class, SSO_CREATED_LOCAL_ADMINS );
+                        createdAdminsCounter.inc();
+
                     } else {
 
                         // already created user, so just create an org
@@ -614,11 +639,18 @@ public class ManagementResource extends AbstractContextResource {
                     .withAccessToken( extAccessToken );
 
         } catch (Exception e) {
+            timerContext.stop();
+            timerContext.close();
             logger.debug("Error validating external token", e);
             throw e;
         }
 
-        return Response.status( SC_OK ).type( jsonMediaType( callback ) ).entity( accessInfo
).build();
+        final Response response = Response.status( SC_OK ).type( jsonMediaType( callback
) ).entity( accessInfo ).build();
+
+        timerContext.stop();
+        timerContext.close();
+
+        return response;
     }
 
 
@@ -631,6 +663,13 @@ public class ManagementResource extends AbstractContextResource {
      */
     private JsonNode getMeFromUgCentral( String extAccessToken )  throws EntityNotFoundException
{
 
+        // prepare to count tokens validated and rejected
+
+        Counter tokensRejectedCounter = metricsFactory.getCounter(
+                ManagementResource.class, SSO_TOKENS_REJECTED );
+        Counter tokensValidatedCounter = metricsFactory.getCounter(
+                ManagementResource.class, SSO_TOKENS_VALIDATED );
+
         // create URL of central Usergrid's /management/me endpoint
 
         String externalUrl = properties.getProperty( USERGRID_CENTRAL_URL ).trim();
@@ -643,18 +682,23 @@ public class ManagementResource extends AbstractContextResource {
 
         ClientConfig clientConfig = new DefaultClientConfig();
         clientConfig.getFeatures().put( JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
-        Client client = Client.create(clientConfig);
+        Client client = Client.create( clientConfig );
+
         final JsonNode accessInfoNode;
         try {
             accessInfoNode = client.resource( me )
                     .type( MediaType.APPLICATION_JSON_TYPE)
                     .get(JsonNode.class);
 
+            tokensValidatedCounter.inc();
+
         } catch ( Exception e ) {
             // user not found 404
+            tokensRejectedCounter.inc();
             String msg = "Cannot find Admin User associated with " + extAccessToken;
             throw new EntityNotFoundException( msg, e );
         }
+
         return accessInfoNode;
     }
 


Mime
View raw message