/** * {@inheritDoc} */ @Override public String toString() { String sup = super.toString(); return sup + "HttpTimerData [uri=" + (null != httpInfo ? httpInfo.getUri() : HttpInfo.UNDEFINED) + ", parameters=" + parameters + ", attributes=" + attributes + ", headers=" + headers + "]"; } }
/** * {@inheritDoc} */ @Override public String[] getStringValues(InvocationSequenceData invocSequence, ICachedDataService cachedDataService) { if (!InvocationSequenceDataHelper.hasHttpTimerData(invocSequence)) { return new String[0]; } HttpTimerData httpData = (HttpTimerData) invocSequence.getTimerData(); return new String[] { httpData.getHttpInfo().getUri() }; } }
/** * {@inheritDoc} */ @Override protected void addTags(HttpTimerData data, Builder builder) { super.addTags(data, builder); HttpInfo httpInfo = data.getHttpInfo(); if (null != httpInfo.getUri()) { builder.tag(Series.Http.TAG_URI, httpInfo.getUri()); } if (httpInfo.hasInspectItTaggingHeader()) { builder.tag(Series.Http.TAG_INSPECTIT_TAGGING_HEADER, httpInfo.getInspectItTaggingHeaderValue()); } }
/** * Default constructor. * * @param timerData * the timerData that is taken into account */ public DiagnosisTimerData(final TimerData timerData) { if (timerData == null) { throw new IllegalArgumentException("TimerData cannot be null!"); } this.duration = timerData.getDuration(); this.cpuDuration = timerData.getCpuDuration(); this.exclusiveDuration = timerData.getExclusiveDuration(); if (timerData instanceof SqlStatementData) { SqlStatementData sqlStatementData = (SqlStatementData) timerData; this.metaData.put(MetaDataType.SQL, sqlStatementData.getSql()); } else if (timerData instanceof HttpTimerData) { HttpTimerData httpTimerData = (HttpTimerData) timerData; this.metaData.put(MetaDataType.URI, httpTimerData.getHttpInfo().getUri()); } }
/** * Finds matching template for the given {@link HttpTimerData} by uri. * * @param httpTimerData * Data to find matching template. * @param templates * List of templates to search. * @param checkOnlyUri * If matching should be done only by uri. * @return Matching template of <code>null</code> if one can not be found. */ private HttpTimerData findTemplateForUriData(HttpTimerData httpTimerData, List<HttpTimerData> templates, boolean checkOnlyUri) { for (HttpTimerData template : templates) { if (Objects.equals(template.getHttpInfo().getUri(), httpTimerData.getHttpInfo().getUri())) { if (!checkOnlyUri && HttpTimerData.REQUEST_METHOD_MULTIPLE.equals(template.getHttpInfo().getRequestMethod())) { return template; } else if (Objects.equals(template.getHttpInfo().getRequestMethod(), httpTimerData.getHttpInfo().getRequestMethod())) { return template; } } } return null; }
/** * {@inheritDoc} */ @Override public Object getAggregationKey(HttpTimerData httpData) { final int prime = 31; int result = 0; if (uriBased) { result = (prime * result) + ((httpData.getHttpInfo().getUri() == null) ? 0 : httpData.getHttpInfo().getUri().hashCode()); } else { result = (prime * result) + ((httpData.getHttpInfo().getInspectItTaggingHeaderValue() == null) ? 0 : httpData.getHttpInfo().getInspectItTaggingHeaderValue().hashCode()); } if (includeRequestMethod) { result = (prime * result) + ((httpData.getHttpInfo().getRequestMethod() == null) ? 0 : httpData.getHttpInfo().getRequestMethod().hashCode()); } return result; }
/** * Returns transformed URI. * * @param object * {@link HttpTimerData} * @param httpSensorTypeIdent * Sensor that holds the regEx expression and the template. * @return Transformed URI or URI if regular expression is not provided or can not be compiled. */ public static String getTransformedUri(HttpTimerData object, MethodSensorTypeIdent httpSensorTypeIdent) { try { if ((null != httpSensorTypeIdent) && (null != MethodSensorTypeIdentHelper.getRegEx(httpSensorTypeIdent))) { return HttpTimerDataHelper.getTransformedUri(object, MethodSensorTypeIdentHelper.getRegEx(httpSensorTypeIdent), MethodSensorTypeIdentHelper.getRegExTemplate(httpSensorTypeIdent)); } else { return object.getHttpInfo().getUri(); } } catch (IllegalArgumentException e) { return object.getHttpInfo().getUri(); } }
/** * {@inheritDoc} */ @Override public List<HttpTimerData> getChartingHttpTimerDataFromDateToDate(Collection<HttpTimerData> templates, Date fromDate, Date toDate, boolean retrieveByTag) { if (CollectionUtils.isNotEmpty(templates)) { StorageIndexQuery query = httpDataQueryFactory.getFindAllHttpTimersQuery(templates.iterator().next(), fromDate, toDate); if (!retrieveByTag) { Set<String> uris = new HashSet<>(); for (HttpTimerData httpTimerData : templates) { if (!HttpInfo.UNDEFINED.equals(httpTimerData.getHttpInfo().getUri())) { uris.add(httpTimerData.getHttpInfo().getUri()); } } query.addIndexingRestriction(IndexQueryRestrictionFactory.isInCollection("uri", uris)); } else { Set<String> tags = new HashSet<>(); for (HttpTimerData httpTimerData : templates) { if (httpTimerData.getHttpInfo().hasInspectItTaggingHeader()) { tags.add(httpTimerData.getHttpInfo().getInspectItTaggingHeaderValue()); } } query.addIndexingRestriction(IndexQueryRestrictionFactory.isInCollection("inspectItTaggingHeaderValue", tags)); } return super.executeQuery(query, TIMESTAMP_COMPARATOR); } else { return Collections.emptyList(); } }
@Test public void noUri() throws Exception { when(cachedDataService.getPlatformIdentForId(PLATFORM_ID)).thenReturn(null); long time = RandomUtils.nextLong(); when(data.getPlatformIdent()).thenReturn(PLATFORM_ID); when(data.getTimeStamp()).thenReturn(new Timestamp(time)); when(data.getHttpInfo()).thenReturn(httpInfo); when(httpInfo.getUri()).thenReturn(null); when(httpInfo.hasInspectItTaggingHeader()).thenReturn(false); when(httpInfo.getInspectItTaggingHeaderValue()).thenReturn(null); Collection<Builder> pointBuilderCol = builder.createBuilders(data); assertThat(pointBuilderCol.size(), is(1)); Builder pointBuilder = pointBuilderCol.iterator().next(); assertThat(getMeasurement(pointBuilder), is(Series.Http.NAME)); assertThat(getTime(pointBuilder), is(time)); assertThat(getPrecision(pointBuilder), is(TimeUnit.MILLISECONDS)); assertThat(getTags(pointBuilder), not(hasKey(Series.Http.TAG_URI))); assertThat(getFields(pointBuilder), hasEntry(Series.Http.FIELD_DURATION, (Object) data.getDuration())); }
@Override public boolean matches(Object object) { if (!HttpTimerData.class.isInstance(object)) { return false; } HttpTimerData other = (HttpTimerData) object; assertThat(data.getHttpInfo().getUri(), is(equalTo(other.getHttpInfo().getUri()))); assertThat(data.getHttpInfo().getRequestMethod(), is(equalTo(other.getHttpInfo().getRequestMethod()))); assertThat(data.getAttributes(), is(equalTo(other.getAttributes()))); assertThat(data.getHeaders(), is(equalTo(other.getHeaders()))); assertThat(data.getSessionAttributes(), is(equalTo(other.getSessionAttributes()))); assertThat(data.getParameters(), is(equalTo(other.getParameters()))); assertThat(data.isCharting(), is(equalTo(other.isCharting()))); assertThat(data.getHttpResponseStatus(), is(equalTo(other.getHttpResponseStatus()))); return true; } }
@Test public void noPlatform() throws Exception { when(cachedDataService.getPlatformIdentForId(PLATFORM_ID)).thenReturn(null); long time = RandomUtils.nextLong(); when(data.getPlatformIdent()).thenReturn(PLATFORM_ID); when(data.getTimeStamp()).thenReturn(new Timestamp(time)); when(data.getHttpInfo()).thenReturn(httpInfo); when(httpInfo.getUri()).thenReturn(URI); when(httpInfo.hasInspectItTaggingHeader()).thenReturn(true); when(httpInfo.getInspectItTaggingHeaderValue()).thenReturn(TAG); Collection<Builder> pointBuilderCol = builder.createBuilders(data); assertThat(pointBuilderCol.size(), is(1)); Builder pointBuilder = pointBuilderCol.iterator().next(); assertThat(getMeasurement(pointBuilder), is(Series.Http.NAME)); assertThat(getTime(pointBuilder), is(time)); assertThat(getPrecision(pointBuilder), is(TimeUnit.MILLISECONDS)); assertThat(getTags(pointBuilder), hasEntry(Series.TAG_AGENT_ID, String.valueOf(PLATFORM_ID))); assertThat(getTags(pointBuilder), not(hasKey(Series.TAG_AGENT_NAME))); assertThat(getFields(pointBuilder), hasEntry(Series.Http.FIELD_DURATION, (Object) data.getDuration())); }
@Test public void noTag() throws Exception { when(cachedDataService.getPlatformIdentForId(PLATFORM_ID)).thenReturn(platformIdent); long time = RandomUtils.nextLong(); when(data.getPlatformIdent()).thenReturn(PLATFORM_ID); when(data.getTimeStamp()).thenReturn(new Timestamp(time)); when(data.getHttpInfo()).thenReturn(httpInfo); when(httpInfo.getUri()).thenReturn(URI); when(httpInfo.hasInspectItTaggingHeader()).thenReturn(false); when(httpInfo.getInspectItTaggingHeaderValue()).thenReturn(null); Collection<Builder> pointBuilderCol = builder.createBuilders(data); assertThat(pointBuilderCol.size(), is(1)); Builder pointBuilder = pointBuilderCol.iterator().next(); assertThat(getMeasurement(pointBuilder), is(Series.Http.NAME)); assertThat(getTime(pointBuilder), is(time)); assertThat(getPrecision(pointBuilder), is(TimeUnit.MILLISECONDS)); assertThat(getTags(pointBuilder), not(hasKey(Series.Http.TAG_INSPECTIT_TAGGING_HEADER))); assertThat(getFields(pointBuilder), hasEntry(Series.Http.FIELD_DURATION, (Object) data.getDuration())); }
@Test private void musteReturnAnObjectWithAPairLongStringIfTheTimerDataHasHttpTimerData() { InvocationSequenceData invocationSequenceData = new InvocationSequenceData(new Timestamp(10L), 10L, 20L, 108L); HttpTimerData timerData = new HttpTimerData(new Timestamp(10), 10, 10, 108L); HttpInfo httpInfo = new HttpInfo("URI", "requestMethod", "headerValue"); timerData.setHttpInfo(httpInfo); invocationSequenceData.setTimerData(timerData); Pair<Long, String> aggregationKey = (Pair<Long, String>) DiagnosisDataAggregator.getInstance().getAggregationKey(invocationSequenceData); assertThat("The string of the pair must be the sql data", aggregationKey.getSecond(), is(timerData.getHttpInfo().getUri())); }
@Test public void happyPath() throws Exception { when(cachedDataService.getPlatformIdentForId(PLATFORM_ID)).thenReturn(platformIdent); long time = RandomUtils.nextLong(); when(data.getPlatformIdent()).thenReturn(PLATFORM_ID); when(data.getTimeStamp()).thenReturn(new Timestamp(time)); when(data.getDuration()).thenReturn(RandomUtils.nextDouble()); when(data.getHttpInfo()).thenReturn(httpInfo); when(httpInfo.getUri()).thenReturn(URI); when(httpInfo.hasInspectItTaggingHeader()).thenReturn(true); when(httpInfo.getInspectItTaggingHeaderValue()).thenReturn(TAG); Collection<Builder> pointBuilderCol = builder.createBuilders(data); assertThat(pointBuilderCol.size(), is(1)); Builder pointBuilder = pointBuilderCol.iterator().next(); assertThat(getMeasurement(pointBuilder), is(Series.Http.NAME)); assertThat(getTime(pointBuilder), is(time)); assertThat(getPrecision(pointBuilder), is(TimeUnit.MILLISECONDS)); assertThat(getTags(pointBuilder), hasEntry(Series.TAG_AGENT_ID, String.valueOf(PLATFORM_ID))); assertThat(getTags(pointBuilder), hasEntry(Series.TAG_AGENT_NAME, String.valueOf(AGENT_NAME))); assertThat(getTags(pointBuilder), hasEntry(Series.Http.TAG_URI, String.valueOf(URI))); assertThat(getTags(pointBuilder), hasEntry(Series.Http.TAG_INSPECTIT_TAGGING_HEADER, String.valueOf(TAG))); assertThat(getFields(pointBuilder), hasEntry(Series.Http.FIELD_DURATION, (Object) data.getDuration())); }
/** * {@inheritDoc} */ @Override public int compare(HttpTimerData o1, HttpTimerData o2) { switch (this) { case URI: return ObjectUtils.compare(o1.getHttpInfo().getUri(), o2.getHttpInfo().getUri()); case HTTP_METHOD: return ObjectUtils.compare(o1.getHttpInfo().getRequestMethod(), o2.getHttpInfo().getRequestMethod()); case TAG_VALUE: return ObjectUtils.compare(o1.getHttpInfo().getInspectItTaggingHeaderValue(), o2.getHttpInfo().getInspectItTaggingHeaderValue()); default: return 0; } }
@Test private void ifTheAggregatedObjectHasHttpTimerDataAndIsDefinedInTheMapItMustBeAggregatedToTheDiagnosisAggregator() { InvocationSequenceData invocationSequenceData = new InvocationSequenceData(new Timestamp(10L), 10L, 20L, 2L); diagnosisDataAggregationPerformer = new DiagnosisDataAggregationPerformer(); HttpTimerData timerData = new HttpTimerData(new Timestamp(10), 10, 10, 108L); HttpInfo httpInfo = new HttpInfo("URI", "requestMethod", "headerValue"); timerData.setHttpInfo(httpInfo); invocationSequenceData.setTimerData(timerData); Object key = new Pair<Long, String>(invocationSequenceData.getMethodIdent(), ((HttpTimerData) invocationSequenceData.getTimerData()).getHttpInfo().getUri()); diagnosisDataAggregationPerformer.diagnosisDataAggregationMap.put(key, alreadyAggregatedObject); diagnosisDataAggregationPerformer.aggregateInvocationSequenceData(invocationSequenceData); verify(alreadyAggregatedObject, times(1)).aggregate(invocationSequenceData); } }
@Test public void aggregationURI() { final HttpTimerData data = new HttpTimerData(); data.getHttpInfo().setUri("URI"); data.getHttpInfo().setRequestMethod("GET"); data.setDuration(100d); data.setCpuDuration(10d); data.calculateCpuMin(10d); data.calculateCpuMax(10d); data.setCpuDuration(10d); final HttpTimerData data2 = new HttpTimerData(); data2.getHttpInfo().setUri("URI"); data2.getHttpInfo().setRequestMethod("POST"); data2.setDuration(500d); data2.calculateCpuMin(20d); data2.calculateCpuMax(20d); data2.setCpuDuration(20d); final List<HttpTimerData> input = new ArrayList<>(); Collections.addAll(input, data, data2); AggregationPerformer<HttpTimerData> aggregationPerformer = new AggregationPerformer<>(new HttpTimerDataAggregator(true, false)); aggregationPerformer.processCollection(input); final List<HttpTimerData> output = aggregationPerformer.getResultList(); assertThat(output, is(notNullValue())); assertThat(output.size(), is(equalTo(1))); HttpTimerData result = output.get(0); assertThat(result.getHttpInfo().getUri(), is(equalTo("URI"))); assertThat(result.getHttpInfo().hasInspectItTaggingHeader(), is(equalTo(false))); assertThat(result.getDuration(), is(equalTo(600d))); assertThat(result.getCpuDuration(), is(equalTo(30d))); }
/** * Get key for aggregation. * * @param invocationSequenceData * invocationSequenceData to key should be determined * @return key as object */ public Object getAggregationKey(InvocationSequenceData invocationSequenceData) { if (InvocationSequenceDataHelper.hasSQLData(invocationSequenceData)) { return new Pair<Long, String>(invocationSequenceData.getMethodIdent(), invocationSequenceData.getSqlStatementData().getSql()); } else if (InvocationSequenceDataHelper.hasHttpTimerData(invocationSequenceData)) { return new Pair<Long, String>(invocationSequenceData.getMethodIdent(), ((HttpTimerData) invocationSequenceData.getTimerData()).getHttpInfo().getUri()); } else { return invocationSequenceData.getMethodIdent(); } }
/** * Returns the series key for the {@link HttpTimerData} object. * * @param httpTimerData * {@link HttpTimerData}. * @return Key used to initialize the series and later on compare which series data should be * added to. */ @Override protected Comparable<?> getSeriesKey(HttpTimerData httpTimerData) { if (regExTransformation && (httpTimerData instanceof RegExAggregatedHttpTimerData)) { return "Transformed URI: " + ((RegExAggregatedHttpTimerData) httpTimerData).getTransformedUri() + " [" + httpTimerData.getHttpInfo().getRequestMethod() + "]"; } else { if (plotByTagValue) { return "Tag: " + httpTimerData.getHttpInfo().getInspectItTaggingHeaderValue() + " [" + httpTimerData.getHttpInfo().getRequestMethod() + "]"; } else { return "URI: " + httpTimerData.getHttpInfo().getUri() + " [" + httpTimerData.getHttpInfo().getRequestMethod() + "]"; } } }
/** * {@inheritDoc} */ @Override public IAggregatedData<HttpTimerData> getClone(HttpTimerData httpData) { AggregatedHttpTimerData clone = new AggregatedHttpTimerData(); clone.setPlatformIdent(httpData.getPlatformIdent()); clone.setSensorTypeIdent(httpData.getSensorTypeIdent()); clone.setMethodIdent(httpData.getMethodIdent()); clone.setCharting(httpData.isCharting()); if (uriBased) { clone.getHttpInfo().setUri(httpData.getHttpInfo().getUri()); } else { // Aggregation based on Usecase. We reset the URI so that we can easily know // that use case aggregation is used. clone.getHttpInfo().setUri(HttpInfo.UNDEFINED); clone.getHttpInfo().setInspectItTaggingHeaderValue(httpData.getHttpInfo().getInspectItTaggingHeaderValue()); } clone.getHttpInfo().setRequestMethod(httpData.getHttpInfo().getRequestMethod()); return clone; }