Skip to content

Commit

Permalink
Put associated values into request/response
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherDavenport committed Jul 12, 2023
1 parent 4ec510a commit 60ad617
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ import cats.effect.Outcome
object HttpStructuredContext {

object Common {
def method(m: Method): (String, String) = ("http.method", m.name)
def url(url: Uri): (String, String)= ("http.url", url.renderString)
def target(url: Uri): (String, String) = ("http.target", url.copy(scheme = None, authority = None).renderString)
def host(host: org.http4s.headers.Host): (String, String) = ("http.host", org.http4s.headers.Host.headerInstance.value(host))
def scheme(scheme: Uri.Scheme): (String, String) = ("http.scheme", scheme.value)
def method(m: Method): (String, String) = ("http.request.method", m.name)
def url(url: Uri): (String, String)= ("http.request.url", url.renderString)
def target(url: Uri): (String, String) = ("http.request.target", url.copy(scheme = None, authority = None).renderString)
def host(host: org.http4s.headers.Host): (String, String) = ("http.request.host", org.http4s.headers.Host.headerInstance.value(host))
def scheme(scheme: Uri.Scheme): (String, String) = ("http.request.scheme", scheme.value)

def status(status: Status): (String, String) = ("http.status_code", status.code.show)
def status(status: Status): (String, String) = ("http.response.status_code", status.code.show)
// Need to check both request and response in case negotiation happens
def flavor(httpVersion: HttpVersion): (String, String) = ("http.flavor", httpVersion.major.toString() ++ "." ++ httpVersion.minor.toString())
def userAgent(userAgent: `User-Agent`): (String, String) = ("http.user_agent", `User-Agent`.headerInstance.value(userAgent))
def requestContentLength(cl: Long): (String, String) = ("http.request_content_length", cl.show)
def responseContentLength(cl: Long): (String, String) = ("http.response_content_length", cl.show)
def userAgent(userAgent: `User-Agent`): (String, String) = ("http.request.user_agent", `User-Agent`.headerInstance.value(userAgent))
def requestContentLength(cl: Long): (String, String) = ("http.request.content_length", cl.show)
def responseContentLength(cl: Long): (String, String) = ("http.response.content_length", cl.show)
def retryCount(i: Int): (String, String)= ("http.retry_count", i.show)
def peerIp(ip: IpAddress): (String, String) = ("net.peer.ip", ip.toString()) // TODO: Check that this is the right way
def peerPort(port: Port): (String, String) = ("net.peer.port", port.value.show)
Expand Down Expand Up @@ -70,7 +70,7 @@ object HttpStructuredContext {
headers.headers.filter(h => s.contains(h.name))
.groupBy(r => (r.name))
.map{
case (name, list) => ("http." ++ messageType ++ ".header." ++ name.toString.toLowerCase, list.map(_.value).mkString(", "))
case (name, list) => ("http." ++ messageType ++ ".headers." ++ name.toString.toLowerCase, list.map(_.value).mkString(", "))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,15 @@ class MainSpec extends CatsEffectSuite {
None,
Map(

"http.target" -> "/",
"http.request.target" -> "/",
"http.exit_case" -> "succeeded",
"http.method" -> "GET",
"http.request.method" -> "GET",

"http.status_code" -> "200",
"http.response.status_code" -> "200",

"http.host" -> "localhost",
"http.request.host" -> "localhost",
"http.flavor" -> "1.1",
"http.url" -> "/",
"http.request.url" -> "/",
"http.kind" -> "server",
)
)
Expand Down Expand Up @@ -86,22 +86,22 @@ class MainSpec extends CatsEffectSuite {
"Http Server - GET",
None,
Map(
"http.response.header.content-length" -> "20",
"http.target" -> "/",
"http.response.headers.content-length" -> "20",
"http.request.target" -> "/",
"http.exit_case" -> "succeeded",
"http.method" -> "GET",
"http.request_content_length" -> "19",
"http.status_code" -> "200",
"http.request.method" -> "GET",
"http.request.content_length" -> "19",
"http.response.status_code" -> "200",
"http.request.body" -> "Hello from Request!",
"http.response.body" -> "Hello from Response!",
"http.kind" -> "server",
"http.request.header.content-length" -> "19",
"http.request.header.content-type" -> "text/plain; charset=UTF-8",
"http.response.header.content-type" -> "text/plain; charset=UTF-8",
"http.response_content_length" -> "20",
"http.host" -> "localhost",
"http.request.headers.content-length" -> "19",
"http.request.headers.content-type" -> "text/plain; charset=UTF-8",
"http.response.headers.content-type" -> "text/plain; charset=UTF-8",
"http.response.content_length" -> "20",
"http.request.host" -> "localhost",
"http.flavor" -> "1.1",
"http.url" -> "/"
"http.request.url" -> "/"
)
)
))
Expand Down Expand Up @@ -136,22 +136,22 @@ class MainSpec extends CatsEffectSuite {
"Req Body - Hello from Request!\nResp Body - Hello from Response!",
None,
Map(
"http.response.header.content-length" -> "20",
"http.target" -> "/",
"http.response.headers.content-length" -> "20",
"http.request.target" -> "/",
"http.exit_case" -> "succeeded",
"http.method" -> "GET",
"http.request_content_length" -> "19",
"http.status_code" -> "200",
"http.request.method" -> "GET",
"http.request.content_length" -> "19",
"http.response.status_code" -> "200",
"http.request.body" -> "Hello from Request!",
"http.response.body" -> "Hello from Response!",
"http.kind" -> "server",
"http.request.header.content-length" -> "19",
"http.request.header.content-type" -> "text/plain; charset=UTF-8",
"http.response.header.content-type" -> "text/plain; charset=UTF-8",
"http.response_content_length" -> "20",
"http.host" -> "localhost",
"http.request.headers.content-length" -> "19",
"http.request.headers.content-type" -> "text/plain; charset=UTF-8",
"http.response.headers.content-type" -> "text/plain; charset=UTF-8",
"http.response.content_length" -> "20",
"http.request.host" -> "localhost",
"http.flavor" -> "1.1",
"http.url" -> "/"
"http.request.url" -> "/"
)
)
))
Expand Down Expand Up @@ -185,24 +185,25 @@ class MainSpec extends CatsEffectSuite {
"HttpClient \"GET http://test.http4s.org/ HTTP/1.1\" 200 20",
None,
Map(
"http.scheme" -> "http",
"http.response.header.content-length" -> "20",
"http.target" -> "/",
"http.request.method" -> "GET",
"http.request.scheme" -> "http",
"http.response.headers.content-length" -> "20",
"http.request.target" -> "/",
"http.exit_case" -> "succeeded",
"http.method" -> "GET",
"http.request_content_length" -> "19",
"http.status_code" -> "200",

"http.request.content_length" -> "19",
"http.response.status_code" -> "200",
"http.kind" -> "client",
"http.request.body" -> "Hello from Request!",
"net.peer.name" -> "test.http4s.org",
"http.response.body" -> "Hello from Response!",
"http.request.header.content-length" -> "19",
"http.request.header.content-type" -> "text/plain; charset=UTF-8",
"http.response.header.content-type" -> "text/plain; charset=UTF-8",
"http.response_content_length" -> "20",
"http.host" -> "test.http4s.org",
"http.request.headers.content-length" -> "19",
"http.request.headers.content-type" -> "text/plain; charset=UTF-8",
"http.response.headers.content-type" -> "text/plain; charset=UTF-8",
"http.response.content_length" -> "20",
"http.request.host" -> "test.http4s.org",
"http.flavor" -> "1.1",
"http.url" -> "http://test.http4s.org/"
"http.request.url" -> "http://test.http4s.org/"
)
)
))
Expand Down

0 comments on commit 60ad617

Please sign in to comment.