Skip to content

NomadBlacky/scaladog

Repository files navigation

scaladog

Datadog API client for Scala.

Scala Steward badge

Getting Started

Add the dependency.

  • build.sbt
libraryDependencies += "dev.nomadblacky" %% "scaladog" % "0.5.3"
import $ivy.`dev.nomadblacky::scaladog:0.5.3`

Set API key and Application key

  • from environment variables
export DATADOG_API_KEY=<your api key>
export DATADOG_APP_KEY=<your application key>
export DATADOG_SITE=<your site> # Optional, "US" or "EU", default is "US"
val client = scaladog.Client()
  • or manually
val client = scaladog.Client("<your api key>", "<your application key>", scaladog.api.DatadogSite.US)

Supported APIs

  • Service Checks
  • Comments
  • Dashboards
  • Dashboard Lists
  • Downtimes
  • Embeddable graphs
  • Events
  • Graphs
  • Hosts
  • Integration AWS
  • Integration Azure
  • Integration GCP
  • Integration PagerDuty
  • Integration Slack
  • Integration Webhooks
  • Key Management
  • Logs
  • Logs Indexes
  • Metrics
    • GET /v1/metrics
    • POST /v1/series
    • GET /v1/query
    • GET /v1/metrics/<METRIC_NAME>
    • PUT /v1/metrics/<METRIC_NAME>
    • GET /v1/search
  • Monitors
  • Organizations
  • Screenboards
  • Synthetics
  • Tags
  • Timeboards
  • Tracing
  • Usage metering
  • Users

Examples

import scaladog.api.service_checks.ServiceCheckStatus
import java.time.Instant

val client = scaladog.Client()

val response = client.serviceCheck.postStatus(
  check = "app.is_ok",
  hostName = "app1",
  status = ServiceCheckStatus.OK,
  timestamp = Instant.now(),
  message = "The application is healthy.",
  tags = Seq("env:prod")
)

assert(response.isOk)
import java.time._, temporal._

val client = scaladog.Client()

val from = Instant.now().minus(1, ChronoUnit.DAYS)
val host = "myhost"
val response = client.metrics.getMetrics(from, host)

println(response) // GetMetricsResponse(List(test.metric),2019-07-30T15:22:39Z,Some(myhost))
import scaladog.api.metrics._
import java.time.Instant
import scala.util.Random

val response = scaladog.Client().metrics.postMetrics(
  Seq(
    Series(
      metric = "test.metric",
      points = Seq(Point(Instant.now(), Random.nextInt(1000))),
      host = "myhost",
      tags = Seq("project:scaladog"),
      MetricType.Gauge
    )
  )
)

assert(response.isOk)

// If you want to send a single metric, you can write more simply.
scaladog.Client().metrics.postSingleMetric("test.metric", 12.34)
import scaladog.api.events._
import java.time.Instant

val response = scaladog.Client().events.postEvent(
  title = "TEST EVENT",
  text = "This is a test event.",
  dateHappened = Instant.now(),
  priority = Priority.Low,
  tags = Seq("project:scaladog"),
  alertType = AlertType.Info
)

assert(response.isOk)
import scaladog.api.events._
import java.time.Instant

val event = scaladog.Client().events.getEvent(1234567890123456789L)

assert(event.id == 1234567890123456789L)
assert(event.title == "TEST EVENT")
assert(event.text == "This is a test event.")
assert(event.tags == Seq("project:scaladog"))
import scaladog.api.events._
import java.time._, temporal._

val now = Instant.now()
val events = scaladog.Client().events.query(start = now.minus(1, ChronoUnit.DAYS), end = now)
import java.time._, temporal._

val now = Instant.now()
val url = scaladog.Client().graphs.snapshot(
  metricQuery = "avg:datadog.estimated_usage.hosts{*}",
  start = now.minus(7, ChronoUnit.DAYS),
  end = now,
  title = getClass.getSimpleName
)
assert(url.toString startsWith "https://p.datadoghq.com/snapshot/view/dd-snapshots-prod/")