This repository has been archived by the owner on Oct 31, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the concept of RequestLabeller for metric labels
- Loading branch information
Sam Starling
committed
Dec 16, 2016
1 parent
5518879
commit 03f8277
Showing
9 changed files
with
103 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/main/scala/com/samstarling/prometheusfinagle/filter/HttpRequestLabeller.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.samstarling.prometheusfinagle.filter | ||
|
||
import com.twitter.finagle.http.{Request, Response} | ||
|
||
trait RequestLabeller { | ||
|
||
/** | ||
* TODO Explain why this is modelled like this | ||
* @return | ||
*/ | ||
def keys: Seq[String] | ||
|
||
/** | ||
* TODO Explain why this is modelled like this | ||
* @return | ||
*/ | ||
def labelsFor(request: Request, response: Response): Seq[String] | ||
} | ||
|
||
// TODO Include response in name | ||
class HttpRequestLabeller extends RequestLabeller { | ||
|
||
def keys: Seq[String] = Seq( | ||
"status", | ||
"statusClass", | ||
"method" | ||
) | ||
|
||
def labelsFor(request: Request, response: Response): List[String] = List( | ||
response.status.code.toString, | ||
s"${response.status.code.toString.charAt(0)}xx", | ||
request.method.toString | ||
) | ||
} |
9 changes: 0 additions & 9 deletions
9
src/main/scala/com/samstarling/prometheusfinagle/filter/StatusClass.scala
This file was deleted.
Oops, something went wrong.
11 changes: 10 additions & 1 deletion
11
src/test/scala/com/samstarling/prometheusfinagle/UnitTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
package com.samstarling.prometheusfinagle | ||
|
||
import com.samstarling.prometheusfinagle.filter.RequestLabeller | ||
import com.twitter.finagle.http.{Request, Response} | ||
import org.specs2.mock.Mockito | ||
import org.specs2.mutable.Specification | ||
|
||
trait UnitTest extends Specification with Mockito | ||
trait UnitTest extends Specification with Mockito { | ||
|
||
class TestLabeller extends RequestLabeller { | ||
override def keys: List[String] = List("foo") | ||
override def labelsFor(request: Request, response: Response): List[String] = List("bar") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/scala/com/samstarling/prometheusfinagle/filter/HttpRequestLabellerSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.samstarling.prometheusfinagle.filter | ||
|
||
import com.samstarling.prometheusfinagle.UnitTest | ||
import com.twitter.finagle.http.{Method, Request, Response, Status} | ||
import org.specs2.specification.Scope | ||
|
||
class HttpRequestLabellerSpec extends UnitTest { | ||
|
||
trait Context extends Scope { | ||
val request = Request(Method.Get, "/foo/bar") | ||
val response = Response(Status.Ok) | ||
val labeller = new HttpRequestLabeller() | ||
val labels = labeller.labelsFor(request, response) | ||
} | ||
|
||
"keys" >> { | ||
"returns the keys in the correct order" in new Context { | ||
labeller.keys ==== Seq("status", "statusClass", "method") | ||
} | ||
} | ||
|
||
"labelsFor" >> { | ||
"returns the status code of the response" in new Context { | ||
labels(0) ==== "200" | ||
} | ||
|
||
"returns the status class of the request" in new Context { | ||
labels(1) ==== "2xx" | ||
} | ||
|
||
"returns the method of the request" in new Context { | ||
labels(2) ==== "GET" | ||
} | ||
} | ||
} |