-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(issue-35): add server side handler for adding otel tracing * fix(issue-35): missed the nil scenario * example(issue-35): created simple grpc example * example(issue-35): make simple grpc port more predictable * fix(issue-35): forgot to update goreleaser image names * example(issue-35): register reflection service so grpc example works with insomnia etc.
- Loading branch information
Showing
11 changed files
with
478 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Copyright (c) 2023 Z5Labs and Contributors | ||
# | ||
# This software is released under the MIT License. | ||
# https://opensource.org/licenses/MIT | ||
|
||
FROM scratch | ||
EXPOSE 9080 | ||
COPY simple_grpc / | ||
ENTRYPOINT ["/simple_grpc"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Copyright (c) 2023 Z5Labs and Contributors | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
|
||
package main | ||
|
||
import ( | ||
"context" | ||
"log/slog" | ||
"os" | ||
|
||
"github.com/z5labs/bedrock" | ||
"github.com/z5labs/bedrock/example/simple_grpc/simple_grpc_pb" | ||
brgrpc "github.com/z5labs/bedrock/grpc" | ||
"github.com/z5labs/bedrock/pkg/health" | ||
"github.com/z5labs/bedrock/pkg/otelconfig" | ||
|
||
"go.opentelemetry.io/otel" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/reflection" | ||
) | ||
|
||
type simpleService struct { | ||
simple_grpc_pb.UnimplementedSimpleServer | ||
} | ||
|
||
func (*simpleService) Echo(ctx context.Context, req *simple_grpc_pb.EchoRequest) (*simple_grpc_pb.EchoResponse, error) { | ||
_, span := otel.Tracer("main").Start(ctx, "simpleService.Echo") | ||
defer span.End() | ||
resp := &simple_grpc_pb.EchoResponse{ | ||
Message: req.Message, | ||
} | ||
return resp, nil | ||
} | ||
|
||
func registerSimpleService(s *grpc.Server) { | ||
simple_grpc_pb.RegisterSimpleServer(s, &simpleService{}) | ||
} | ||
|
||
func initRuntime(bc bedrock.BuildContext) (bedrock.Runtime, error) { | ||
logHandler := slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{AddSource: true}) | ||
|
||
rt := brgrpc.NewRuntime( | ||
brgrpc.ListenOnPort(9080), | ||
brgrpc.LogHandler(logHandler), | ||
brgrpc.Service( | ||
registerSimpleService, | ||
brgrpc.ServiceName("simple"), | ||
brgrpc.Readiness(&health.Readiness{}), | ||
), | ||
// register reflection service so you can test this example | ||
// via Insomnia, Postman and any other API testing tool that | ||
// understands gRPC reflection. | ||
brgrpc.Service(func(s *grpc.Server) { | ||
reflection.Register(s) | ||
}), | ||
) | ||
return rt, nil | ||
} | ||
|
||
func main() { | ||
bedrock.New( | ||
bedrock.InitTracerProvider(func(bc bedrock.BuildContext) (otelconfig.Initializer, error) { | ||
return otelconfig.Local( | ||
otelconfig.ServiceName("simple_grpc"), | ||
), nil | ||
}), | ||
bedrock.WithRuntimeBuilderFunc(initRuntime), | ||
).Run() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright (c) 2023 Z5Labs and Contributors | ||
// | ||
// This software is released under the MIT License. | ||
// https://opensource.org/licenses/MIT | ||
|
||
syntax = "proto3"; | ||
|
||
package simple_grpc_pb; | ||
|
||
option go_package = "github.com/z5labs/bedrock/example/simple_grpc/simple_grpc_pb"; | ||
|
||
service Simple { | ||
rpc Echo (EchoRequest) returns (EchoResponse); | ||
} | ||
|
||
message EchoRequest { | ||
string message = 1; | ||
} | ||
|
||
message EchoResponse { | ||
string message = 1; | ||
} |
Oops, something went wrong.