Skip to content

Commit b3f5962

Browse files
authored
add token authentication to the InfluxDB reporter, fixes #1107 (#1110)
1 parent dd7ae1c commit b3f5962

File tree

2 files changed

+32
-19
lines changed

2 files changed

+32
-19
lines changed

reporters/kamon-influxdb/src/main/resources/reference.conf

+13-7
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,19 @@ kamon {
2424
# [ns,u,µ,ms,s]
2525
precision = "s"
2626

27-
# Client authentication credentials for connection to the InfluxDB server. There is no authentication by default,
28-
# if you wish to enable it, add an authentication section to your configuration file. E.g.:
29-
#
30-
# authentication {
31-
# user = "user"
32-
# password = "password"
33-
# }
27+
# Client authentication credentials for connection to the InfluxDB server. There is no authentication by default.
28+
# You can enable authentication by adding an authentication section to your configuration file with either token or
29+
# user/password settings in it. If you specify both, token authentication will be used.
30+
#
31+
# authentication {
32+
# token = "your_api_token"
33+
#
34+
# // OR
35+
#
36+
# user = "user"
37+
# password = "password"
38+
#
39+
# }
3440

3541
# Allow including environment information as tags on all reported metrics.
3642
environment-tags {

reporters/kamon-influxdb/src/main/scala/kamon/influxdb/InfluxDBReporter.scala

+19-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package kamon.influxdb
1818

1919
import java.time.Instant
2020
import java.util.concurrent.TimeUnit
21-
2221
import com.typesafe.config.Config
2322
import kamon.influxdb.InfluxDBReporter.Settings
2423
import kamon.metric.{MetricSnapshot, PeriodSnapshot}
@@ -27,8 +26,10 @@ import kamon.module.{MetricReporter, ModuleFactory}
2726
import kamon.tag.{Tag, TagSet}
2827
import kamon.util.{EnvironmentTags, Filter}
2928
import okhttp3.{Credentials, Interceptor, MediaType, OkHttpClient, Request, RequestBody, Response}
29+
import okio.ByteString
3030
import org.slf4j.LoggerFactory
3131

32+
import java.nio.charset.StandardCharsets.ISO_8859_1
3233
import scala.util.Try
3334

3435

@@ -226,19 +227,25 @@ object InfluxDBReporter {
226227

227228
def readSettings(config: Config): Settings = {
228229
import scala.collection.JavaConverters._
229-
val root = config.getConfig("kamon.influxdb")
230-
val host = root.getString("hostname")
231-
val credentials = if (root.hasPath("authentication")) {
232-
Some(Credentials.basic(root.getString("authentication.user"), root.getString("authentication.password")))
230+
val influxDBConfig = config.getConfig("kamon.influxdb")
231+
val host = influxDBConfig.getString("hostname")
232+
val credentials = if (influxDBConfig.hasPath("authentication")) {
233+
if(influxDBConfig.hasPath("token"))
234+
Some("Token " + ByteString.encodeString(influxDBConfig.getString("token"), ISO_8859_1))
235+
else
236+
Some(Credentials.basic(
237+
influxDBConfig.getString("authentication.user"),
238+
influxDBConfig.getString("authentication.password")
239+
))
233240
} else {
234241
None
235242
}
236-
val port = root.getInt("port")
237-
val database = root.getString("database")
238-
val protocol = root.getString("protocol").toLowerCase
239-
val additionalTags = EnvironmentTags.from(Kamon.environment, root.getConfig("environment-tags"))
243+
val port = influxDBConfig.getInt("port")
244+
val database = influxDBConfig.getString("database")
245+
val protocol = influxDBConfig.getString("protocol").toLowerCase
246+
val additionalTags = EnvironmentTags.from(Kamon.environment, influxDBConfig.getConfig("environment-tags"))
240247

241-
val precision = root.getString("precision")
248+
val precision = influxDBConfig.getString("precision")
242249

243250
if (!Set("ns","u","µ","ms","s").contains(precision)){
244251
throw new RuntimeException("Precision must be one of `[ns,u,µ,ms,s]` to match https://docs.influxdata.com/influxdb/v1.7/tools/api/#query-string-parameters-1")
@@ -249,10 +256,10 @@ object InfluxDBReporter {
249256

250257
Settings(
251258
url,
252-
root.getDoubleList("percentiles").asScala.toList.map(_.toDouble),
259+
influxDBConfig.getDoubleList("percentiles").asScala.toList.map(_.toDouble),
253260
credentials,
254261
Filter.from("kamon.influxdb.tag-filter"),
255-
root.getBoolean("post-empty-distributions"),
262+
influxDBConfig.getBoolean("post-empty-distributions"),
256263
additionalTags,
257264
precision
258265
)

0 commit comments

Comments
 (0)