generated from rmcguire/go-server-with-otel
Initial commit
This commit is contained in:
86
pkg/demogrpc/demo.go
Normal file
86
pkg/demogrpc/demo.go
Normal file
@ -0,0 +1,86 @@
|
||||
package demogrpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
"github.com/rs/zerolog"
|
||||
"go.opentelemetry.io/otel/attribute"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
|
||||
grpccodes "google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/otel"
|
||||
|
||||
pb "gitea.libretechconsulting.com/rmcguire/go-server-with-otel/api/demo/app/v1alpha1"
|
||||
"gitea.libretechconsulting.com/rmcguire/go-server-with-otel/pkg/config"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultFactType = config.TypeRandom
|
||||
factsBaseURI = "https://uselessfacts.jsph.pl/api/v2/facts"
|
||||
)
|
||||
|
||||
type DemoGRPCServer struct {
|
||||
tracer trace.Tracer
|
||||
ctx context.Context
|
||||
cfg *config.DemoConfig
|
||||
client *resty.Client
|
||||
pb.UnimplementedDemoAppServiceServer
|
||||
}
|
||||
|
||||
func NewDemoGRPCServer(ctx context.Context, cfg *config.DemoConfig) *DemoGRPCServer {
|
||||
if cfg.Opts == nil {
|
||||
cfg.Opts = &config.DemoOpts{}
|
||||
}
|
||||
|
||||
if cfg.Opts.FactType == "" {
|
||||
cfg.Opts.FactType = config.TypeRandom
|
||||
}
|
||||
|
||||
return &DemoGRPCServer{
|
||||
ctx: ctx,
|
||||
cfg: cfg,
|
||||
tracer: otel.GetTracer(ctx, "demoGRPCServer"),
|
||||
client: resty.New().SetBaseURL(factsBaseURI),
|
||||
}
|
||||
}
|
||||
|
||||
func (d *DemoGRPCServer) GetDemo(ctx context.Context, req *pb.GetDemoRequest) (
|
||||
*pb.GetDemoResponse, error,
|
||||
) {
|
||||
ctx, span := d.tracer.Start(ctx, "getDemo", trace.WithAttributes(
|
||||
attribute.String("language", req.GetLanguage()),
|
||||
))
|
||||
defer span.End()
|
||||
|
||||
r := d.client.R().SetContext(ctx)
|
||||
|
||||
if req.GetLanguage() != "" {
|
||||
r = r.SetQueryParam("language", req.GetLanguage())
|
||||
}
|
||||
|
||||
fact := new(RandomFact)
|
||||
r.SetResult(fact)
|
||||
|
||||
resp, err := r.Get(fmt.Sprintf("/%s", d.cfg.Opts.FactType))
|
||||
if err != nil {
|
||||
span.SetStatus(codes.Error, err.Error())
|
||||
span.RecordError(err)
|
||||
|
||||
zerolog.Ctx(d.ctx).Err(err).Send()
|
||||
|
||||
return nil, status.Errorf(grpccodes.Unknown, "%s: %s",
|
||||
err.Error(), string(resp.Body()))
|
||||
}
|
||||
|
||||
span.SetAttributes(attribute.String("fact", fact.Text))
|
||||
span.SetStatus(codes.Ok, "")
|
||||
|
||||
zerolog.Ctx(d.ctx).Debug().Str("fact", fact.Text).Msg("retrieved fact")
|
||||
|
||||
return fact.FactToProto(), nil
|
||||
}
|
25
pkg/demogrpc/fact.go
Normal file
25
pkg/demogrpc/fact.go
Normal file
@ -0,0 +1,25 @@
|
||||
package demogrpc
|
||||
|
||||
import (
|
||||
"google.golang.org/protobuf/types/known/timestamppb"
|
||||
|
||||
pb "gitea.libretechconsulting.com/rmcguire/go-server-with-otel/api/demo/app/v1alpha1"
|
||||
)
|
||||
|
||||
type RandomFact struct {
|
||||
ID string `json:"id,omitempty"`
|
||||
Text string `json:"text,omitempty"`
|
||||
Source string `json:"source,omitempty"`
|
||||
SourceURL string `json:"source_url,omitempty"`
|
||||
Language string `json:"language,omitempty"`
|
||||
Permalink string `json:"permalink,omitempty"`
|
||||
}
|
||||
|
||||
func (f *RandomFact) FactToProto() *pb.GetDemoResponse {
|
||||
return &pb.GetDemoResponse{
|
||||
Timestamp: timestamppb.Now(),
|
||||
Fact: f.Text,
|
||||
Source: f.SourceURL,
|
||||
Language: f.Language,
|
||||
}
|
||||
}
|
30
pkg/demogrpc/server.go
Normal file
30
pkg/demogrpc/server.go
Normal file
@ -0,0 +1,30 @@
|
||||
package demogrpc
|
||||
|
||||
import (
|
||||
"gitea.libretechconsulting.com/rmcguire/go-app/pkg/srv/grpc/opts"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
|
||||
demoAppPb "gitea.libretechconsulting.com/rmcguire/go-server-with-otel/api/demo/app/v1alpha1"
|
||||
)
|
||||
|
||||
func (ds *DemoGRPCServer) GetDialOpts() []grpc.DialOption {
|
||||
return []grpc.DialOption{
|
||||
// NOTE: Necessary for grpc-gateway to connect to grpc server
|
||||
// Update if grpc service has credentials, tls, etc..
|
||||
grpc.WithTransportCredentials(insecure.NewCredentials()),
|
||||
}
|
||||
}
|
||||
|
||||
func (ds *DemoGRPCServer) GetServices() []*opts.GRPCService {
|
||||
return []*opts.GRPCService{
|
||||
{
|
||||
Name: "Demo App",
|
||||
Type: &demoAppPb.DemoAppService_ServiceDesc,
|
||||
Service: ds,
|
||||
GwRegistrationFuncs: []opts.GwRegistrationFunc{
|
||||
demoAppPb.RegisterDemoAppServiceHandler,
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user