Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(agent): add browser fingerprint label to http metrics #1231

Merged
merged 7 commits into from
Jun 27, 2024
Merged
Changes from 3 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
@@ -1,23 +1,80 @@
package org.hyperledger.identus.agent.server.http

import io.circe.*
import io.circe.generic.semiauto.*
import io.circe.syntax.*
import io.micrometer.prometheus.PrometheusMeterRegistry
import org.http4s.*
import org.http4s.blaze.server.BlazeServerBuilder
import org.http4s.server.Router
import org.hyperledger.identus.api.http.ErrorResponse
import org.hyperledger.identus.shared.crypto.Sha256Hash
import org.hyperledger.identus.shared.utils.Json
import org.hyperledger.identus.system.controller.SystemEndpoints
import sttp.tapir.*
import sttp.tapir.model.ServerRequest
import sttp.tapir.server.http4s.ztapir.ZHttp4sServerInterpreter
import sttp.tapir.server.http4s.Http4sServerOptions
import sttp.tapir.server.metrics.prometheus.PrometheusMetrics
import sttp.tapir.server.metrics.MetricLabels
import sttp.tapir.ztapir.ZServerEndpoint
import zio.*
import zio.interop.catz.*

class ZHttp4sBlazeServer(micrometerRegistry: PrometheusMeterRegistry, metricsNamespace: String) {

private def browserFingerprint(sr: ServerRequest): Option[Sha256Hash] = {
case class FingerPrintData(
userAgent: Option[String],
accept: Option[String],
acceptLanguage: Option[String],
acceptEncoding: Option[String],
referrer: Option[String],
dnt: Option[String],
SecChUa: Option[String],
SecChUaMobile: Option[String],
SecChUaPlatform: Option[String],
)
object FingerPrintData {
given encoder: Encoder[FingerPrintData] = deriveEncoder[FingerPrintData]

given decoder: Decoder[FingerPrintData] = deriveDecoder[FingerPrintData]
}

val headers = sr.headers
val fingerPrintData = FingerPrintData(
headers.find(_.name.toLowerCase == "user-agent").map(_.value),
headers.find(_.name.toLowerCase == "accept").map(_.value),
headers.find(_.name.toLowerCase == "accept-language").map(_.value),
headers.find(_.name.toLowerCase == "accept-encoding").map(_.value),
headers.find(_.name.toLowerCase == "referer").map(_.value),
headers.find(_.name.toLowerCase == "dnt").map(_.value),
headers.find(_.name.toLowerCase == "sec-ch-ua").map(_.value),
headers.find(_.name.toLowerCase == "sec-ch-ua-mobile").map(_.value),
headers.find(_.name.toLowerCase == "sec-ch-ua-platform").map(_.value),
)

val jsonStr = fingerPrintData.asJson.dropNullValues.spaces2
val canonicalized = Json.canonicalizeToJcs(jsonStr).toOption

canonicalized.map(x => Sha256Hash.compute(x.getBytes))
}

private val metricsLabel: MetricLabels = MetricLabels.Default.copy(
forRequest = MetricLabels.Default.forRequest :+ "browser_fingerprint" -> { case (_, sr) =>
browserFingerprint(sr) match {
case Some(hash) => hash.hexEncoded
case None => "unknown"
}
},
)

private val tapirPrometheusMetricsZIO: Task[PrometheusMetrics[Task]] = ZIO.attempt {
PrometheusMetrics.default[Task](namespace = metricsNamespace, registry = micrometerRegistry.getPrometheusRegistry)
PrometheusMetrics.default[Task](
namespace = metricsNamespace,
registry = micrometerRegistry.getPrometheusRegistry,
labels = metricsLabel
)
}

private val serverOptionsZIO: ZIO[PrometheusMetrics[Task], Throwable, Http4sServerOptions[Task]] = for {
Expand Down
Loading