-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
97 lines (89 loc) · 2.91 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// © 2023 SolarWinds Worldwide, LLC. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"github.com/solarwinds/apm-go/swo"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/attribute"
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"log"
"net"
"time"
)
func main() {
// Initialize the SolarWinds APM library
cb, err := swo.Start(
// Optionally add service-level resource attributes
semconv.ServiceName("my-service"),
semconv.ServiceVersion("v0.0.1"),
attribute.String("environment", "testing"),
)
if err != nil {
// Handle error
}
// This function returned from `Start()` will tell the apm library to
// shut down, often deferred until the end of `main()`.
defer cb()
lis, err := net.Listen("tcp", "localhost:9090")
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
grpcServer := grpc.NewServer(
// We provide `otelgrpc` interceptors to add instrumentation to each call
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor()),
// Even though we have no streaming calls in this service, adding this
// instrumentation adds zero overhead. It's a good idea to _always_ have
// both Unary and Stream interceptors for both server and client.
grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor()),
)
RegisterPingerServer(grpcServer, newPingServer())
// Create a loop that calls the pinger via an instrumented grpc client
go func() {
// Here we create an instrumented client
conn, err := grpc.Dial("localhost:9090",
grpc.WithTransportCredentials(insecure.NewCredentials()),
// otelgrpc instrumentation for the client
grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
grpc.WithStreamInterceptor(otelgrpc.StreamClientInterceptor()),
)
if err != nil {
panic(err.Error())
}
defer conn.Close()
client := NewPingerClient(conn)
for {
time.Sleep(1 * time.Second)
log.Println("Ping!")
_, err := client.Ping(context.Background(), &PingRequest{})
if err != nil {
panic(err.Error())
}
}
}()
grpcServer.Serve(lis)
}
type pinger struct {
UnimplementedPingerServer
}
func (p pinger) Ping(context.Context, *PingRequest) (*PingResponse, error) {
log.Println("Pong!")
return &PingResponse{}, nil
}
var _ PingerServer = pinger{}
func newPingServer() PingerServer {
return pinger{}
}