Search in sources :

Example 1 with ReportSourceTag

use of wavefront.report.ReportSourceTag in project java by wavefrontHQ.

the class PostSourceTagTimedTask method drainBuffersToQueue.

public void drainBuffersToQueue() {
    try {
        isFlushingToQueue = true;
        while (sourceTags.size() > 0) {
            List<ReportSourceTag> tags = createAgentPostBatch();
            int dataCount = tags.size();
            if (dataCount > 0) {
                for (ReportSourceTag sourceTag : tags) {
                    switch(sourceTag.getSourceTagLiteral()) {
                        case "SourceDescription":
                            if (sourceTag.getAction().equals("delete")) {
                                agentAPI.removeDescription(sourceTag.getSource(), token, true);
                            } else {
                                agentAPI.setDescription(sourceTag.getSource(), token, sourceTag.getDescription(), true);
                            }
                            break;
                        case "SourceTag":
                            if (sourceTag.getAction().equals("delete")) {
                                // delete the source tag
                                agentAPI.removeTag(sourceTag.getSource(), token, sourceTag.getAnnotations().get(0), true);
                            } else {
                                // add the source tag
                                agentAPI.setTags(sourceTag.getSource(), token, sourceTag.getAnnotations(), true);
                            }
                            break;
                    }
                    this.sourceTagsAttempted.inc();
                    this.sourceTagsQueued.inc();
                    numApiCalls++;
                }
            }
        }
    } finally {
        isFlushingToQueue = true;
    }
}
Also used : ReportSourceTag(wavefront.report.ReportSourceTag)

Example 2 with ReportSourceTag

use of wavefront.report.ReportSourceTag in project java by wavefrontHQ.

the class SourceTagHandlerTest method testSourceTagsSetting.

/**
 * This test will add 3 source tags and verify that the server side api is called properly.
 *
 * @throws Exception
 */
@Test
public void testSourceTagsSetting() throws Exception {
    ReportSourceTag[] sourceTags = new ReportSourceTag[1];
    String[] annotations = new String[] { "tag1", "tag2", "tag3" };
    sourceTags[0] = new ReportSourceTag("SourceTag", "save", "dummy", "desc", Arrays.asList(annotations));
    EasyMock.expect(mockAgentAPI.setTags("dummy", StringUtils.EMPTY, Arrays.asList(annotations), false)).andReturn(Response.ok().build()).once();
    EasyMock.replay(mockAgentAPI);
    sourceTagHandler.reportSourceTags(Arrays.asList(sourceTags));
    TimeUnit.SECONDS.sleep(2);
    EasyMock.verify(mockAgentAPI);
}
Also used : ReportSourceTag(wavefront.report.ReportSourceTag) Test(org.junit.Test)

Example 3 with ReportSourceTag

use of wavefront.report.ReportSourceTag in project java by wavefrontHQ.

the class SourceTagDecoderTest method testSimpleSourceTagFormat.

@Test
public void testSimpleSourceTagFormat() throws Exception {
    SourceTagDecoder decoder = new SourceTagDecoder();
    List<ReportSourceTag> out = new ArrayList<>();
    // Testwith 3sourceTags
    decoder.decodeSourceTagLine(String.format("@%s %s=%s %s=aSource sourceTag1 sourceTag2 " + "sourceTag3", SOURCE_TAG, ReportSourceTagIngesterFormatter.ACTION, ReportSourceTagIngesterFormatter.ACTION_SAVE, ReportSourceTagIngesterFormatter.SOURCE), out);
    ReportSourceTag reportSourceTag = out.get(0);
    assertEquals("Action name didn't match.", ReportSourceTagIngesterFormatter.ACTION_SAVE, reportSourceTag.getAction());
    assertEquals("Source did not match.", "aSource", reportSourceTag.getSource());
    assertTrue("SourceTag1 did not match.", reportSourceTag.getAnnotations().contains("sourceTag1"));
    // Test with one sourceTag
    out.clear();
    decoder.decodeSourceTagLine(String.format("@%s action = %s source = \"A Source\" sourceTag3", SOURCE_TAG, ReportSourceTagIngesterFormatter.ACTION_SAVE), out);
    reportSourceTag = out.get(0);
    assertEquals("Action name didn't match.", ReportSourceTagIngesterFormatter.ACTION_SAVE, reportSourceTag.getAction());
    assertEquals("Source did not match.", "A Source", reportSourceTag.getSource());
    assertTrue("SourceTag3 did not match.", reportSourceTag.getAnnotations().contains("sourceTag3"));
    // Test with a multi-word source tag
    out.clear();
    decoder.decodeSourceTagLine(String.format("@%s action = %s source=aSource \"A source tag\" " + "\"Another tag\"", SOURCE_TAG, ReportSourceTagIngesterFormatter.ACTION_SAVE), out);
    reportSourceTag = out.get(0);
    assertEquals("Action name didn't match.", ReportSourceTagIngesterFormatter.ACTION_SAVE, reportSourceTag.getAction());
    assertEquals("Source did not match.", "aSource", reportSourceTag.getSource());
    assertTrue("'A source tag' did not match.", reportSourceTag.getAnnotations().contains("A source tag"));
    assertTrue("'Another tag' did not match", reportSourceTag.getAnnotations().contains("Another tag"));
    // Test sourceTag without any action -- this should result in an exception
    String msg = String.format("@%s source=aSource sourceTag4 sourceTag5", SOURCE_TAG);
    out.clear();
    boolean isException = false;
    try {
        decoder.decodeSourceTagLine(msg, out);
    } catch (Exception ex) {
        isException = true;
        logger.info(ex.getMessage());
    }
    assertTrue("Did not see an exception for SourceTag message without an action for input : " + msg, isException);
    // Test sourceTag without any source -- this should result in an exception
    msg = String.format("@%s action=save description=desc sourceTag5", SOURCE_TAG);
    out.clear();
    isException = false;
    try {
        decoder.decodeSourceTagLine(msg, out);
    } catch (Exception ex) {
        isException = true;
        logger.info(ex.getMessage());
    }
    assertTrue("Did not see an exception for SourceTag message without a source for input : " + msg, isException);
    // Test sourceTag with action, source, and description -- this should result in an exception
    out.clear();
    msg = String.format("@%s action = save source = aSource description = desc sourceTag5", SOURCE_TAG);
    isException = false;
    try {
        decoder.decodeSourceTagLine(msg, out);
    } catch (Exception ex) {
        isException = true;
        logger.info(ex.getMessage());
    }
    assertTrue("Did not see an exception when description was present in SourceTag message for " + "input : " + msg, isException);
    // Test sourceTag message without the source field -- should throw an exception
    out.clear();
    isException = false;
    msg = "@SourceTag action=remove sourceTag3";
    try {
        decoder.decodeSourceTagLine(msg, out);
    } catch (Exception ex) {
        isException = true;
        logger.info(ex.getMessage());
    }
    assertTrue("Did not see an exception when source field was absent for input: " + msg, isException);
    // Test a message where action is not save or delete -- should throw an exception
    out.clear();
    isException = false;
    msg = String.format("@%s action = anAction source=aSource sourceTag2 sourceTag3", SOURCE_TAG);
    try {
        decoder.decodeSourceTagLine(msg, out);
    } catch (Exception ex) {
        isException = true;
        logger.info(ex.getMessage());
    }
    assertTrue("Did not see an exception when action field was invalid for input : " + msg, isException);
}
Also used : ArrayList(java.util.ArrayList) ReportSourceTag(wavefront.report.ReportSourceTag) Test(org.junit.Test)

Example 4 with ReportSourceTag

use of wavefront.report.ReportSourceTag in project java by wavefrontHQ.

the class SourceTagDecoderTest method testSimpleSourceDescriptions.

@Test
public void testSimpleSourceDescriptions() throws Exception {
    SourceTagDecoder decoder = new SourceTagDecoder();
    List<ReportSourceTag> out = new ArrayList<>();
    // Testwith source description
    decoder.decodeSourceTagLine(String.format("@%s %s=%s %s= aSource description=desc", SOURCE_DESCRIPTION, ReportSourceTagIngesterFormatter.ACTION, ReportSourceTagIngesterFormatter.ACTION_SAVE, ReportSourceTagIngesterFormatter.SOURCE), out);
    ReportSourceTag reportSourceTag = out.get(0);
    assertEquals("Action name didn't match.", ReportSourceTagIngesterFormatter.ACTION_SAVE, reportSourceTag.getAction());
    assertEquals("Source did not match.", "aSource", reportSourceTag.getSource());
    assertEquals("Description did not match.", "desc", reportSourceTag.getDescription());
    // Test delete action where description field is not necessary
    out.clear();
    String format = String.format("@%s %s=%s %s=aSource", SOURCE_DESCRIPTION, ReportSourceTagIngesterFormatter.ACTION, ReportSourceTagIngesterFormatter.ACTION_DELETE, ReportSourceTagIngesterFormatter.SOURCE);
    decoder.decodeSourceTagLine(format, out);
    reportSourceTag = out.get(0);
    assertEquals("Action name did not match for input : " + format, ReportSourceTagIngesterFormatter.ACTION_DELETE, reportSourceTag.getAction());
    assertEquals("Source did not match for input : " + format, "aSource", reportSourceTag.getSource());
    // Add a source tag to the SourceDescription message -- this should cause an exception
    out.clear();
    String msg = String.format("@%s action = save source = aSource description = desc " + "sourceTag4", SOURCE_DESCRIPTION);
    boolean isException = false;
    try {
        decoder.decodeSourceTagLine(msg, out);
    } catch (Exception ex) {
        isException = true;
        logger.info(ex.getMessage());
    }
    assertTrue("Expected an exception, since source tag was supplied in SourceDescription " + "message for input : " + msg, isException);
}
Also used : ArrayList(java.util.ArrayList) ReportSourceTag(wavefront.report.ReportSourceTag) Test(org.junit.Test)

Example 5 with ReportSourceTag

use of wavefront.report.ReportSourceTag in project java by wavefrontHQ.

the class ReportSourceTagIngesterFormatter method drive.

/**
 * This method can be used to parse the input line into a ReportSourceTag object.
 *
 * @return The parsed ReportSourceTag object.
 */
@Override
public ReportSourceTag drive(String input, String defaultHostName, String customerId, List<String> customerSourceTags) {
    Queue<Token> queue = getQueue(input);
    ReportSourceTag sourceTag = new ReportSourceTag();
    ReportSourceTagWrapper wrapper = new ReportSourceTagWrapper(sourceTag);
    try {
        for (FormatterElement element : elements) {
            element.consume(queue, wrapper);
        }
    } catch (Exception ex) {
        throw new RuntimeException("Could not parse: " + input, ex);
    }
    if (!queue.isEmpty()) {
        throw new RuntimeException("Could not parse: " + input);
    }
    // verify the values - especially 'action' field
    if (sourceTag.getSource() == null)
        throw new RuntimeException("No source key was present in the input: " + input);
    if (sourceTag.getAction() != null) {
        // verify that only 'add' or 'delete' is present
        String actionStr = sourceTag.getAction();
        if (!actionStr.equals(ACTION_SAVE) && !actionStr.equals(ACTION_DELETE))
            throw new RuntimeException("Action string did not match save/delete: " + input);
    } else {
        // no value was specified hence throw an exception
        throw new RuntimeException("No action key was present in the input: " + input);
    }
    return ReportSourceTag.newBuilder(sourceTag).build();
}
Also used : Token(org.antlr.v4.runtime.Token) ReportSourceTag(wavefront.report.ReportSourceTag)

Aggregations

ReportSourceTag (wavefront.report.ReportSourceTag)8 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 TimerContext (com.yammer.metrics.core.TimerContext)1 Response (javax.ws.rs.core.Response)1 Token (org.antlr.v4.runtime.Token)1