-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Micrometer Prometheus timer interceptor (#29)
* Add Micrometer & Prometheus counter interceptor * Formatting code * Remove spring boot actuator dependency * Formatting code
- Loading branch information
1 parent
cb9937b
commit ac9ad45
Showing
7 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
...tter/sisyphus/middleware/configuration/SpringApplicationConfigArtifactPropertyExporter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.bybutter.sisyphus.middleware.configuration | ||
|
||
object SpringApplicationConfigArtifactPropertyExporter : ConfigFormatFilePropertyExporter() { | ||
override val names: Collection<String> = listOf("spring/application") | ||
} |
1 change: 1 addition & 0 deletions
1
...NF/services/com.bybutter.sisyphus.middleware.configuration.ConfigArtifactPropertyExporter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
com.bybutter.sisyphus.middleware.configuration.SpringApplicationConfigArtifactPropertyExporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...-server-starter/src/main/kotlin/com/bybutter/sisyphus/starter/grpc/MicrometerRegistrar.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.bybutter.sisyphus.starter.grpc | ||
|
||
import io.micrometer.core.instrument.MeterRegistry | ||
import io.micrometer.core.instrument.Timer | ||
import java.time.Duration | ||
import org.springframework.stereotype.Component | ||
|
||
/** | ||
* When Sisyphus calls other service interfaces, It will give priority to see if the service is referenced locally, | ||
* if requested, request the local service interfaces. | ||
* The remote request will be initiated if the local request is not available. | ||
* Requesting local services has little overhead for network requests, | ||
* So it may be inaccurate to calculate the average time of the request。 | ||
* Use two [Timer] to count remote request and all requests separately. | ||
* */ | ||
@Component | ||
class MicrometerRegistrar constructor(private var registry: MeterRegistry) { | ||
/** | ||
* Only record remote requests | ||
* */ | ||
private val remoteRequestTimer: Timer by lazy { | ||
registry.timer("sisyphus_remote_request") | ||
} | ||
/** | ||
* Log all requests | ||
* */ | ||
private val allRequestTimer: Timer by lazy { | ||
registry.timer("sisyphus_all_request") | ||
} | ||
|
||
fun incrRemoteRequest(duration: Duration) { | ||
remoteRequestTimer.record(duration) | ||
} | ||
|
||
fun incrAllRequest(duration: Duration) { | ||
allRequestTimer.record(duration) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...arter/src/main/kotlin/com/bybutter/sisyphus/starter/grpc/support/MicrometerInterceptor.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.bybutter.sisyphus.starter.grpc.support | ||
|
||
import com.bybutter.sisyphus.starter.grpc.MicrometerRegistrar | ||
import io.grpc.Context | ||
import io.grpc.Contexts | ||
import io.grpc.ForwardingServerCall | ||
import io.grpc.Metadata | ||
import io.grpc.ServerCall | ||
import io.grpc.ServerCallHandler | ||
import io.grpc.ServerInterceptor | ||
import io.grpc.Status | ||
import java.time.Duration | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.stereotype.Component | ||
|
||
@Component | ||
class ServerMicrometerInterceptor : ServerInterceptor { | ||
@Autowired | ||
private lateinit var micrometerRegistrar: MicrometerRegistrar | ||
|
||
override fun <ReqT : Any, RespT : Any> interceptCall(call: ServerCall<ReqT, RespT>, headers: Metadata, next: ServerCallHandler<ReqT, RespT>): ServerCall.Listener<ReqT> { | ||
val host = headers.get(Metadata.Key.of("host", Metadata.ASCII_STRING_MARSHALLER))?.toString() | ||
return Contexts.interceptCall(Context.current(), ServerMicrometerCall(call, micrometerRegistrar, host), headers, next) | ||
} | ||
} | ||
|
||
class ServerMicrometerCall<ReqT : Any, RespT : Any>(call: ServerCall<ReqT, RespT>, private val micrometerRegistrar: MicrometerRegistrar, private val host: String?) : ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) { | ||
override fun close(status: Status, trailers: Metadata) { | ||
val costDuration = Duration.ofNanos(System.nanoTime() - REQUEST_TIMESTAMP_CONTEXT_KEY.get()) | ||
micrometerRegistrar.incrAllRequest(costDuration) | ||
if (host != null && !host.startsWith("localhost")) { | ||
micrometerRegistrar.incrRemoteRequest(costDuration) | ||
} | ||
super.close(status, trailers) | ||
} | ||
} |