Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -53,7 +54,11 @@ public List<Dispatcher> 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;
}
Expand All @@ -66,7 +71,7 @@ public List<Enricher> 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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down