diff options
author | Andrei Pozolotin <Andrei.Pozolotin@gmail.com> | 2014-12-03 10:27:17 -0600 |
---|---|---|
committer | Andrei Pozolotin <Andrei.Pozolotin@gmail.com> | 2014-12-04 08:02:56 -0600 |
commit | fe724251011c177aabdf2810dfb5d4eb3a97852c (patch) | |
tree | 2d9be3524a1b0df6c29c96a3e1b455f1147ab0ff /kamon-system-metrics | |
parent | 3905757c2eb9658d136c50de82180a74b002df7e (diff) | |
download | Kamon-fe724251011c177aabdf2810dfb5d4eb3a97852c.tar.gz Kamon-fe724251011c177aabdf2810dfb5d4eb3a97852c.tar.bz2 Kamon-fe724251011c177aabdf2810dfb5d4eb3a97852c.zip |
+ system-metrics: two-phase sigar loading
* update slf4jVersion
* redirect JUL and LOG4J logging to SLF4J
* use two-phase sigar loading
Diffstat (limited to 'kamon-system-metrics')
4 files changed, 63 insertions, 7 deletions
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 <http://kamon.io/> + * + * 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 { |