Skip to content

Commit

Permalink
feat(info): ✨ add info route to get all build and system properties
Browse files Browse the repository at this point in the history
Signed-off-by: Felipe Bonezi <[email protected]>
  • Loading branch information
felipebonezi committed Dec 9, 2022
1 parent 0577a9d commit 1643028
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 9 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Below we have all project endpoints available on this project.
| Endpoint ID | Description | Path | Ready to use? |
|-------------|----------------------------------------------|---------------------|---------------|
| health | Displays your application’s health status. | `/actuator/health` | ✔️ |
| info | Displays information about your application. | `/actuator/info` | |
| info | Displays information about your application. | `/actuator/info` | |
| logfile | Returns the contents of the log file. | `/actuator/logfile` | ✖️ |

## Health endpoint details
Expand Down Expand Up @@ -80,6 +80,13 @@ Show to you information about your Redis connection.
libraryDependencies ++= "io.github.felipebonezi" %% "play-actuator-redis-indicator" % "(version)"
```

## Info endpoint details

You can enable to get all operational system info
inside the JSON return by `/actuator/info` route. Default is disabled.

`play.actuator.info.system.enabled = true`

## Scala compatibility

This project is compatible with Scala `2.12` and `2.13`, so you need to use the right version.
Expand Down
7 changes: 5 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ lazy val core = project
lazy val actuator = project
.in(file("play-actuator"))
.dependsOn(core)
.enablePlugins(Common, BuildInfoPlugin)
.settings(
name := s"$repoName",
organization := "io.github.felipebonezi",
Expand All @@ -67,9 +68,11 @@ lazy val actuator = project
s"scm:git:[email protected]:felipebonezi/$repoName.git"
)
),
Dependencies.actuator
Dependencies.actuator,
buildInfoKeys := Seq[BuildInfoKey](name, version, scalaVersion, sbtVersion),
buildInfoPackage := "play.actuator.build",
buildInfoOptions += BuildInfoOption.ToJson
)
.enablePlugins(Common)

lazy val jdbc = project
.in(file("play-actuator-indicators/database/jdbc"))
Expand Down
2 changes: 1 addition & 1 deletion play-actuator-sample/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ libraryDependencies += guice
libraryDependencies += "org.postgresql" % "postgresql" % "42.5.1"
libraryDependencies += "com.github.karelcemus" %% "play-redis" % "2.7.0"

lazy val actuatorVersion = "0.2.0"
lazy val actuatorVersion = "0.2.0+8-0577a9db+20221209-1830-SNAPSHOT"
libraryDependencies += "io.github.felipebonezi" %% "play-actuator" % actuatorVersion
libraryDependencies += "io.github.felipebonezi" %% "play-actuator-redis-indicator" % actuatorVersion
libraryDependencies += "io.github.felipebonezi" %% "play-actuator-jdbc-indicator" % actuatorVersion
Expand Down
1 change: 1 addition & 0 deletions play-actuator-sample/conf/application.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
play {
http.secret.key = "KQ87XDCc^NxJ1529v[v6vee8X<S>d49LE/DhxprrDzU<j]7bfZW7B3`nzBN2svoF"
actuator {
info.system.enabled = true
health {
indicators {
# Check disk space details.
Expand Down
5 changes: 3 additions & 2 deletions play-actuator/src/main/resources/reference.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
play {
actuator {
indicator {
health {
info.system.enabled = false
health {
indicators {
diskspace = false
database = false
redis = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,14 @@ import play.actuator.ActuatorEnum.Down
import play.actuator.ActuatorEnum.Status
import play.actuator.ActuatorEnum.Up
import play.actuator.health.HealthService
import play.actuator.info.InfoService
import play.api.libs.json.Json.toJson

import javax.inject.Inject
import scala.concurrent.ExecutionContext

class ActuatorController @Inject() (healthService: HealthService, cc: ControllerComponents)(implicit
ec: ExecutionContext
class ActuatorController @Inject() (healthService: HealthService, infoService: InfoService, cc: ControllerComponents)(
implicit ec: ExecutionContext
) extends BaseController {

def health: Action[AnyContent] = Action {
Expand All @@ -52,7 +53,9 @@ class ActuatorController @Inject() (healthService: HealthService, cc: Controller
}
}

def info: Action[AnyContent] = TODO
def info: Action[AnyContent] = Action {
Ok(this.infoService.getBuildInfos)
}

protected override def controllerComponents: ControllerComponents = this.cc

Expand Down
61 changes: 61 additions & 0 deletions play-actuator/src/main/scala/play/actuator/info/InfoService.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2022 Felipe Bonezi <https://about.me/felipebonezi>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package play.actuator.info

import play.actuator.build.BuildInfo
import play.api.Configuration
import play.api.libs.json.JsObject
import play.api.libs.json.JsString
import play.api.libs.json.JsValue
import play.api.libs.json.Json

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter.ISO_DATE_TIME
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class InfoService @Inject() (config: Configuration) {

import scala.collection.JavaConverters._

def getBuildInfos: JsValue = {
var buildInfos = Json.parse(BuildInfo.toJson).as[JsObject]
buildInfos = buildInfos + ("dateTime" -> JsString(LocalDateTime.now().format(ISO_DATE_TIME)))

if (isSystemInfoActive) {
import scala.collection.immutable.ListMap

val systemInfos = System.getProperties.asScala.map(p => (p._1, JsString(p._2)))
val sortedSystemInfos = ListMap(systemInfos.toSeq.sortBy(_._1): _*)
buildInfos = buildInfos + ("system" -> JsObject(sortedSystemInfos))
}

buildInfos
}

private def isSystemInfoActive: Boolean =
this.config
.getOptional[Boolean](s"play.actuator.info.system.enabled")
.orElse(Some(false))
.get

}
4 changes: 4 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
addSbtPlugin("de.heikoseeberger" % "sbt-header" % "5.9.0")
addSbtPlugin("com.github.sbt" % "sbt-ci-release" % "1.5.11")

// Build Info
// See more: https://github.com/sbt/sbt-buildinfo
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.11.0")

// Code formatter for Scala.
// See more: https://github.com/scalameta/sbt-scalafmt
// Oficial Website: https://scalameta.org/scalafmt/
Expand Down

0 comments on commit 1643028

Please sign in to comment.