Skip to content

Commit

Permalink
Merge pull request #1830 from cloud-apim/master
Browse files Browse the repository at this point in the history
add apikey/user tags and metadata to identity in analytics events
  • Loading branch information
mathieuancelin authored Feb 7, 2024
2 parents ade643b + 10c875d commit e6d8896
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
4 changes: 3 additions & 1 deletion otoroshi/app/controllers/BackOfficeController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2138,7 +2138,9 @@ class BackOfficeController(
identity = Identity(
identityType = "APIKEY",
identity = "client_id",
label = "client"
label = "client",
tags = Seq.empty,
metadata = Map.empty,
).some,
responseChunked = false,
`@serviceId` = s"route_${IdGenerator.uuid}",
Expand Down
27 changes: 25 additions & 2 deletions otoroshi/app/events/analytics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import otoroshi.tcp.TcpService
import play.api.Logger
import play.api.libs.json._
import otoroshi.utils.json.JsonImplicits._
import otoroshi.utils.syntax.implicits.{BetterJsReadable, BetterJsValue}

import scala.collection.mutable
import scala.concurrent.duration.FiniteDuration
Expand Down Expand Up @@ -236,10 +237,32 @@ trait AnalyticEvent extends OtoroshiEvent {
}
}

case class Identity(identityType: String, identity: String, label: String)
case class Identity(identityType: String, identity: String, label: String, tags: Seq[String], metadata: Map[String, String]) {
def json: JsValue = Identity.format.writes(this)
}

object Identity {
implicit val format = Json.format[Identity]
implicit val format = new Format[Identity] {
override def writes(o: Identity): JsValue = Json.obj(
"identityType" -> o.identityType,
"identity" -> o.identity,
"label" -> o.label,
"metadata" -> o.metadata,
"tags" -> o.tags,
)
override def reads(json: JsValue): JsResult[Identity] = scala.util.Try {
Identity(
identityType = json.select("identityType").asString,
identity = json.select("identity").asString,
label = json.select("label").asString,
metadata = json.select("metadata").asOpt[Map[String, String]].getOrElse(Map.empty),
tags = json.select("tags").asOpt[Seq[String]].getOrElse(Seq.empty),
)
} match {
case Success(s) => JsSuccess(s)
case Failure(e) => JsError(e.getMessage)
}
}
}

case class Location(host: String, scheme: String, uri: String)
Expand Down
8 changes: 6 additions & 2 deletions otoroshi/app/gateway/http.scala
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,19 @@ class HttpHandler()(implicit env: Env) {
Identity(
identityType = "APIKEY",
identity = k.clientId,
label = k.clientName
label = k.clientName,
tags = k.tags,
metadata = k.metadata,
)
)
.orElse(
paUsr.map(k =>
Identity(
identityType = "PRIVATEAPP",
identity = k.email,
label = k.name
label = k.name,
tags = k.tags,
metadata = k.metadata,
)
)
),
Expand Down
8 changes: 6 additions & 2 deletions otoroshi/app/gateway/websockets.scala
Original file line number Diff line number Diff line change
Expand Up @@ -268,15 +268,19 @@ class WebSocketHandler()(implicit env: Env) {
Identity(
identityType = "APIKEY",
identity = k.clientId,
label = k.clientName
label = k.clientName,
tags = k.tags,
metadata = k.metadata,
)
)
.orElse(
paUsr.map(k =>
Identity(
identityType = "PRIVATEAPP",
identity = k.email,
label = k.name
label = k.name,
tags = k.tags,
metadata = k.metadata,
)
)
),
Expand Down
16 changes: 12 additions & 4 deletions otoroshi/app/next/proxy/engine.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3457,15 +3457,19 @@ class ProxyEngine() extends RequestHandler {
Identity(
identityType = "APIKEY",
identity = k.clientId,
label = k.clientName
label = k.clientName,
tags = k.tags,
metadata = k.metadata,
)
)
.orElse(
paUsr.map(k =>
Identity(
identityType = "PRIVATEAPP",
identity = k.email,
label = k.name
label = k.name,
tags = k.tags,
metadata = k.metadata,
)
)
),
Expand Down Expand Up @@ -3615,15 +3619,19 @@ class ProxyEngine() extends RequestHandler {
Identity(
identityType = "APIKEY",
identity = k.clientId,
label = k.clientName
label = k.clientName,
tags = k.tags,
metadata = k.metadata,
)
)
.orElse(
paUsr.map(k =>
Identity(
identityType = "PRIVATEAPP",
identity = k.email,
label = k.name
label = k.name,
tags = k.tags,
metadata = k.metadata,
)
)
),
Expand Down

0 comments on commit e6d8896

Please sign in to comment.