Navigation Menu

Skip to content

Commit

Permalink
trivial examples of the metrics plugin, exposed through the Datastrea…
Browse files Browse the repository at this point in the history
…mTest
  • Loading branch information
cbeer committed Mar 20, 2013
1 parent 95b721f commit 5f84e3e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 9 deletions.
5 changes: 5 additions & 0 deletions fcrepo-kernel/pom.xml
Expand Up @@ -62,6 +62,11 @@
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>com.yammer.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>2.2.0</version>
</dependency>
</dependencies>

<build>
Expand Down
44 changes: 35 additions & 9 deletions fcrepo-kernel/src/main/java/org/fcrepo/Datastream.java
Expand Up @@ -20,6 +20,7 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import javax.jcr.Node;
import javax.jcr.Property;
Expand All @@ -32,6 +33,11 @@
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.Counter;
import com.yammer.metrics.core.Histogram;
import com.yammer.metrics.core.Timer;
import com.yammer.metrics.core.TimerContext;
import org.fcrepo.exception.InvalidChecksumException;
import org.fcrepo.services.LowLevelStorageService;
import org.fcrepo.utils.ContentDigest;
Expand Down Expand Up @@ -131,7 +137,10 @@ public void setContent(InputStream content, String checksumType, String checksum
logger.debug("Failed checksum test");
throw new InvalidChecksumException("Checksum Mismatch of " + dsChecksum + " and " + checksum);
}
}
}

final Histogram contentSizeHistogram = Metrics.newHistogram(Datastream.class, "content-size");
contentSizeHistogram.update(dataProperty.getLength());

contentNode.setProperty(CONTENT_SIZE, dataProperty.getLength());
contentNode.setProperty(DIGEST_VALUE, dsChecksum);
Expand Down Expand Up @@ -173,26 +182,43 @@ public String getContentDigestType() throws RepositoryException {

public Collection<FixityResult> runFixityAndFixProblems() throws RepositoryException {
HashSet<FixityResult> fixityResults = null;
Set< FixityResult> goodEntries;
final URI digestUri = getContentDigest();
final long size = getContentSize();
MessageDigest digest = null;
try {


final Counter contentSizeHistogram = Metrics.newCounter(Datastream.class, "fixity-check-counter");
contentSizeHistogram.inc();


try {
digest = MessageDigest.getInstance(getContentDigestType());
} catch (NoSuchAlgorithmException e) {
throw new RepositoryException(e.getMessage(), e);
}


final Timer timer = Metrics.newTimer(Datastream.class, "fixity-check-time", TimeUnit.MILLISECONDS, TimeUnit.SECONDS);

final TimerContext context = timer.time();

try {
fixityResults = new HashSet<FixityResult>(
LowLevelStorageService.getFixity(node, digest, digestUri, size));

Set< FixityResult> goodEntries = ImmutableSet.copyOf(filter(fixityResults, new Predicate<FixityResult>() {
goodEntries = ImmutableSet.copyOf(filter(fixityResults, new Predicate<FixityResult>() {

@Override
public boolean apply(FixityResult result) {
return result.computedChecksum.equals(digestUri) && result.computedSize == size;
}
@Override
public boolean apply(FixityResult result) {
return result.computedChecksum.equals(digestUri) && result.computedSize == size;
}

;
}));
;
}));
} finally {
context.stop();
}

if (goodEntries.size() == 0) {
logger.error("ALL COPIES OF " + getObject().getName() + "/" + getDsId() + " HAVE FAILED FIXITY CHECKS.");
Expand Down
15 changes: 15 additions & 0 deletions fcrepo-kernel/src/test/java/org/fcrepo/integration/AbstractIT.java
@@ -1,5 +1,9 @@
package org.fcrepo.integration;

import com.yammer.metrics.Metrics;
import com.yammer.metrics.core.MetricPredicate;
import com.yammer.metrics.reporting.ConsoleReporter;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
Expand All @@ -9,11 +13,22 @@
@RunWith(SpringJUnit4ClassRunner.class)
public abstract class AbstractIT {


protected Logger logger;

@Before
public void setLogger() {
logger = LoggerFactory.getLogger(this.getClass());
}


@AfterClass
public static void dumpMetrics() {
final ConsoleReporter reporter = new ConsoleReporter(Metrics.defaultRegistry(),
System.out,
MetricPredicate.ALL);

reporter.run();
}

}

0 comments on commit 5f84e3e

Please sign in to comment.