Skip to content

Commit 0d28c57

Browse files
Observability support (#3)
* observability support * metrics, tracing keep going, add unit tests
1 parent 627d08f commit 0d28c57

38 files changed

Lines changed: 1042 additions & 0 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ _**Make the integration of gRPC with Spring Boot feel seamless and native.**_
2929

3030
- **JSON transcoder, one set of code supports both gRPC and HTTP/JSON.**
3131
- **Protobuf validation, implemented by [protoc-gen-validate](https://github.com/bufbuild/protoc-gen-validate).**
32+
- Metric, Spring Boot Actuator integration with gRPC service.
33+
- Tracing, Spring Boot Actuator integration with gRPC server and client.
3234
- Testing support.
3335

3436
## Quick Start

docs/zh-cn/extension/metrcis.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## Overview
2+
3+
Metrics 扩展了 Spring Boot Actuator,为 gRPC 服务端和客户端提供了指标采集功能。
4+
5+
> Metrics extension works with Spring Boot 3.1.0+
6+
7+
## Quick Start
8+
9+
1. 引入依赖
10+
11+
```groovy
12+
implementation("com.freemanan:grpc-starter-metrics")
13+
// Actuator 使用 Micrometer 作为指标采集门面,这里使用 Prometheus
14+
// Micrometer 支持的指标采集器可以参考 https://micrometer.io/docs/
15+
implementation("io.micrometer:micrometer-registry-prometheus")
16+
```
17+
18+
2. 配置 Prometheus 端点
19+
20+
```yaml
21+
management:
22+
endpoints:
23+
web:
24+
exposure:
25+
include: prometheus
26+
```
27+
28+
3. Visit endpoint
29+
30+
```shell
31+
curl http://localhost:8080/actuator/prometheus
32+
```
33+
34+
See [examples/metrics](https://github.com/DanielLiu1123/grpc-starter/tree/main/examples/metrics) for more details.
35+
36+
## Annotation Driven
37+
38+
Micrometer 提供了一些开箱即用的注解,可以用于对方法的指标采集,Metrics 扩展提供了对这些注解的支持。
39+
40+
```java
41+
@Controller
42+
public class SimpleServiceImpl extends SimpleServiceGrpc.SimpleServiceImplBase {
43+
44+
@Override
45+
@Timed("simple.unaryRpc")
46+
@Counted("simple.unaryRpc")
47+
public void unaryRpc(SimpleRequest request, StreamObserver<SimpleResponse> responseObserver) {
48+
SimpleResponse response = SimpleResponse.newBuilder()
49+
.setResponseMessage("Hello " + request.getRequestMessage())
50+
.build();
51+
responseObserver.onNext(response);
52+
responseObserver.onCompleted();
53+
}
54+
}
55+
```
56+
57+
> 使用注解方式添加指标需要添加 `spring-boot-starter-aop` 依赖!
58+
59+
## Related Configurations
60+
61+
```yaml
62+
grpc:
63+
metrics:
64+
enabled: true # enable metrics
65+
client:
66+
enabled: true # enable metrics for client
67+
order: 0 # order of metrics client interceptor
68+
server:
69+
enabled: true # enable metrics for server
70+
order: 0 # order of metrics server interceptor
71+
```

docs/zh-cn/extension/tracing.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## Overview
2+
3+
Tracing 扩展了 Spring Boot Actuator,为 gRPC 服务端和客户端提供了链路追踪功能。
4+
5+
> Tracing extension works with Spring Boot 3.1.0+
6+
7+
## Quick Start
8+
9+
1. Start Zipkin
10+
11+
```shell
12+
docker run -t -p 9411:9411 openzipkin/zipkin
13+
```
14+
15+
2. 引入依赖
16+
17+
```groovy
18+
implementation("com.freemanan:grpc-starter-tracing")
19+
// Micrometer 支持的 Tracing system 可以参考 https://micrometer.io/docs/tracing
20+
implementation("io.micrometer:micrometer-tracing-bridge-brave")
21+
```
22+
23+
3. 配置 Zipkin 上报地址
24+
25+
```yaml
26+
management:
27+
zipkin:
28+
tracing:
29+
endpoint: http://localhost:9411/api/v2/spans
30+
tracing:
31+
sampling:
32+
probability: 1.0
33+
```
34+
35+
4. Visit Zipkin UI
36+
37+
```shell
38+
curl http://localhost:9411
39+
```
40+
41+
See [examples/tracing](https://github.com/DanielLiu1123/grpc-starter/tree/main/examples/tracing) for more details.
42+
43+
## Related Configurations
44+
45+
```yaml
46+
grpc:
47+
tracing:
48+
enabled: true # enable tracing
49+
client:
50+
enabled: true # enable tracing for client
51+
order: 0 # order of tracing client interceptor
52+
server:
53+
enabled: true # enable tracing for server
54+
order: 0 # order of tracing server interceptor
55+
```

examples/metrics/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1. Start application
2+
3+
2. Visit http://localhost:8080/actuator/prometheus

examples/metrics/build.gradle

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
plugins {
2+
id 'org.springframework.boot'
3+
}
4+
5+
dependencies {
6+
implementation(project(":examples:grpc-sample-api"))
7+
implementation(project(":grpc-starters:grpc-boot-starter"))
8+
implementation(project(":grpc-extensions:grpc-metrics"))
9+
10+
implementation("org.springframework.boot:spring-boot-starter-web")
11+
implementation("org.springframework.boot:spring-boot-starter-aop")
12+
implementation("io.micrometer:micrometer-registry-prometheus")
13+
14+
testImplementation(project(":grpc-starters:grpc-starter-test"))
15+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.freemanan.example;
2+
3+
import com.freemanan.starter.grpc.client.EnableGrpcClients;
4+
import io.grpc.testing.protobuf.SimpleRequest;
5+
import io.grpc.testing.protobuf.SimpleResponse;
6+
import io.grpc.testing.protobuf.SimpleServiceGrpc;
7+
import io.micrometer.core.instrument.Counter;
8+
import io.micrometer.core.instrument.MeterRegistry;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.boot.ApplicationArguments;
11+
import org.springframework.boot.ApplicationRunner;
12+
import org.springframework.boot.SpringApplication;
13+
import org.springframework.boot.autoconfigure.SpringBootApplication;
14+
15+
/**
16+
* @author Freeman
17+
*/
18+
@SpringBootApplication
19+
@EnableGrpcClients("io.grpc")
20+
public class App implements ApplicationRunner {
21+
private final Counter counter;
22+
23+
public App(MeterRegistry mr) {
24+
this.counter = Counter.builder("app_run")
25+
.description("app run")
26+
.tags("app", "metrics-test")
27+
.register(mr);
28+
}
29+
30+
@Autowired
31+
SimpleServiceGrpc.SimpleServiceBlockingStub stub;
32+
33+
public static void main(String[] args) {
34+
SpringApplication.run(App.class, args);
35+
}
36+
37+
@Override
38+
public void run(ApplicationArguments args) throws Exception {
39+
counter.increment();
40+
SimpleResponse resp = stub.unaryRpc(
41+
SimpleRequest.newBuilder().setRequestMessage("Hello").build());
42+
System.out.println(resp);
43+
}
44+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.freemanan.example.controller;
2+
3+
import io.grpc.stub.StreamObserver;
4+
import io.grpc.testing.protobuf.SimpleRequest;
5+
import io.grpc.testing.protobuf.SimpleResponse;
6+
import io.grpc.testing.protobuf.SimpleServiceGrpc;
7+
import io.micrometer.core.annotation.Counted;
8+
import io.micrometer.core.annotation.Timed;
9+
import org.springframework.stereotype.Controller;
10+
11+
/**
12+
* @author Freeman
13+
*/
14+
@Controller
15+
public class SimpleServiceImpl extends SimpleServiceGrpc.SimpleServiceImplBase {
16+
17+
@Override
18+
@Timed("simple.unaryRpc")
19+
@Counted("simple.unaryRpc")
20+
public void unaryRpc(SimpleRequest request, StreamObserver<SimpleResponse> responseObserver) {
21+
SimpleResponse response = SimpleResponse.newBuilder()
22+
.setResponseMessage("Hello " + request.getRequestMessage())
23+
.build();
24+
responseObserver.onNext(response);
25+
responseObserver.onCompleted();
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
spring:
2+
application:
3+
name: metrics-example
4+
5+
grpc:
6+
server:
7+
reflection:
8+
enabled: true
9+
client:
10+
authority: localhost:${grpc.server.port:9090}
11+
12+
management:
13+
endpoints:
14+
web:
15+
exposure:
16+
include: '*'
17+
metrics:
18+
tags:
19+
app: ${spring.application.name:UNKNOWN}
20+
debug: true
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.freemanan.example;
2+
3+
class AppTest {}

examples/tracing/README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
1. Start Zipkin server
2+
3+
```shell
4+
docker run -it -p 9411:9411 openzipkin/zipkin
5+
```
6+
7+
2. Start the example
8+
9+
3. Visit http://localhost:9411/zipkin/ to view the traces
10+

0 commit comments

Comments
 (0)