diff --git a/agent-providers/span/src/main/java/com/expedia/www/haystack/agent/core/BaseAgent.java b/agent-providers/span/src/main/java/com/expedia/www/haystack/agent/core/BaseAgent.java index 7760383..beefcef 100644 --- a/agent-providers/span/src/main/java/com/expedia/www/haystack/agent/core/BaseAgent.java +++ b/agent-providers/span/src/main/java/com/expedia/www/haystack/agent/core/BaseAgent.java @@ -4,6 +4,7 @@ import com.expedia.www.haystack.agent.span.enricher.Enricher; import com.google.common.annotations.VisibleForTesting; import com.typesafe.config.Config; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import org.slf4j.Logger; @@ -53,7 +54,11 @@ public List loadAndInitializeDispatchers(final Config config, ClassL }); } - Validate.notEmpty(dispatchers, "Span agent dispatchers can't be an empty set"); + Validate.notEmpty( + dispatchers, + "%s agent dispatchers can't be an empty set", + StringUtils.capitalize(getName()) + ); return dispatchers; } @@ -66,7 +71,7 @@ public List loadSpanEnrichers(final Config config) { .map(clazz -> { try { final Class c = Class.forName(clazz); - logger.info("Initializing the span enricher with class name '{}'", clazz); + logger.info("Initializing the {} enricher with class name '{}'", getName(), clazz); return (Enricher) c.newInstance(); } catch (Exception e) { logger.error("Fail to initialize the enricher with clazz name {}", clazz, e); diff --git a/agent-providers/span/src/test/scala/com/expedia/www/haystack/agent/pitchfork/spi/PitchforkAgentSpec.scala b/agent-providers/span/src/test/scala/com/expedia/www/haystack/agent/pitchfork/spi/PitchforkAgentSpec.scala new file mode 100644 index 0000000..1d4904c --- /dev/null +++ b/agent-providers/span/src/test/scala/com/expedia/www/haystack/agent/pitchfork/spi/PitchforkAgentSpec.scala @@ -0,0 +1,117 @@ +/* + * Copyright 2017 Expedia, Inc. + * + * 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 com.expedia.www.haystack.agent.pitchfork.spi + +import java.io.IOException +import java.net.URL +import java.util + +import com.expedia.open.tracing.Span +import com.expedia.www.haystack.agent.span.enricher.Enricher +import com.typesafe.config.ConfigFactory +import org.scalatest.easymock.EasyMockSugar +import org.scalatest.{FunSpec, Matchers} + +class DummyEnricher extends Enricher { + override def apply(span: Span.Builder): Unit = () +} + +class PitchforkAgentSpec extends FunSpec with Matchers with EasyMockSugar { + + private val dispatcherLoadFile = "META-INF/services/com.expedia.www.haystack.agent.core.Dispatcher" + + describe("Pitchfork Agent") { + it("should return the 'pitchfork' as agent name") { + new PitchforkAgent().getName shouldEqual "pitchfork" + } + + it("should load the dispatchers from the config") { + val agent = new PitchforkAgent() + val cfg = ConfigFactory.parseString( + """ + | k1 = "v1" + | port = 8080 + | + | dispatchers { + | test-dispatcher { + | queueName = "myqueue" + | } + | } + """.stripMargin) + + val cl = new ReplacingClassLoader(getClass.getClassLoader, dispatcherLoadFile, "dispatcherProvider.txt") + val dispatchers = agent.loadAndInitializeDispatchers(cfg, cl, "spans") + dispatchers.size() shouldBe 1 + dispatchers.get(0).close() + } + + it("initialization should fail if no dispatchers exist") { + val agent = new PitchforkAgent() + val cfg = ConfigFactory.parseString( + """ + | k1 = "v1" + | port = 8080 + | + | dispatchers { + | test-dispatcher { + | queueName = "myqueue" + | } + | } + """.stripMargin) + + val caught = intercept[Exception] { + agent.loadAndInitializeDispatchers(cfg, getClass.getClassLoader, "spans") + } + + caught.getMessage shouldEqual "Pitchfork agent dispatchers can't be an empty set" + } + + it ("should load enrichers") { + val agent = new PitchforkAgent() + val cfg = ConfigFactory.parseString( + """ + | k1 = "v1" + | port = 8080 + | + | enrichers = [ + | "com.expedia.www.haystack.agent.span.spi.DummyEnricher" + | ] + """.stripMargin) + val enrichers = agent.loadSpanEnrichers(cfg) + enrichers.size() shouldBe 1 + } + } + + class ReplacingClassLoader(val parent: ClassLoader, val resource: String, val replacement: String) extends ClassLoader(parent) { + override def getResource(name: String): URL = { + if (resource == name) { + return getParent.getResource(replacement) + } + super.getResource(name) + } + + @throws[IOException] + override def getResources(name: String): util.Enumeration[URL] = { + if (resource == name) { + return getParent.getResources(replacement) + } + super.getResources(name) + } + } +} diff --git a/agent-providers/span/src/test/scala/com/expedia/www/haystack/agent/span/spi/SpanAgentSpec.scala b/agent-providers/span/src/test/scala/com/expedia/www/haystack/agent/span/spi/SpanAgentSpec.scala index 9c4ec9e..7194b6a 100644 --- a/agent-providers/span/src/test/scala/com/expedia/www/haystack/agent/span/spi/SpanAgentSpec.scala +++ b/agent-providers/span/src/test/scala/com/expedia/www/haystack/agent/span/spi/SpanAgentSpec.scala @@ -133,7 +133,7 @@ class SpanAgentSpec extends FunSpec with Matchers with EasyMockSugar { agent.loadAndInitializeDispatchers(cfg, getClass.getClassLoader, "spans") } - caught.getMessage shouldEqual "Span agent dispatchers can't be an empty set" + caught.getMessage shouldEqual "Spans agent dispatchers can't be an empty set" } it ("should load enrichers") {