Skip to content

Commit

Permalink
Add support for passing query params
Browse files Browse the repository at this point in the history
  • Loading branch information
pambrose committed Apr 30, 2020
1 parent 9dcd073 commit 7dd5441
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 11 deletions.
6 changes: 3 additions & 3 deletions examples/simple.conf
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ proxy {
}

agent {
proxy.hostname = ${HOSTNAME}
proxy.hostname = localhost
admin.enabled: true
metrics.enabled: true

pathConfigs: [
{
name: proxy
path: proxy_metrics
url: "http://"${HOSTNAME}":8082/metrics"
url: "http://localhost:8082/metrics"
}
{
name: agent
path: agent_metrics
url: "http://"${HOSTNAME}":8083/metrics"
url: "http://localhost:8083/metrics"
}
]
}
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ grpc_version=1.28.1
jcommander_version=1.78
jetty_version=9.4.22.v20191022
junit_version=5.6.1
kluent_version=1.60
kluent_version=1.61
ktor_version=1.3.2
logback_version=1.2.3
logging_version=1.7.9
Expand All @@ -14,5 +14,5 @@ protoc_version=3.11.4
serialization_version=0.20.0-1.3.70-eap-274-2
slf4j_version=1.7.28
typesafe_version=1.4.0
utils_version=5e83440
utils_version=b107c71
zipkin_version=5.11.2
2 changes: 1 addition & 1 deletion src/main/java/io/prometheus/package-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

@VersionAnnotation(version = "1.6.4", date = "3/6/20")
@VersionAnnotation(version = "1.6.4", date = "4/29/20")
package io.prometheus;

import io.prometheus.common.VersionAnnotation;
7 changes: 6 additions & 1 deletion src/main/kotlin/io/prometheus/agent/AgentHttpService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import io.prometheus.common.ScrapeResults
import io.prometheus.grpc.ScrapeRequest
import mu.KLogging
import java.io.IOException
import java.net.URLDecoder
import java.util.concurrent.atomic.AtomicReference

class AgentHttpService(val agent: Agent) {
Expand All @@ -41,6 +42,7 @@ class AgentHttpService(val agent: Agent) {
ScrapeResults(agentId = request.agentId, scrapeId = request.scrapeId).also { scrapeResults ->
val scrapeMsg = AtomicReference("")
val path = request.path
val encodedQueryParams = request.encodedQueryParams
val pathContext = agent.pathManager[path]

if (pathContext == null) {
Expand All @@ -51,7 +53,10 @@ class AgentHttpService(val agent: Agent) {
}
else {
val requestTimer = if (agent.isMetricsEnabled) agent.startTimer() else null
val url = pathContext.url
val url = pathContext.url +
(if (encodedQueryParams.isNotEmpty())
"?${URLDecoder.decode(encodedQueryParams, Charsets.UTF_8)}"
else "")
logger.debug { "Fetching $pathContext" }

// Content is fetched here
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/io/prometheus/common/GrpcObjects.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,15 @@ object GrpcObjects {
fun newScrapeRequest(agentId: String,
scrapeId: Long,
path: String,
encodedQueryParams: String,
accept: String?,
debugEnabled: Boolean): ScrapeRequest {
require(agentId.isNotEmpty()) { EMPTY_AGENTID }
return ScrapeRequest.newBuilder().let { builder ->
builder.agentId = agentId
builder.scrapeId = scrapeId
builder.path = path
builder.encodedQueryParams = encodedQueryParams
builder.debugEnabled = debugEnabled
if (!accept.isNullOrBlank())
builder.accept = accept
Expand Down
16 changes: 12 additions & 4 deletions src/main/kotlin/io/prometheus/proxy/ProxyHttpService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

@file:Suppress("UndocumentedPublicClass", "UndocumentedPublicFunction")
@file:Suppress("UndocumentedPublicClass", "UndocumentedPublicFunction", "UnstableApiUsage")

package io.prometheus.proxy

Expand All @@ -36,6 +36,7 @@ import io.ktor.features.minimumSize
import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.HttpStatusCode
import io.ktor.http.formUrlEncode
import io.ktor.http.isSuccess
import io.ktor.request.ApplicationRequest
import io.ktor.request.header
Expand Down Expand Up @@ -82,10 +83,15 @@ class ProxyHttpService(private val proxy: Proxy, val httpPort: Int) : GenericIdl
call.response.header(HttpHeaders.CacheControl, "must-revalidate,no-cache,no-store")

val path = call.request.path().drop(1)
logger.debug { "Servicing request for path: $path" }
val encodedQueryParams = call.request.queryParameters.formUrlEncode()
val agentContext = proxy.pathManager[path]
val responseResults = ResponseResults()

logger.debug {
"Servicing request for path: $path" +
(if (encodedQueryParams.isNotEmpty()) " with query params $encodedQueryParams" else "")
}

when {
!proxy.isRunning -> {
logger.error { "Proxy stopped" }
Expand All @@ -108,7 +114,7 @@ class ProxyHttpService(private val proxy: Proxy, val httpPort: Int) : GenericIdl
path == "favicon.ico" -> {
//logger.info { "Invalid path request /${path}" }
responseResults.apply {
updateMsg = "invalid_request"
updateMsg = "invalid_path"
statusCode = HttpStatusCode.NotFound
}
}
Expand Down Expand Up @@ -137,7 +143,7 @@ class ProxyHttpService(private val proxy: Proxy, val httpPort: Int) : GenericIdl
}

else -> {
submitScrapeRequest(path, agentContext, call.request, call.response)
submitScrapeRequest(path, encodedQueryParams, agentContext, call.request, call.response)
.also { response ->

var status = "/${path} - ${response.updateMsg} - ${response.statusCode}"
Expand Down Expand Up @@ -204,12 +210,14 @@ class ProxyHttpService(private val proxy: Proxy, val httpPort: Int) : GenericIdl
val fetchDuration: Duration)

private suspend fun submitScrapeRequest(path: String,
encodedQueryParams: String,
agentContext: AgentContext,
request: ApplicationRequest,
response: ApplicationResponse): ScrapeRequestResponse {

val scrapeRequest = ScrapeRequestWrapper(proxy,
path,
encodedQueryParams,
agentContext,
request.header(ACCEPT),
proxy.options.debugEnabled)
Expand Down
2 changes: 2 additions & 0 deletions src/main/kotlin/io/prometheus/proxy/ScrapeRequestWrapper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import kotlin.time.TimeSource.Monotonic

class ScrapeRequestWrapper(proxy: Proxy,
path: String,
encodedQueryParams: String,
val agentContext: AgentContext,
accept: String?,
debugEnabled: Boolean) {
Expand All @@ -43,6 +44,7 @@ class ScrapeRequestWrapper(proxy: Proxy,
val scrapeRequest = newScrapeRequest(agentContext.agentId,
SCRAPE_ID_GENERATOR.getAndIncrement(),
path,
encodedQueryParams,
accept,
debugEnabled)

Expand Down
1 change: 1 addition & 0 deletions src/main/proto/proxy_service.proto
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ message ScrapeRequest {
string path = 3;
string accept = 4;
bool debug_enabled = 5;
string encodedQueryParams = 6;
}

message ScrapeResponse {
Expand Down

0 comments on commit 7dd5441

Please sign in to comment.