From fe724251011c177aabdf2810dfb5d4eb3a97852c Mon Sep 17 00:00:00 2001 From: Andrei Pozolotin Date: Wed, 3 Dec 2014 10:27:17 -0600 Subject: + system-metrics: two-phase sigar loading * update slf4jVersion * redirect JUL and LOG4J logging to SLF4J * use two-phase sigar loading --- .../src/main/resources/reference.conf | 4 +++ .../kamon/system/SystemMetricsCollector.scala | 30 +++++++++++++++---- .../test/scala/kamon/metrics/RedirectLogging.scala | 34 ++++++++++++++++++++++ .../scala/kamon/metrics/SystemMetricsSpec.scala | 2 +- 4 files changed, 63 insertions(+), 7 deletions(-) create mode 100644 kamon-system-metrics/src/test/scala/kamon/metrics/RedirectLogging.scala (limited to 'kamon-system-metrics/src') diff --git a/kamon-system-metrics/src/main/resources/reference.conf b/kamon-system-metrics/src/main/resources/reference.conf index 0fab129f..4d564e38 100644 --- a/kamon-system-metrics/src/main/resources/reference.conf +++ b/kamon-system-metrics/src/main/resources/reference.conf @@ -2,6 +2,10 @@ # Kamon-System-Metrics Reference Configuration # # ============================================ # +# Sigar provisioner native library extract location. +# Use per-application-instance scoped location, such as program working directory. +kamon.sigar.folder = ${user.dir}"/native" + kamon { metrics { precision { diff --git a/kamon-system-metrics/src/main/scala/kamon/system/SystemMetricsCollector.scala b/kamon-system-metrics/src/main/scala/kamon/system/SystemMetricsCollector.scala index bb7fa105..8094cac4 100644 --- a/kamon-system-metrics/src/main/scala/kamon/system/SystemMetricsCollector.scala +++ b/kamon-system-metrics/src/main/scala/kamon/system/SystemMetricsCollector.scala @@ -158,16 +158,34 @@ class SystemMetricsCollector(collectInterval: FiniteDuration) extends Actor with rcs.global.record(contextSwitches) } + def verifiedSigarInstance: SigarProxy = { + val sigar = new Sigar() + printBanner(sigar) + sigar + } + + def provisionSigarLibrary: Unit = { + val folder = context.system.settings.config.getString("kamon.sigar.folder") + SigarProvisioner.provision(new File(folder)) + } + def createSigarInstance: SigarProxy = { - val tempFolder = new File(System.getProperty("java.io.tmpdir")) + // 1) Assume that library is already provisioned. + try { + return verifiedSigarInstance + } catch { + // Not using [[Try]] - any error is non-fatal in this case. + case e: Throwable ⇒ log.info(s"Sigar is not yet provisioned: ${e}") + } + + // 2) Attempt to provision library via sigar-loader. try { - SigarProvisioner.provision(tempFolder) - val sigar = new Sigar() - printBanner(sigar) - sigar + provisionSigarLibrary + return verifiedSigarInstance } catch { - case NonFatal(t) ⇒ throw new UnexpectedSigarException("Failed to load sigar") + // Not using [[Try]] - any error is non-fatal in this case. + case e: Throwable ⇒ throw new UnexpectedSigarException(s"Failed to load Sigar: ${e}") } } } diff --git a/kamon-system-metrics/src/test/scala/kamon/metrics/RedirectLogging.scala b/kamon-system-metrics/src/test/scala/kamon/metrics/RedirectLogging.scala new file mode 100644 index 00000000..fbf42cf0 --- /dev/null +++ b/kamon-system-metrics/src/test/scala/kamon/metrics/RedirectLogging.scala @@ -0,0 +1,34 @@ +/* ========================================================================================= + * Copyright © 2013-2014 the kamon project + * + * 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 kamon.metric + +import java.util.logging.LogManager +import org.slf4j.bridge.SLF4JBridgeHandler + +/** + * Redirect different logging sources to SLF4J. + */ +trait RedirectLogging { + + def redirectLogging(): Unit = { + // Redirect JUL to SLF4J. + LogManager.getLogManager().reset(); + SLF4JBridgeHandler.install(); + } + + redirectLogging() + +} diff --git a/kamon-system-metrics/src/test/scala/kamon/metrics/SystemMetricsSpec.scala b/kamon-system-metrics/src/test/scala/kamon/metrics/SystemMetricsSpec.scala index 058dafb4..714a1e6d 100644 --- a/kamon-system-metrics/src/test/scala/kamon/metrics/SystemMetricsSpec.scala +++ b/kamon-system-metrics/src/test/scala/kamon/metrics/SystemMetricsSpec.scala @@ -33,7 +33,7 @@ import org.scalatest.{ Matchers, WordSpecLike } import scala.concurrent.duration._ -class SystemMetricsSpec extends TestKitBase with WordSpecLike with Matchers { +class SystemMetricsSpec extends TestKitBase with WordSpecLike with Matchers with RedirectLogging { implicit lazy val system: ActorSystem = ActorSystem("system-metrics-spec", ConfigFactory.parseString( """ |akka { -- cgit v1.2.3