Skip to content

Commit

Permalink
1.23.1 (#107)
Browse files Browse the repository at this point in the history
* Catch and report exceptions on labels json deserialization
* Update ktor and logging jars
* Fix platform type issue
* Default Plain content types to charset UTF-8
* Replace explicit size check with .isNotEmpty() call
* Add charset to JSON values
* Add logging to --debug option
  • Loading branch information
pambrose authored Dec 10, 2024
1 parent 0db8810 commit d8f9a97
Show file tree
Hide file tree
Showing 21 changed files with 110 additions and 94 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION=1.23.0
VERSION=1.23.1

default: versioncheck

Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ agent {
{
name: "App2 metrics"
path: app2_metrics
labels: "{\"key3\": \"value3\", \"key4\": \"value4\"}"
labels: "{\"key3\": \"value3\", \"key4\": 4}"
url: "http://app2.local:9100/metrics"
},
{
name: "App3 metrics"
path: app3_metrics
labels: "{\"key5\": \"value5\", \"key6\": \"value6\"}"
labels: "{\"key5\": \"value5\", \"key6\": 6}"
url: "http://app3.local:9100/metrics"
}
]
Expand Down Expand Up @@ -116,8 +116,8 @@ scrape_configs:
The docker images are available via:
```bash
docker pull pambrose/prometheus-proxy:1.23.0
docker pull pambrose/prometheus-agent:1.23.0
docker pull pambrose/prometheus-proxy:1.23.1
docker pull pambrose/prometheus-agent:1.23.1
```

Start a proxy container with:
Expand All @@ -126,15 +126,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.23.0
pambrose/prometheus-proxy:1.23.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.23.0
pambrose/prometheus-agent:1.23.1
```

Using the config
Expand All @@ -152,7 +152,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.23.0
pambrose/prometheus-agent:1.23.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 @@ -303,15 +303,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.23.0
pambrose/prometheus-proxy:1.23.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.23.0
pambrose/prometheus-agent:1.23.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.23.0
pambrose/prometheus-agent:1.23.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.23.0
pambrose/prometheus-proxy:1.23.1
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ plugins {
}

group = "io.prometheus"
version = "1.23.0"
version = "1.23.1"

buildConfig {
packageName("io.prometheus")
buildConfigField("String", "APP_NAME", "\"${project.name}\"")
buildConfigField("String", "APP_VERSION", "\"${project.version}\"")
buildConfigField("String", "APP_RELEASE_DATE", "\"11/29/2024\"")
buildConfigField("String", "APP_RELEASE_DATE", "\"12/09/2024\"")
buildConfigField("long", "BUILD_TIME", "${System.currentTimeMillis()}L")
}

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.23.0'
image: 'pambrose/prometheus-proxy:1.23.1'
ports:
- '8080:8080'
- '8082:8082'
Expand Down
4 changes: 2 additions & 2 deletions examples/myapps.conf
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ agent {
{
name: "App2 metrics"
path: app2_metrics
labels: "{\"key3\": \"value3\", \"key4\": \"value4\"}"
labels: "{\"key3\": \"value3\", \"key4\": 4}"
url: "http://app2.local:9100/metrics"
},
{
name: "App3 metrics"
path: app3_metrics
labels: "{\"key5\": \"value5\", \"key6\": \"value6\"}"
labels: "{\"key5\": \"value5\", \"key6\": 6}"
url: "http://app3.local:9100/metrics"
}
]
Expand Down
4 changes: 2 additions & 2 deletions examples/simple.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ agent {
{
name: "Proxy metrics"
path: proxy_metrics
labels: "{\"key1\": \"value1\", \"key2\": \"value2\"}"
labels: "{\"key1\": \"value1\", \"key2\": 2}"
url: "http://localhost:8082/metrics"
//url: "http://"${?HOSTNAME}":8082/metrics"
}
{
name: "Agent metrics"
path: agent_metrics
labels: "{\"key3\": \"value3\", \"key4\": \"value4\"}"
labels: "{\"key3\": \"value3\", \"key4\": 4}"
url: "http://localhost:8083/metrics"
//url: "http://"${?HOSTNAME}":8083/metrics"
}
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ jettyVersion=10.0.24
junitVersion=5.11.3
kluentVersion=1.73
kotlinVersion=2.1.0
ktorVersion=3.0.1
ktorVersion=3.0.2
logbackVersion=1.5.12
loggingVersion=7.0.0
loggingVersion=7.0.3
# Keep in sync with grpc
tcnativeVersion=2.0.69.Final
prometheusVersion=0.16.0
Expand All @@ -41,5 +41,5 @@ protocVersion=3.25.4
serializationVersion=1.7.3
slf4jVersion=2.0.13
typesafeVersion=1.4.3
utilsVersion=2.3.1
utilsVersion=2.3.4
zipkinVersion=6.0.3
11 changes: 8 additions & 3 deletions src/main/kotlin/io/prometheus/Proxy.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import com.github.pambrose.common.time.format
import com.github.pambrose.common.util.MetricsUtils.newMapHealthCheck
import com.github.pambrose.common.util.getBanner
import com.github.pambrose.common.util.isNotNull
import com.github.pambrose.common.util.simpleClassName
import com.google.common.base.Joiner
import com.google.common.collect.EvictingQueue
import io.github.oshai.kotlinlogging.KotlinLogging
Expand Down Expand Up @@ -127,7 +128,7 @@ class Proxy(
listOf(
toPlainText(),
pathManager.toPlainText(),
if (recentReqs.size > 0) "\n${recentReqs.size} most recent requests:" else "",
if (recentReqs.isNotEmpty()) "\n${recentReqs.size} most recent requests:" else "",
recentReqs.reversed().joinToString("\n"),
).joinToString("\n")
},
Expand Down Expand Up @@ -255,8 +256,12 @@ class Proxy(
put("hostName", JsonPrimitive(agentContexts.joinToString { it.hostName }))

val labels = agentContextInfo.labels
val json = labels.toJsonElement()
json.jsonObject.forEach { (k, v) -> put(k, v) }
runCatching {
val json = labels.toJsonElement()
json.jsonObject.forEach { (k, v) -> put(k, v) }
}.onFailure { e ->
logger.warn { "Invalid JSON in labels value: $labels - ${e.simpleClassName}: ${e.message}" }
}
} else {
logger.warn { "No agent context info for path: $path" }
}
Expand Down
23 changes: 10 additions & 13 deletions src/main/kotlin/io/prometheus/agent/AgentGrpcService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ internal class AgentGrpcService(
it.labels = labelsJson
}
.build()
return stub.registerPath(request)//.toDataClass()
return stub.registerPath(request)
.apply {
agent.markMsgSent()
if (!valid)
Expand All @@ -246,7 +246,7 @@ internal class AgentGrpcService(
it.agentId = agent.agentId
it.path = path
}.build()
return stub.unregisterPath(request)//.toDataClass()
return stub.unregisterPath(request)
.apply {
agent.markMsgSent()
if (!valid)
Expand All @@ -260,7 +260,7 @@ internal class AgentGrpcService(
if (agentId.isNotEmpty())
runCatching {
val request = HeartBeatRequest.newBuilder().also { it.agentId = agentId }.build()
stub.sendHeartBeat(request)//.toDataClass()
stub.sendHeartBeat(request)
.apply {
agent.markMsgSent()
if (!valid) {
Expand Down Expand Up @@ -294,9 +294,8 @@ internal class AgentGrpcService(
.readRequestsFromProxy(agentInfo)
.collect { grpcRequest: ScrapeRequest ->
// The actual fetch happens at the other end of the channel, not here.
val request = grpcRequest//.toDataClass()
logger.debug { "readRequestsFromProxy():\n$request" }
connectionContext.scrapeRequestsChannel.send { agentHttpService.fetchScrapeUrl(request) }
logger.debug { "readRequestsFromProxy():\n$grpcRequest" }
connectionContext.scrapeRequestsChannel.send { agentHttpService.fetchScrapeUrl(grpcRequest) }
agent.scrapeRequestBacklogSize.incrementAndGet()
}
}
Expand All @@ -313,7 +312,7 @@ internal class AgentGrpcService(

if (!scrapeResults.zipped) {
logger.debug { "Writing non-chunked msg scrapeId: $scrapeId length: ${scrapeResults.contentAsText.length}" }
nonChunkedChannel.send(scrapeResults.toScrapeResponse()/*.toProto()*/)
nonChunkedChannel.send(scrapeResults.toScrapeResponse())
agent.metrics { scrapeResultCount.labels(agent.launchId, "non-gzipped").inc() }
} else {
val zipped = scrapeResults.contentAsZipped
Expand All @@ -323,7 +322,7 @@ internal class AgentGrpcService(

if (zipped.size < chunkContentSize) {
logger.debug { "Writing zipped non-chunked msg scrapeId: $scrapeId length: ${zipped.size}" }
nonChunkedChannel.send(scrapeResults.toScrapeResponse() /*.toProto()*/)
nonChunkedChannel.send(scrapeResults.toScrapeResponse())
agent.metrics { scrapeResultCount.labels(agent.launchId, "gzipped").inc() }
} else {
scrapeResults.toScrapeResponseHeader()
Expand All @@ -347,14 +346,14 @@ internal class AgentGrpcService(
newScrapeResponseChunk(scrapeId, totalChunkCount, readByteCount, checksum, buffer)
.also {
logger.debug { "Writing chunk $totalChunkCount for scrapeId: $scrapeId" }
chunkedChannel.send(it/*.toProto()*/)
chunkedChannel.send(it)
}
}

newScrapeResponseSummary(scrapeId, totalChunkCount, totalByteCount, checksum)
.also {
logger.debug { "Writing summary totalChunkCount: $totalChunkCount for scrapeID: $scrapeId" }
chunkedChannel.send(it /*.toProto()*/)
chunkedChannel.send(it)
agent.metrics { scrapeResultCount.labels(agent.launchId, "chunked").inc() }
}
}
Expand All @@ -376,9 +375,7 @@ internal class AgentGrpcService(
CoroutineExceptionHandler { _, e ->
if (agent.isRunning)
Status.fromThrowable(e)
.apply {
logger.error { "Error in writeResponsesToProxyUntilDisconnected(): $code $description" }
}
.apply { logger.error { "Error in writeResponsesToProxyUntilDisconnected(): $code $description" } }
}

coroutineScope {
Expand Down
4 changes: 3 additions & 1 deletion src/main/kotlin/io/prometheus/agent/AgentHttpService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ internal class AgentHttpService(
val url = pathContext.url + decodeParams(scrapeRequest.encodedQueryParams)
logger.debug { "Fetching $pathContext ${if (url.isNotBlank()) "URL: $url" else ""}" }

// Content is fetched here
try {
// Content is fetched here
fetchContent(url, scrapeRequest, scrapeResults)
} finally {
requestTimer?.observeDuration()
Expand Down Expand Up @@ -132,6 +132,8 @@ internal class AgentHttpService(
with(scrapeResults) {
if (response.status.isSuccess()) {
contentType = response.headers[CONTENT_TYPE].orEmpty()
if (agent.options.debugEnabled)
logger.info { "CT check - setScrapeDetailsAndDebugInfo() contentType: $contentType" }
// Zip the content here
val content = response.bodyAsText()
zipped = content.length > agent.configVals.agent.minGzipSizeBytes
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/io/prometheus/agent/AgentPathManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ internal class AgentPathManager(
val labelsJson = labels.defaultEmptyJsonObject()
val pathId = agent.grpcService.registerPathOnProxy(path, labelsJson).pathId
if (!agent.isTestMode)
logger.info { "Registered $url as /$path with labels: $labelsJson" }
logger.info { "Registered $url as /$path with labels $labelsJson" }
pathContextMap[path] = PathContext(pathId, path, url, labelsJson)
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/io/prometheus/common/GrpcObjects.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ internal object GrpcObjects {
}
.build()
}
.build()
.build()!!

fun newScrapeResponseSummary(
scrapeId: Long,
Expand All @@ -86,5 +86,5 @@ internal object GrpcObjects {
}
.build()
}
.build()
.build()!!
}
4 changes: 2 additions & 2 deletions src/main/kotlin/io/prometheus/common/ScrapeResults.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ internal class ScrapeResults(
it.failureReason = failureReason
it.url = url
}
.build()
.build()!!

fun toScrapeResponseHeader() =
ChunkedScrapeResponse
Expand All @@ -92,7 +92,7 @@ internal class ScrapeResults(
it.headerContentType = contentType
}
.build()
}.build()
}.build()!!

companion object {
private val logger = KotlinLogging.logger {}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/io/prometheus/proxy/ProxyHttpConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package io.prometheus.proxy

import com.github.pambrose.common.util.simpleClassName
import io.github.oshai.kotlinlogging.KotlinLogging
import io.ktor.http.ContentType.Text.Plain
import io.ktor.http.ContentType.Text
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
import io.ktor.http.HttpStatusCode.Companion.NotFound
Expand Down Expand Up @@ -105,7 +105,7 @@ internal object ProxyHttpConfig {
}

status(NotFound) { call, cause ->
call.respond(TextContent("${cause.value} ${cause.description}", Plain.withCharset(Charsets.UTF_8), cause))
call.respond(TextContent("${cause.value} ${cause.description}", Text.Plain.withCharset(Charsets.UTF_8), cause))
}
}
}
Loading

0 comments on commit d8f9a97

Please sign in to comment.