Search in sources :

Example 31 with Context

use of com.codahale.metrics.Timer.Context in project Singularity by HubSpot.

the class SingularityUploader method uploadBatch.

int uploadBatch(List<Path> toUpload) {
    final long start = System.currentTimeMillis();
    LOG.info("{} Uploading {} item(s)", logIdentifier, toUpload.size());
    int success = 0;
    for (int i = 0; i < toUpload.size(); i++) {
        final Context context = metrics.getUploadTimer().time();
        final Path file = toUpload.get(i);
        if (!configuration.isCheckForOpenFiles() || !isFileOpen(file)) {
            try {
                uploadSingle(i, file);
                metrics.upload();
                success++;
                Files.delete(file);
            } catch (RetryException re) {
                metrics.error();
                LOG.warn("{} Couldn't upload or delete {}", logIdentifier, file, re);
                exceptionNotifier.notify(String.format("%s exception during upload", re.getCause().getClass()), re.getCause(), ImmutableMap.of("logIdentifier", logIdentifier, "file", file.toString(), "failedAttempts", Integer.toString(re.getNumberOfFailedAttempts())));
            } catch (Exception e) {
                metrics.error();
                LOG.warn("{} Couldn't upload or delete {}", logIdentifier, file, e);
                exceptionNotifier.notify(String.format("Error during upload (%s)", e.getMessage()), e, ImmutableMap.of("logIdentifier", logIdentifier, "file", file.toString()));
            } finally {
                context.stop();
            }
        } else {
            LOG.info("{} is in use by another process, will retry upload later", file);
        }
    }
    LOG.info("{} Uploaded {} out of {} item(s) in {}", logIdentifier, success, toUpload.size(), JavaUtils.duration(start));
    return toUpload.size();
}
Also used : Context(com.codahale.metrics.Timer.Context) Path(java.nio.file.Path) RetryException(com.github.rholder.retry.RetryException) RetryException(com.github.rholder.retry.RetryException) IOException(java.io.IOException)

Example 32 with Context

use of com.codahale.metrics.Timer.Context in project sidewinder by srotya.

the class GrafanaQueryApi method query.

@Path("/query")
@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public List<Target> query(@PathParam(DatabaseOpsApi.DB_NAME) String dbName, String query) throws ParseException {
    grafanaQueryCounter.mark();
    Context time = grafanaQueryLatency.time();
    Gson gson = new GsonBuilder().setPrettyPrinting().create();
    logger.log(Level.FINE, () -> "Grafana query:" + dbName + "\t" + gson.toJson(gson.fromJson(query, JsonObject.class)));
    JsonObject json = gson.fromJson(query, JsonObject.class);
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    JsonObject range = json.get("range").getAsJsonObject();
    long startTs = sdf.parse(range.get("from").getAsString()).getTime();
    long endTs = sdf.parse(range.get("to").getAsString()).getTime();
    startTs = tz.getOffset(startTs) + startTs;
    endTs = tz.getOffset(endTs) + endTs;
    List<TargetSeries> targetSeries = new ArrayList<>();
    List<Target> output = new ArrayList<>();
    try {
        GrafanaUtils.extractTargetsFromJson(json, targetSeries);
    } catch (IllegalArgumentException e) {
        return output;
    }
    logger.log(Level.FINE, "Extracted targets from query json, target count:" + targetSeries.size() + " " + new Date(startTs));
    for (TargetSeries targetSeriesEntry : targetSeries) {
        logger.log(Level.FINE, () -> "Running grafana query fetch for:" + targetSeriesEntry);
        try {
            GrafanaUtils.queryAndGetData(engine, dbName, startTs, endTs, output, targetSeriesEntry);
        } catch (IOException e) {
            throw new InternalServerErrorException(e);
        }
    }
    time.stop();
    return output;
}
Also used : Context(com.codahale.metrics.Timer.Context) GsonBuilder(com.google.gson.GsonBuilder) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) JsonObject(com.google.gson.JsonObject) IOException(java.io.IOException) Date(java.util.Date) InternalServerErrorException(javax.ws.rs.InternalServerErrorException) SimpleDateFormat(java.text.SimpleDateFormat) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes)

Example 33 with Context

use of com.codahale.metrics.Timer.Context in project wikidata-query-rdf by wikimedia.

the class RecentChangesPoller method fetchRecentChanges.

/**
 * Fetch recent changes from Wikibase.
 * If we're close to current time, we back off a bit from last timestamp,
 * and fetch by timestamp. If it's back in the past, we fetch by continuation.
 *
 * @throws RetryableException on fetch failure
 */
private RecentChangeResponse fetchRecentChanges(Instant lastNextStartTime, Batch lastBatch) throws RetryableException {
    try (Context timerContext = recentChangesTimer.time()) {
        RecentChangeResponse recentChanges = doFetchRecentChanges(lastNextStartTime, lastBatch);
        recentChangesCounter.inc(recentChanges.getQuery().getRecentChanges().size());
        return recentChanges;
    }
}
Also used : Context(com.codahale.metrics.Timer.Context) RecentChangeResponse(org.wikidata.query.rdf.tool.wikibase.RecentChangeResponse)

Example 34 with Context

use of com.codahale.metrics.Timer.Context in project newts by OpenNMS.

the class FileImport method run.

@Override
public void run() {
    String line;
    try {
        // Throw away the first (header).
        m_reader.readLine();
        while ((line = m_reader.readLine()) != null) {
            try {
                List<Sample> samples = m_lineParser.parseLine(line);
                Context timerCtx = m_writeTimer.time();
                try {
                    m_repository.insert(samples);
                } finally {
                    timerCtx.stop();
                }
                m_numRows.inc();
                m_numSamples.inc(10);
            } catch (ParseException e) {
                LOG.error("Unable to parse date from line '{}'", line);
            }
        }
    } catch (IOException e) {
        LOG.error("Error reading GSOD data file: {]", e);
    }
}
Also used : Context(com.codahale.metrics.Timer.Context) Sample(org.opennms.newts.api.Sample) ParseException(java.text.ParseException) IOException(java.io.IOException)

Example 35 with Context

use of com.codahale.metrics.Timer.Context in project metrics by dropwizard.

the class ExponentiallyDecayingReservoirTest method removeZeroWeightsInSamplesToPreventNaNInMeanValues.

@Test
public void removeZeroWeightsInSamplesToPreventNaNInMeanValues() {
    final ManualClock clock = new ManualClock();
    final Reservoir reservoir = reservoirFactory.create(1028, 0.015, clock);
    Timer timer = new Timer(reservoir, clock);
    Context context = timer.time();
    clock.addMillis(100);
    context.stop();
    for (int i = 1; i < 48; i++) {
        clock.addHours(1);
        assertThat(reservoir.getSnapshot().getMean()).isBetween(0.0, Double.MAX_VALUE);
    }
}
Also used : Context(com.codahale.metrics.Timer.Context) Test(org.junit.Test)

Aggregations

Context (com.codahale.metrics.Timer.Context)37 Timer (com.codahale.metrics.Timer)11 MetricsContext (com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext)10 ApplierContext (com.torodb.mongodb.repl.oplogreplier.ApplierContext)9 OplogOperation (com.eightkdata.mongowp.server.api.oplog.OplogOperation)8 Test (org.junit.Test)7 RetrierGiveUpException (com.torodb.core.retrier.RetrierGiveUpException)6 SQLException (java.sql.SQLException)5 PreparedStatementExecutorWrapper (com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper)4 RetrierAbortException (com.torodb.core.retrier.RetrierAbortException)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 StatementExecutorWrapper (com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper)3 RollbackException (com.torodb.core.transaction.RollbackException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ExecutionException (java.util.concurrent.ExecutionException)3 ExecutorService (java.util.concurrent.ExecutorService)3 ConsoleReporter (com.codahale.metrics.ConsoleReporter)2 Counter (com.codahale.metrics.Counter)2