@Test
  public void testLocateExternalResourceNameWhenEnabledWithSqlAndDisabledApp() {
    assertInitialState();

    // enable query external resources creation
    registry.set(CollectionSettingNames.CS_QUERY_EXRTERNAL_RESOURCE_NAME, Boolean.TRUE);

    Operation op = createJdbcOperation("jdbc:foobar://huh:8080");
    op.type(JdbcOperationExternalResourceAnalyzer.TYPE);

    Frame frame = createJdbcFrame(op);
    Trace trace = createJdbcTrace(frame);

    registry.set(
        CollectionSettingNames.createApplicationCollectionSettingName(trace.getAppName()),
        Boolean.FALSE);

    Collection<ExternalResourceDescriptor> resources = locateExternalResourceName(trace);

    // make sure that no query external resources were created
    assertNotNull("external resource descriptors list", resources);
    assertEquals("total external resource descriptors", 1, resources.size());

    ExternalResourceDescriptor firstDescriptor = ListUtil.getFirstMember(resources);
    ExternalResourceType type = ExternalResourceType.valueOf(firstDescriptor.getType());
    assertSame("first descriptor type", ExternalResourceType.DATABASE, type);
    assertFalse("first descriptor is a parent", firstDescriptor.isParent());

    assertTrue(
        trace.getAppName() + "is known", generator.isApplicationNameKnown(trace.getAppName()));
  }
  private void assertQueryExternalResources(Trace trace, String sql) {
    Collection<ExternalResourceDescriptor> resources = locateExternalResourceName(trace);

    assertNotNull("external resource descriptors list", resources);
    assertEquals("total external resource descriptors", 2, resources.size());

    assertTrue(
        trace.getAppName() + "is known", generator.isApplicationNameKnown(trace.getAppName()));

    int totalDbs = 0;
    int totalQuerys = 0;

    for (ExternalResourceDescriptor desc : resources) {
      ExternalResourceType type = ExternalResourceType.valueOf(desc.getType());

      switch (type) {
        case DATABASE:
          totalDbs++;
          break;
        case QUERY:
          assertQueryDescriptor(desc, sql);
          totalQuerys++;
          break;
        default:
          assertNotSame(
              "Unexpected external resource type (should be QUERY or DATABASE)",
              ExternalResourceType.OTHER,
              type);
      }
    }

    assertEquals("Total db external resource descriptors", 1, totalDbs);
    assertEquals("Total query external resource descriptors", 1, totalQuerys);
  }
  public List<ExternalResourceDescriptor> locateExternalResourceName(Trace trace) {
    Collection<Frame> dbFrames = trace.getLastFramesOfType(TYPE);
    if ((dbFrames == null) || dbFrames.isEmpty()) {
      return Collections.emptyList();
    }

    List<ExternalResourceDescriptor> dbDescriptors =
        new ArrayList<ExternalResourceDescriptor>(dbFrames.size());

    for (Frame dbFrame : dbFrames) {
      Operation op = dbFrame.getOperation();
      String host = op.get("host", String.class);
      Integer portProperty = op.get("port", Integer.class);
      int port = portProperty == null ? -1 : portProperty.intValue();

      String dbName = op.get("dbName", String.class);

      String redisHash = MD5NameGenerator.getName(dbName + host + port);

      dbDescriptors.add(
          new ExternalResourceDescriptor(
              dbFrame,
              "redis:" + redisHash,
              dbName,
              ExternalResourceType.DATABASE.name(),
              "Redis",
              host,
              port));
    }

    return dbDescriptors;
  }
 public Set<FrameId> excludeFrames(Trace trace) {
   Set<Frame> frames = new HashSet<Frame>();
   for (Frame topLevelFrame : topLevelFramesOfType(trace.getRootFrame(), TYPE)) {
     frames.addAll(descendantFramesOfType(topLevelFrame, TYPE));
   }
   return frameIDs(frames);
 }
 @Test
 public void locateEndPointNoHttp() {
   FrameBuilder b = new SimpleFrameBuilder();
   b.enter(new Operation());
   b.enter(grailsOp);
   Frame grailsFrame = b.exit();
   assertNotNull("No grails frame", grailsFrame);
   Frame rootFrame = b.exit();
   Trace trace = Trace.newInstance(app, TraceId.valueOf("0"), rootFrame);
   assertEndpointAnalysis(trace, grailsFrame, null);
 }
 @Test
 public void locateEndPointHttpComesAfterGrails() {
   FrameBuilder b = new SimpleFrameBuilder();
   Operation httpOp = new Operation().type(OperationType.HTTP);
   b.enter(httpOp);
   b.enter(grailsOp);
   Frame grailsFrame = b.exit();
   assertNotNull("No grails frame", grailsFrame);
   Frame httpFrame = b.exit();
   Trace trace = Trace.newInstance(app, TraceId.valueOf("0"), httpFrame);
   assertEndpointAnalysis(trace, grailsFrame, httpFrame);
 }
 @Test
 public void locateEndPointNoGrails() {
   FrameBuilder b = new SimpleFrameBuilder();
   Operation httpOp = new Operation().type(OperationType.HTTP);
   b.enter(httpOp);
   b.enter(new Operation());
   Frame simpleFrame = b.exit();
   assertNotNull("No simple frame", simpleFrame);
   Frame httpFrame = b.exit();
   Trace trace = Trace.newInstance(app, TraceId.valueOf("0"), httpFrame);
   EndPointAnalysis ep = endPointAnalyzer.locateEndPoint(trace);
   assertNull("Unexpected result: " + ep, ep);
 }
  @Test
  public void testLocateExternalResourceNameWhenDisabled() {
    assertInitialState();

    Operation op = createJdbcOperation("jdbc:foobar://huh:8080");
    op.type(JdbcOperationExternalResourceAnalyzer.TYPE);

    Frame frame = createJdbcFrame(op);
    Trace trace = createJdbcTrace(frame);

    Collection<ExternalResourceDescriptor> resources = locateExternalResourceName(trace);

    // make sure that no query external resources were created
    assertNotNull("external resource descriptors list", resources);
    assertEquals("total external resource descriptors", 1, resources.size());

    ExternalResourceDescriptor firstDescriptor = ListUtil.getFirstMember(resources);
    ExternalResourceType type = ExternalResourceType.valueOf(firstDescriptor.getType());
    assertSame("first descriptor type", ExternalResourceType.DATABASE, type);
    assertFalse("first descriptor is a parent", firstDescriptor.isParent());

    assertTrue(
        trace.getAppName() + " is known", generator.isApplicationNameKnown(trace.getAppName()));
  }
 public Set<FrameId> includeFrames(Trace trace) {
   Set<Frame> frames = topLevelFramesOfType(trace.getRootFrame(), TYPE);
   return frameIDs(frames);
 }