File: examples_test.go

package info (click to toggle)
golang-gitlab-gitlab-org-labkit 1.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,092 kB
  • sloc: sh: 210; javascript: 49; makefile: 4
file content (43 lines) | stat: -rw-r--r-- 1,053 bytes parent folder | download | duplicates (4)
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
package grpccorrelation_test

import (
	"log"
	"net"

	grpccorrelation "gitlab.com/gitlab-org/labkit/correlation/grpc"
	"google.golang.org/grpc"
)

func Example_client() {
	// Add the interceptor to the grpc dialer
	dialer, err := grpc.Dial("https://gitaly-server.internal:9095",
		grpc.WithStreamInterceptor(grpccorrelation.StreamClientCorrelationInterceptor(
			grpccorrelation.WithClientName("my-client"),
		)),
		grpc.WithUnaryInterceptor(grpccorrelation.UnaryClientCorrelationInterceptor(
			grpccorrelation.WithClientName("my-client"),
		)),
	)

	if err != nil {
		log.Fatalf("unable to dial: %v", err)
	}

	// Use the client connection with a protobuf service here...

	defer dialer.Close()
}

func Example_server() {
	server := grpc.NewServer(
		grpc.StreamInterceptor(grpccorrelation.StreamServerCorrelationInterceptor()),
		grpc.UnaryInterceptor(grpccorrelation.UnaryServerCorrelationInterceptor()),
	)

	listener, err := net.Listen("unix", "/tmp/grpc")
	if err != nil {
		log.Fatalf("unable to listen: %v", err)
	}

	server.Serve(listener)
}