Skip to content

Commit

Permalink
story(grpc): configure otel (#38)
Browse files Browse the repository at this point in the history
* 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
Zaba505 authored Dec 17, 2023
1 parent 54c4682 commit b6307a8
Show file tree
Hide file tree
Showing 11 changed files with 478 additions and 10 deletions.
44 changes: 38 additions & 6 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ builds:
main: example/otlp/main.go
binary: otlp

- id: simple_grpc
env:
- CGO_ENABLED=0
goos:
- linux
goarch:
- amd64
- arm64
goamd64:
- v3
main: example/simple_grpc/main.go
binary: simple_grpc

- id: simple_http
env:
- CGO_ENABLED=0
Expand Down Expand Up @@ -53,8 +66,8 @@ dockers:
ids:
- otlp
image_templates:
- "ghcr.io/z5labs/app/example/otlp:latest"
- "ghcr.io/z5labs/app/example/otlp:{{ .Tag }}"
- "ghcr.io/z5labs/bedrock/example/otlp:latest"
- "ghcr.io/z5labs/bedrock/example/otlp:{{ .Tag }}"
dockerfile: example/otlp/Containerfile
use: docker
build_flag_templates:
Expand All @@ -65,15 +78,34 @@ dockers:
- "--label=org.opencontainers.image.version={{.Version}}"
- "--platform=linux/amd64"

- id: simple_grpc
goos: linux
goarch: amd64
goamd64: v3
ids:
- simple_grpc
image_templates:
- "ghcr.io/z5labs/bedrock/example/simple_grpc:latest"
- "ghcr.io/z5labs/bedrock/example/simple_grpc:{{ .Tag }}"
dockerfile: example/simple_grpc/Containerfile
use: docker
build_flag_templates:
- "--pull"
- "--label=org.opencontainers.image.created={{.Date}}"
- "--label=org.opencontainers.image.title={{.ProjectName}}"
- "--label=org.opencontainers.image.revision={{.FullCommit}}"
- "--label=org.opencontainers.image.version={{.Version}}"
- "--platform=linux/amd64"

- id: simple_http
goos: linux
goarch: amd64
goamd64: v3
ids:
- simple_http
image_templates:
- "ghcr.io/z5labs/app/example/simple_http:latest"
- "ghcr.io/z5labs/app/example/simple_http:{{ .Tag }}"
- "ghcr.io/z5labs/bedrock/example/simple_http:latest"
- "ghcr.io/z5labs/bedrock/example/simple_http:{{ .Tag }}"
dockerfile: example/simple_http/Containerfile
use: docker
build_flag_templates:
Expand All @@ -91,8 +123,8 @@ dockers:
ids:
- simple_queue
image_templates:
- "ghcr.io/z5labs/app/example/simple_queue:latest"
- "ghcr.io/z5labs/app/example/simple_queue:{{ .Tag }}"
- "ghcr.io/z5labs/bedrock/example/simple_queue:latest"
- "ghcr.io/z5labs/bedrock/example/simple_queue:{{ .Tag }}"
dockerfile: example/simple_queue/Containerfile
use: docker
build_flag_templates:
Expand Down
9 changes: 9 additions & 0 deletions example/simple_grpc/Containerfile
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"]
71 changes: 71 additions & 0 deletions example/simple_grpc/main.go
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()
}
22 changes: 22 additions & 0 deletions example/simple_grpc/simple_grpc.proto
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;
}
Loading

0 comments on commit b6307a8

Please sign in to comment.