/
server.go
53 lines (46 loc) · 1.02 KB
/
server.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
package server
import (
"context"
"github.com/soroushj/go-grpc-otel-example/notes"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
otelcodes "go.opentelemetry.io/otel/codes"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
var (
trc = otel.Tracer("go-grpc-otel-example/server")
)
type Server struct {
notes.UnimplementedNotesServer
db map[string]string
}
func New() *Server {
return &Server{
db: map[string]string{
"1": "buy coffee",
"2": "buy bread",
"3": "buy cheese",
},
}
}
func (s *Server) GetNote(ctx context.Context, req *notes.GetNoteRequest) (_ *notes.GetNoteResponse, _err error) {
ctx, span := trc.Start(ctx, "GetNote")
span.SetAttributes(attribute.Stringer("req", req))
defer func() {
if _err != nil {
span.SetStatus(otelcodes.Error, _err.Error())
}
span.End()
}()
t := s.db[req.Id]
if t == "" {
return nil, status.Error(codes.NotFound, "note not found")
}
return ¬es.GetNoteResponse{
Note: ¬es.Note{
Id: req.Id,
Text: t,
},
}, nil
}