Skip to content

Commit

Permalink
otelttrpc: lift over example from otelgrpc.
Browse files Browse the repository at this point in the history
Lift over the sample client/server code from otelgrpc,
gutting out any bits we don't support yet (streaming),
and adapting it for (otel)ttrpc.

Signed-off-by: Krisztian Litkey <[email protected]>
  • Loading branch information
klihub committed Jan 13, 2024
1 parent 043c02a commit 7ce93bb
Show file tree
Hide file tree
Showing 10 changed files with 2,271 additions and 0 deletions.
86 changes: 86 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Copyright The containerd Authors.

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

GO_CMD := go
GO_BUILD := $(GO_CMD) build

PROTO_SOURCES := $(shell find . -name '*.proto' | grep -v /vendor/)
PROTO_GOFILES := $(patsubst %.proto,%.pb.go,$(PROTO_SOURCES)) \
$(patsubst %.proto,%_ttrpc.pb.go,$(PROTO_SOURCES))
PROTO_INCLUDE := -I$(PWD):/usr/local/include:/usr/include
PROTO_OPTIONS := --proto_path=. $(PROTO_INCLUDE) \
--go_opt=paths=source_relative --go_out=. \
--go-ttrpc_opt=paths=source_relative --go-ttrpc_out=.
PROTO_COMPILE := PATH=$(PATH):$(shell go env GOPATH)/bin; protoc $(PROTO_OPTIONS)

INSTALL_PROTOC := https://github.com/containerd/containerd/blob/main/script/setup/install-protobuf
PROTOC_DEPS := google.golang.org/protobuf/cmd/[email protected]
TTRPC_PLUGIN := github.com/containerd/ttrpc/cmd/protoc-gen-go-ttrpc@74421d10189e8c118870d294c9f7f62db2d33ec1
WGET := wget

BINARIES := example-client example-server

ifneq ($(V),1)
Q := @
endif


#
# top-level targets
#

all: build ## build example

build: $(BINARIES)

clean:
$(Q)rm -f $(BINARIES)

protos: $(PROTO_GOFILES) ## generate example API ttrpc bindings

#
# binary compilation targets
#

example-client: $(wildcard api/*.go config/*.go client/*.go) ## build example client
$(Q)echo "Building $@..."; \
$(GO_BUILD) -o $@ ./client

example-server: $(wildcard api/*.go config/*.go server/*.go) ## build example-server
$(Q)echo "Building $@..."; \
$(GO_BUILD) -o $@ ./server

#
# proto generation targets
#

%.pb.go: %.proto
$(Q)echo "Generating $@..."; \
$(PROTO_COMPILE) $<

#
# targets for installing dependencies
#

install-protoc install-protobuf: ## install protobuf compiler
$(Q)$(WGET) $(INSTALL_PROTOC) && chmod a+x install-protobuf && ./install-protobuf

install-protoc-dependencies: ## install protobuf compiler dependencies
$(Q)$(GO_INSTALL) -mod=mod $(PROTOC_DEPS)

install-ttrpc-plugin: ## install protobuf compiler ttrcp plugin
$(Q)$(GO_INSTALL) -mod=mod $(TTRPC_PLUGIN)

help: ## this help
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort
33 changes: 33 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# ttRPC Tracing Example

Traces unary client and server calls via interceptors.

### Compile Example Client and Server

```sh
make
```

### Run server

```sh
./example-server
```

### Run client

```sh
./example-client
```

### Generate Protobuf Go and ttRPC bindings

If you modify the example protobuf definitions (`api/hello-service.proto`),
you need to regenerate the corresponding golang/ttRPC bindings.

```sh
# Install protoc and its dependencies if you don't have them yet.
make install-protoc install-protoc-dependencies install-ttrpc-plugin
# Regenerate golang/ttRPC bindings, recompile client and server.
make
```
227 changes: 227 additions & 0 deletions example/api/hello-service.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions example/api/hello-service.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto3";
package api;

option go_package = "github.com/containerd/otelttrpc/example/api;api";

service HelloService {
rpc SayHello (HelloRequest) returns (HelloResponse);
}

message HelloRequest {
string greeting = 1;
}

message HelloResponse {
string reply = 1;
}
Loading

0 comments on commit 7ce93bb

Please sign in to comment.