Skip to content

Commit

Permalink
1.9.1 (#62)
Browse files Browse the repository at this point in the history
* Update jars
* Add support for basic auth in scrape endpoints
  • Loading branch information
pambrose authored May 23, 2021
1 parent 929d84a commit 9922828
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 53 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=1.9.0
VERSION=1.9.1

default: compile

Expand Down
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ then the *prometheus.yml* scrape_config would target the three apps with:
* http://mymachine.local:8080/app2_metrics
* http://mymachine.local:8080/app3_metrics

If the endpoints use basic auth, then include the credentials in the URL with: `http://user:pass@hostname/metrics`

The `prometheus.yml` file would include:

```yaml
Expand All @@ -106,8 +108,8 @@ scrape_configs:
The docker images are available via:
```bash
docker pull pambrose/prometheus-proxy:1.9.0
docker pull pambrose/prometheus-agent:1.9.0
docker pull pambrose/prometheus-proxy:1.9.1
docker pull pambrose/prometheus-agent:1.9.1
```

Start a proxy container with:
Expand All @@ -116,15 +118,15 @@ Start a proxy container with:
docker run --rm -p 8082:8082 -p 8092:8092 -p 50051:50051 -p 8080:8080 \
--env ADMIN_ENABLED=true \
--env METRICS_ENABLED=true \
pambrose/prometheus-proxy:1.9.0
pambrose/prometheus-proxy:1.9.1
```

Start an agent container with:

```bash
docker run --rm -p 8083:8083 -p 8093:8093 \
--env AGENT_CONFIG='https://raw.githubusercontent.com/pambrose/prometheus-proxy/master/examples/simple.conf' \
pambrose/prometheus-agent:1.9.0
pambrose/prometheus-agent:1.9.1
```

Using the config
Expand All @@ -142,7 +144,7 @@ is in your current directory, run an agent container with:
docker run --rm -p 8083:8083 -p 8093:8093 \
--mount type=bind,source="$(pwd)"/prom-agent.conf,target=/app/prom-agent.conf \
--env AGENT_CONFIG=prom-agent.conf \
pambrose/prometheus-agent:1.9.0
pambrose/prometheus-agent:1.9.1
```

**Note:** The `WORKDIR` of the proxy and agent images is `/app`, so make sure to use `/app` as the base directory in the
Expand Down Expand Up @@ -281,15 +283,15 @@ docker run --rm -p 8082:8082 -p 8092:8092 -p 50440:50440 -p 8080:8080 \
--env PROXY_CONFIG=tls-no-mutual-auth.conf \
--env ADMIN_ENABLED=true \
--env METRICS_ENABLED=true \
pambrose/prometheus-proxy:1.9.0
pambrose/prometheus-proxy:1.9.1

docker run --rm -p 8083:8083 -p 8093:8093 \
--mount type=bind,source="$(pwd)"/testing/certs,target=/app/testing/certs \
--mount type=bind,source="$(pwd)"/examples/tls-no-mutual-auth.conf,target=/app/tls-no-mutual-auth.conf \
--env AGENT_CONFIG=tls-no-mutual-auth.conf \
--env PROXY_HOSTNAME=mymachine.lan:50440 \
--name docker-agent \
pambrose/prometheus-agent:1.9.0
pambrose/prometheus-agent:1.9.1
```

**Note:** The `WORKDIR` of the proxy and agent images is `/app`, so make sure to use `/app` as the base directory in the
Expand Down
2 changes: 1 addition & 1 deletion bin/docker-agent.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
docker run --rm -p 8083:8083 -p 8093:8093 \
--env AGENT_CONFIG='https://raw.githubusercontent.com/pambrose/prometheus-proxy/master/examples/simple.conf' \
--env PROXY_HOSTNAME=mymachine.lan \
pambrose/prometheus-agent:1.9.0
pambrose/prometheus-agent:1.9.1
2 changes: 1 addition & 1 deletion bin/docker-proxy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

docker run --rm -p 8082:8082 -p 8092:8092 -p 50051:50051 -p 8080:8080 \
--env PROXY_CONFIG='https://raw.githubusercontent.com/pambrose/prometheus-proxy/master/examples/simple.conf' \
pambrose/prometheus-proxy:1.9.0
pambrose/prometheus-proxy:1.9.1
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ plugins {
id "com.github.ben-manes.versions" version '0.38.0'
id 'com.github.johnrengelman.shadow' version '6.1.0'
id 'com.github.gmazzo.buildconfig' version '3.0.0'
// Turn these of until jacoco fixes their kotlin 1.5.0 SMAP issue
// Turn these off until jacoco fixes their kotlin 1.5.0 SMAP issue
// id 'jacoco'
// id 'com.github.kt3k.coveralls' version '2.12.0'
}

group = 'io.prometheus'
version = '1.9.0'
version = '1.9.1'

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand Down Expand Up @@ -62,6 +62,7 @@ dependencies {
implementation "io.ktor:ktor-server-cio:$ktor_version"
implementation "io.ktor:ktor-client-core:$ktor_version"
implementation "io.ktor:ktor-client-cio:$ktor_version"
implementation "io.ktor:ktor-client-auth:$ktor_version"

implementation "io.dropwizard.metrics:metrics-healthchecks:$dropwizard_version"

Expand All @@ -79,7 +80,7 @@ dependencies {
buildConfig {
buildConfigField('String', 'APP_NAME', "\"${project.name}\"")
buildConfigField('String', 'APP_VERSION', "\"${project.version}\"")
buildConfigField('String', 'APP_RELEASE_DATE', "\"5/1/21\"")
buildConfigField('String', 'APP_RELEASE_DATE', "\"5/23/21\"")
}

compileKotlin.dependsOn ':generateProto'
Expand Down
2 changes: 1 addition & 1 deletion etc/compose/proxy.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
prometheus-proxy:
autoredeploy: true
image: 'pambrose/prometheus-proxy:1.9.0'
image: 'pambrose/prometheus-proxy:1.9.1'
ports:
- '8080:8080'
- '8082:8082'
Expand Down
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ org.gradle.parallel=true
org.gradle.caching=true
org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8
annotation_version=1.3.2
coroutines_version=1.5.0-RC
dropwizard_version=4.1.17
gengrpc_version=1.0.0
grpc_version=1.37.0
coroutines_version=1.5.0
dropwizard_version=4.2.0
gengrpc_version=1.1.0
grpc_version=1.38.0
jcommander_version=1.81
jetty_version=9.4.40.v20210413
jetty_version=9.4.41.v20210516
junit_version=5.7.1
kluent_version=1.65
kotlin_version=1.5.0
Expand All @@ -22,5 +22,5 @@ prometheus_version=0.10.0
protoc_version=3.12.4
slf4j_version=1.7.30
typesafe_version=1.4.1
utils_version=1.9.0
utils_version=1.10.0
zipkin_version=5.13.3
77 changes: 44 additions & 33 deletions src/main/kotlin/io/prometheus/agent/AgentHttpService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@
package io.prometheus.agent

import com.github.pambrose.common.dsl.KtorDsl.get
import com.github.pambrose.common.util.isNotNull
import com.github.pambrose.common.util.isNull
import com.github.pambrose.common.util.simpleClassName
import com.github.pambrose.common.util.zip
import com.google.common.net.HttpHeaders
import com.google.common.net.HttpHeaders.ACCEPT
import io.ktor.client.*
import io.ktor.client.engine.cio.*
import io.ktor.client.features.*
import io.ktor.client.features.auth.*
import io.ktor.client.features.auth.providers.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
Expand All @@ -39,13 +43,16 @@ import java.io.IOException
import java.net.URLDecoder
import java.net.http.HttpConnectTimeoutException
import java.util.concurrent.atomic.AtomicReference
import kotlin.text.Charsets.UTF_8
import kotlin.time.Duration.Companion.seconds

internal class AgentHttpService(val agent: Agent) {

suspend fun fetchScrapeUrl(request: ScrapeRequest): ScrapeResults =
ScrapeResults(agentId = request.agentId,
scrapeId = request.scrapeId).also { scrapeResults ->
ScrapeResults(
agentId = request.agentId,
scrapeId = request.scrapeId
).also { scrapeResults ->
val scrapeMsg = AtomicReference("")
val path = request.path
val encodedQueryParams = request.encodedQueryParams
Expand All @@ -56,14 +63,13 @@ internal class AgentHttpService(val agent: Agent) {
scrapeMsg.set("invalid_path")
if (request.debugEnabled)
scrapeResults.setDebugInfo("None", "Invalid path: $path")
}
else {
} else {
val requestTimer = if (agent.isMetricsEnabled) agent.startTimer(agent) else null
// Add the incoming query params to the url
val url = pathContext.url +
(if (encodedQueryParams.isNotEmpty())
"?${URLDecoder.decode(encodedQueryParams, Charsets.UTF_8.name())}"
else "")
(if (encodedQueryParams.isNotEmpty())
"?${URLDecoder.decode(encodedQueryParams, UTF_8.name())}"
else "")

logger.debug { "Fetching $pathContext" }
if (encodedQueryParams.isNotEmpty())
Expand All @@ -77,82 +83,88 @@ internal class AgentHttpService(val agent: Agent) {
HttpClient(engine) {
expectSuccess = false
install(HttpTimeout)

val urlObj = Url(url)
val user = urlObj.user
val passwd = urlObj.password
if (user.isNotNull() && passwd.isNotNull()) {
install(Auth) {
basic {
username = user
password = passwd
}
}
}
}
.use { client ->
client.get(
url,
{
val accept: String? = request.accept
if (accept?.isNotEmpty() == true)
header(HttpHeaders.ACCEPT, accept)
request.accept?.also { if (it.isNotEmpty()) header(ACCEPT, it) }
val scrapeTimeout = seconds(agent.options.scrapeTimeoutSecs)
logger.debug { "Setting scrapeTimeoutSecs = $scrapeTimeout" }
logger.debug { "Setting scrapeTimeoutSecs = $scrapeTimeout" }
timeout { requestTimeoutMillis = scrapeTimeout.inWholeMilliseconds }
},
getBlock(url, scrapeResults, scrapeMsg, request.debugEnabled))
},
getBlock(url, scrapeResults, scrapeMsg, request.debugEnabled)
)
}
}
}
catch (e: TimeoutCancellationException) {
} catch (e: TimeoutCancellationException) {
logger.warn(e) { "fetchScrapeUrl() $e - $url" }
scrapeResults.statusCode = HttpStatusCode.RequestTimeout.value
scrapeResults.failureReason = e.message ?: e.simpleClassName

if (request.debugEnabled)
scrapeResults.setDebugInfo(url, "${e.simpleClassName} - ${e.message}")
}
catch (e: HttpConnectTimeoutException) {
} catch (e: HttpConnectTimeoutException) {
logger.warn(e) { "fetchScrapeUrl() $e - $url" }
scrapeResults.statusCode = HttpStatusCode.RequestTimeout.value
scrapeResults.failureReason = e.message ?: e.simpleClassName

if (request.debugEnabled)
scrapeResults.setDebugInfo(url, "${e.simpleClassName} - ${e.message}")
}
catch (e: SocketTimeoutException) {
} catch (e: SocketTimeoutException) {
logger.warn(e) { "fetchScrapeUrl() $e - $url" }
scrapeResults.statusCode = HttpStatusCode.RequestTimeout.value
scrapeResults.failureReason = e.message ?: e.simpleClassName

if (request.debugEnabled)
scrapeResults.setDebugInfo(url, "${e.simpleClassName} - ${e.message}")
}
catch (e: HttpRequestTimeoutException) {
} catch (e: HttpRequestTimeoutException) {
logger.warn(e) { "fetchScrapeUrl() $e - $url" }
scrapeResults.statusCode = HttpStatusCode.RequestTimeout.value
scrapeResults.failureReason = e.message ?: e.simpleClassName

if (request.debugEnabled)
scrapeResults.setDebugInfo(url, "${e.simpleClassName} - ${e.message}")
}
catch (e: IOException) {
} catch (e: IOException) {
logger.info { "Failed HTTP request: $url [${e.simpleClassName}: ${e.message}]" }
scrapeResults.statusCode = HttpStatusCode.NotFound.value
scrapeResults.failureReason = e.message ?: e.simpleClassName

if (request.debugEnabled)
scrapeResults.setDebugInfo(url, "${e.simpleClassName} - ${e.message}")
}
catch (e: Throwable) {
} catch (e: Throwable) {
logger.warn(e) { "fetchScrapeUrl() $e - $url" }
scrapeResults.failureReason = e.message ?: e.simpleClassName
scrapeResults.statusCode = HttpStatusCode.ServiceUnavailable.value

if (request.debugEnabled)
scrapeResults.setDebugInfo(url, "${e.simpleClassName} - ${e.message}")
}
finally {
} finally {
requestTimer?.observeDuration()
}
}

agent.updateScrapeCounter(agent, scrapeMsg.get())
}

private fun getBlock(url: String,
responseArg: ScrapeResults,
scrapeCounterMsg: AtomicReference<String>,
debugEnabled: Boolean): suspend (HttpResponse) -> Unit =
private fun getBlock(
url: String,
responseArg: ScrapeResults,
scrapeCounterMsg: AtomicReference<String>,
debugEnabled: Boolean
): suspend (HttpResponse) -> Unit =
{ response ->
responseArg.statusCode = response.status.value

Expand All @@ -171,8 +183,7 @@ internal class AgentHttpService(val agent: Agent) {
if (debugEnabled)
responseArg.setDebugInfo(url)
scrapeCounterMsg.set("success")
}
else {
} else {
if (debugEnabled)
responseArg.setDebugInfo(url, "Unsuccessful response code ${responseArg.statusCode}")
scrapeCounterMsg.set("unsuccessful")
Expand Down

0 comments on commit 9922828

Please sign in to comment.