org.glowroot.collector.QueryComponent Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2015 the original author or authors.
*
* 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.glowroot.collector;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Nullable;
import org.glowroot.shaded.fasterxml.jackson.annotation.JsonCreator;
import org.glowroot.shaded.fasterxml.jackson.annotation.JsonProperty;
import org.glowroot.shaded.fasterxml.jackson.core.type.TypeReference;
import org.glowroot.shaded.fasterxml.jackson.databind.JsonMappingException;
import org.glowroot.shaded.fasterxml.jackson.databind.ObjectMapper;
import org.glowroot.shaded.google.common.collect.Lists;
import org.glowroot.shaded.google.common.collect.Maps;
import org.glowroot.shaded.google.common.primitives.Longs;
import org.glowroot.common.ObjectMappers;
import org.glowroot.markers.UsedByJsonBinding;
import org.glowroot.transaction.model.QueryData;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static org.glowroot.common.ObjectMappers.checkRequiredProperty;
public class QueryComponent {
private static final ObjectMapper mapper = ObjectMappers.create();
private final Map> queries = Maps.newHashMap();
private final int hardLimitMultiplierWhileBuilding;
private final int maxMultiplierWhileBuilding;
public QueryComponent(int hardLimitMultiplierWhileBuilding, int maxMultiplierWhileBuilding) {
this.hardLimitMultiplierWhileBuilding = hardLimitMultiplierWhileBuilding;
this.maxMultiplierWhileBuilding = maxMultiplierWhileBuilding;
}
public Map> getOrderedAndTruncatedQueries() {
Map> mergedQueries = Maps.newHashMap();
for (Entry> entry : queries.entrySet()) {
List aggregateQueries = Lists.newArrayList(entry.getValue().values());
order(aggregateQueries);
if (aggregateQueries.size() > hardLimitMultiplierWhileBuilding) {
aggregateQueries = aggregateQueries.subList(0, hardLimitMultiplierWhileBuilding);
}
mergedQueries.put(entry.getKey(), aggregateQueries);
}
return mergedQueries;
}
// not using static ObjectMapper because ObjectMapper caches keys, and in this particular case
// the keys are (often very large) sql queries and have seen it retain 26mb worth of memory
public void mergeQueries(String queriesContent) throws IOException {
Map> toBeMergedQueries = ObjectMappers.readRequiredValue(
mapper, queriesContent, new TypeReference