Skip to content

Commit 7ef4259

Browse files
committed
Add OpenTelemetry example
1 parent bb49a88 commit 7ef4259

File tree

3 files changed

+178
-0
lines changed

3 files changed

+178
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# OpenTelemetry
2+
3+
This example shows how to configure OpenTelemetry on a client and server, and shows
4+
what type of telemetry data it can produce for certain RPC's.
5+
6+
## Try it
7+
8+
```
9+
go run server/main.go
10+
```
11+
12+
```
13+
go run client/main.go
14+
```
15+
16+
```
17+
curl localhost:9464/metrics
18+
curl localhost:9465/metrics
19+
```
20+
21+
## Explanation
22+
23+
The client continuously makes RPC's to a server. The client and server both
24+
expose a prometheus exporter to listen and provide metrics. This defaults to
25+
:9464 for the server and :9465 for the client.
26+
27+
OpenTelemetry is configured on both the client and the server, and exports to
28+
the Prometheus exporter. The exporter exposes metrics on the Prometheus ports
29+
described above.
30+
31+
Curling to the exposed Prometheus ports outputs the metrics recorded on the
32+
client and server.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
*
3+
* Copyright 2024 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package main
20+
21+
import (
22+
"context"
23+
"flag"
24+
"fmt"
25+
"log"
26+
"net/http"
27+
"time"
28+
29+
"google.golang.org/grpc"
30+
"google.golang.org/grpc/credentials/insecure"
31+
"google.golang.org/grpc/examples/features/proto/echo"
32+
"google.golang.org/grpc/stats/opentelemetry"
33+
34+
"github.com/prometheus/client_golang/prometheus/promhttp"
35+
"go.opentelemetry.io/otel/exporters/prometheus"
36+
"go.opentelemetry.io/otel/sdk/metric"
37+
)
38+
39+
var (
40+
addr = flag.String("addr", ":50051", "the server address to connect to")
41+
prometheusEndpoint = flag.String("prometheus_endpoint", ":9465", "the Prometheus exporter endpoint")
42+
)
43+
44+
func main() {
45+
exporter, err := prometheus.New()
46+
if err != nil {
47+
log.Fatalf("Failed to start prometheus exporter: %v", err)
48+
}
49+
provider := metric.NewMeterProvider(metric.WithReader(exporter))
50+
go http.ListenAndServe(*prometheusEndpoint, promhttp.Handler())
51+
52+
ctx := context.Background()
53+
do := opentelemetry.DialOption(opentelemetry.Options{MetricsOptions: opentelemetry.MetricsOptions{MeterProvider: provider}})
54+
55+
cc, err := grpc.NewClient(*addr, grpc.WithTransportCredentials(insecure.NewCredentials()), do)
56+
if err != nil {
57+
log.Fatalf("Failed to start NewClient: %v", err)
58+
}
59+
defer cc.Close()
60+
c := echo.NewEchoClient(cc)
61+
62+
// Make a RPC every second. This should trigger telemetry to be emitted from
63+
// the client and the server.
64+
for {
65+
r, err := c.UnaryEcho(ctx, &echo.EchoRequest{Message: "this is examples/opentelemetry"})
66+
if err != nil {
67+
log.Fatalf("UnaryEcho failed: %v", err)
68+
}
69+
fmt.Println(r)
70+
time.Sleep(time.Second)
71+
}
72+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
*
3+
* Copyright 2024 gRPC authors.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package main
20+
21+
import (
22+
"context"
23+
"flag"
24+
"fmt"
25+
"log"
26+
"net"
27+
"net/http"
28+
29+
"google.golang.org/grpc"
30+
pb "google.golang.org/grpc/examples/features/proto/echo"
31+
"google.golang.org/grpc/stats/opentelemetry"
32+
33+
"github.com/prometheus/client_golang/prometheus/promhttp"
34+
"go.opentelemetry.io/otel/exporters/prometheus"
35+
"go.opentelemetry.io/otel/sdk/metric"
36+
)
37+
38+
var (
39+
addr = flag.String("addr", ":50051", "the server address to connect to")
40+
prometheusEndpoint = flag.String("prometheus_endpoint", ":9464", "the Prometheus exporter endpoint")
41+
)
42+
43+
type echoServer struct {
44+
pb.UnimplementedEchoServer
45+
addr string
46+
}
47+
48+
func (s *echoServer) UnaryEcho(ctx context.Context, req *pb.EchoRequest) (*pb.EchoResponse, error) {
49+
return &pb.EchoResponse{Message: fmt.Sprintf("%s (from %s)", req.Message, s.addr)}, nil
50+
}
51+
52+
func main() {
53+
exporter, err := prometheus.New()
54+
if err != nil {
55+
log.Fatalf("Failed to start prometheus exporter: %v", err)
56+
}
57+
provider := metric.NewMeterProvider(metric.WithReader(exporter))
58+
go http.ListenAndServe(*prometheusEndpoint, promhttp.Handler())
59+
60+
so := opentelemetry.ServerOption(opentelemetry.Options{MetricsOptions: opentelemetry.MetricsOptions{MeterProvider: provider}})
61+
62+
lis, err := net.Listen("tcp", *addr)
63+
if err != nil {
64+
log.Fatalf("Failed to listen: %v", err)
65+
}
66+
s := grpc.NewServer(so)
67+
pb.RegisterEchoServer(s, &echoServer{addr: *addr})
68+
69+
log.Printf("Serving on %s\n", *addr)
70+
71+
if err := s.Serve(lis); err != nil {
72+
log.Fatalf("Failed to serve: %v", err)
73+
}
74+
}

0 commit comments

Comments
 (0)