Skip to content

Commit

Permalink
otlp v2
Browse files Browse the repository at this point in the history
  • Loading branch information
rez1dent3 committed Dec 18, 2023
1 parent 07b6bad commit 659d695
Show file tree
Hide file tree
Showing 17 changed files with 219 additions and 63 deletions.
11 changes: 11 additions & 0 deletions deployments/docker-compose/docker-compose.infra.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
version: '3.8'

services:
jaeger:
image: jaegertracing/all-in-one:1.52
ports:
- "16686:16686"
- "4317:4317" # grpc
environment:
- COLLECTOR_OTLP_ENABLED=true
- LOG_LEVEL=debug
11 changes: 11 additions & 0 deletions deployments/docker-compose/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,32 @@ services:
simple:
image: bavix/gripmock:latest
entrypoint: example/simple/entrypoint.sh
environment: &env
OTLP_TRACE_GRPC_HOST: 192.168.1.186
OTLP_TRACE_GRPC_PORT: 4317
OTLP_TRACE_TLS: 0
OTLP_SAMPLE_RATIO: 1
stream:
image: bavix/gripmock:latest
entrypoint: example/stream/entrypoint.sh
environment: *env
well_known_types:
image: bavix/gripmock:latest
entrypoint: example/well_known_types/entrypoint.sh
environment: *env
multi-package:
image: bavix/gripmock:latest
entrypoint: example/multi-package/entrypoint.sh
environment: *env
multi-files:
image: bavix/gripmock:latest
entrypoint: example/multi-files/entrypoint.sh
environment: *env
one-of:
image: bavix/gripmock:latest
entrypoint: example/one-of/entrypoint.sh
environment: *env
stub-subfolders:
image: bavix/gripmock:latest
entrypoint: example/stub-subfolders/entrypoint.sh
environment: *env
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/pkg/errors v0.9.1
github.com/rs/zerolog v1.31.0
github.com/stretchr/testify v1.8.4
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.46.1
go.opentelemetry.io/otel v1.21.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.21.0
go.opentelemetry.io/otel/sdk v1.21.0
Expand All @@ -29,6 +29,7 @@ require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.16.0 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand All @@ -38,6 +39,8 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0 // indirect
go.opentelemetry.io/otel/metric v1.21.0 // indirect
go.opentelemetry.io/otel/trace v1.21.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ github.com/envoyproxy/protoc-gen-validate v1.0.2 h1:QkIBuU5k+x7/QXPvPPnWXWlCdaBF
github.com/envoyproxy/protoc-gen-validate v1.0.2/go.mod h1:GpiZQP3dDbg4JouG/NNS7QWXpgx6x8QiMKdmN72jogE=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
Expand Down Expand Up @@ -96,8 +98,12 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.46.1 h1:Ifzy1lucGMQJh6wPRxusde8bWaDhYjSNOqDyn6Hb4TM=
go.opentelemetry.io/contrib/instrumentation/github.com/gorilla/mux/otelmux v0.46.1/go.mod h1:YfFNem80G9UZ/mL5zd5GGXZSy95eXK+RhzIWBkLjLSc=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 h1:SpGay3w+nEwMpfVnbqOLH5gY52/foP8RE8UzTZ1pdSE=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1/go.mod h1:4UoMYEZOC0yN/sPGH76KPkkU7zgiEWYWL9vwmbnTJPE=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 h1:aFJWCqJMNjENlcleuuOkGAPH82y0yULBScfXcIEdS24=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1/go.mod h1:sEGXWArGqc3tVa+ekntsN65DmVbVeW+7lTKTjZF3/Fo=
go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc=
go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.21.0 h1:cl5P5/GIfFh4t6xyruOgJP5QiA1pw4fYYdv6nc6CBWw=
Expand Down
67 changes: 49 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"github.com/bavix/gripmock/internal/config"
"github.com/bavix/gripmock/internal/pkg/patcher"
"github.com/bavix/gripmock/pkg/trace"
"github.com/bavix/gripmock/pkg/utils"
_ "github.com/bavix/gripmock/protogen"
"github.com/bavix/gripmock/stub"
Expand All @@ -48,6 +49,10 @@ func main() {

ctx = logger.WithContext(ctx)

if err := trace.InitTracer(ctx, "gripmock", conf.OTLPTrace); err != nil {
zerolog.Ctx(ctx).Err(err).Msg("connect to tracer")
}

// deprecated. will be removed in 3.x
grpcPort := flag.String("grpc-port", conf.GRPC.Port, "Deprecated: use ENV GRPC_PORT. Port of gRPC tcp server") //nolint:lll
grpcBindAddr := flag.String("grpc-listen", conf.GRPC.Host, "Deprecated: use ENV GRPC_HOST. Address the gRPC server will bind to. Default to localhost, set to 0.0.0.0 to use from another machine") //nolint:lll
Expand Down Expand Up @@ -108,14 +113,18 @@ func main() {

// generate pb.go and grpc server based on proto
generateProtoc(ctx, protocParam{
protoPath: protoPaths,
adminHost: *adminBindAddr,
adminPort: *adminPort,
grpcNet: conf.GRPC.Network,
grpcAddress: *grpcBindAddr,
grpcPort: *grpcPort,
output: output,
imports: importDirs,
protoPath: protoPaths,
adminHost: *adminBindAddr,
adminPort: *adminPort,
grpcNet: conf.GRPC.Network,
grpcAddress: *grpcBindAddr,
grpcPort: *grpcPort,
otlpHost: conf.OTLPTrace.Host,
otlpPort: conf.OTLPTrace.Port,
otlpTLS: conf.OTLPTrace.TLS,
otlpSampleRatio: conf.OTLPTrace.SampleRatio,
output: output,
imports: importDirs,
})

// and run
Expand Down Expand Up @@ -160,14 +169,18 @@ func main() {
}

type protocParam struct {
protoPath []string
adminHost string
adminPort string
grpcAddress string
grpcNet string
grpcPort string
output string
imports []string
protoPath []string
adminHost string
adminPort string
grpcAddress string
grpcNet string
grpcPort string
output string
otlpHost string
otlpPort string
otlpTLS bool
otlpSampleRatio float64
imports []string
}

func getProtodirs(_ context.Context, protoPath string, imports []string) []string {
Expand Down Expand Up @@ -218,12 +231,22 @@ func generateProtoc(ctx context.Context, param protocParam) {
// the latest go-grpc plugin will generate subfolders under $GOPATH/src based on go_package option
pbOutput := os.Getenv("GOPATH") + "/src"

gmOut := []string{
fmt.Sprintf(
"admin-host=%s,admin-port=%s",
param.adminHost, param.adminPort),
fmt.Sprintf("grpc-network=%s,grpc-address=%s,grpc-port=%s",
param.grpcNet, param.grpcAddress, param.grpcPort),
fmt.Sprintf("otlp-host=%s,otlp-port=%s,otlp-tls=%d,otlp-ratio=%f",
param.otlpHost, param.otlpPort, bool2int(param.otlpTLS), param.otlpSampleRatio),
}

args = append(args, param.protoPath...)
args = append(args, "--go_out="+pbOutput)
args = append(args, "--go-grpc_out="+pbOutput)
args = append(args, fmt.Sprintf(
"--gripmock_out=admin-host=%s,admin-port=%s,grpc-network=%s,grpc-address=%s,grpc-port=%s:%s",
param.adminHost, param.adminPort, param.grpcNet, param.grpcAddress, param.grpcPort, param.output))
"--gripmock_out=%s:%s",
strings.Join(gmOut, ","), param.output))
protoc := exec.Command("protoc", args...)
protoc.Stdout = os.Stdout
protoc.Stderr = os.Stderr
Expand All @@ -233,6 +256,14 @@ func generateProtoc(ctx context.Context, param protocParam) {
}
}

func bool2int(b bool) int {
if b {
return 1
}

return 0
}

// append gopackage in proto files if doesn't have any.
func fixGoPackage(ctx context.Context, protoPaths []string) []string {
results := make([]string, 0, len(protoPaths))
Expand Down
12 changes: 12 additions & 0 deletions pkg/trace/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package trace

type OTLPTrace struct {
Host string `envconfig:"OTLP_TRACE_GRPC_HOST" default:"127.0.0.1"`
Port string `envconfig:"OTLP_TRACE_GRPC_PORT" default:"4317"`
TLS bool `envconfig:"OTLP_TRACE_TLS" default:"false"`
SampleRatio float64 `envconfig:"OTLP_SAMPLE_RATIO"`
}

func (o *OTLPTrace) UseTrace() bool {
return o.Host != "" && o.Port != "" && o.SampleRatio > 0
}
10 changes: 6 additions & 4 deletions pkg/trace/otel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package trace

import (
"context"
"net"

"github.com/pkg/errors"
"go.opentelemetry.io/otel"
Expand All @@ -12,14 +13,15 @@ import (
semconv "go.opentelemetry.io/otel/semconv/v1.21.0"
)

const appName = "gripmock"

func InitTracer(ctx context.Context, otlpTrace OTLPTrace) error {
func InitTracer(ctx context.Context, appName string, otlpTrace OTLPTrace) error {
if !otlpTrace.UseTrace() {
return nil
}

options := []otlptracegrpc.Option{otlptracegrpc.WithEndpoint(otlpTrace.Endpoint)}
options := []otlptracegrpc.Option{
otlptracegrpc.WithEndpoint(
net.JoinHostPort(otlpTrace.Host, otlpTrace.Port)),
}
if !otlpTrace.TLS {
options = append(options, otlptracegrpc.WithInsecure())
}
Expand Down
11 changes: 0 additions & 11 deletions pkg/trace/otlp.go

This file was deleted.

71 changes: 45 additions & 26 deletions protoc-gen-gripmock/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"log"
"net"
"os"
"strconv"
"strings"
"text/template"

Expand Down Expand Up @@ -52,13 +53,20 @@ func main() {
params[split[0]] = split[1]
}

otlpTLS, _ := strconv.ParseBool(params["otlp-tls"])
otlpRatioFloat, _ := strconv.ParseFloat(params["otlp-ratio"], 64)

buf := new(bytes.Buffer)
err = generateServer(protos, &Options{
writer: buf,
adminHost: params["admin-host"],
adminPort: params["admin-port"],
grpcNet: params["grpc-network"],
grpcAddr: net.JoinHostPort(params["grpc-address"], params["grpc-port"]),
writer: buf,
adminHost: params["admin-host"],
adminPort: params["admin-port"],
grpcNet: params["grpc-network"],
grpcAddr: net.JoinHostPort(params["grpc-address"], params["grpc-port"]),
otlpTLS: otlpTLS,
otlpHost: params["otlp-host"],
otlpPort: params["otlp-port"],
otlpSampleRatio: otlpRatioFloat,
})

if err != nil {
Expand All @@ -79,13 +87,17 @@ func main() {
}

type generatorParam struct {
Services []Service
Dependencies map[string]string
GrpcNet string
GrpcAddr string
AdminHost string
AdminPort string
PbPath string
Services []Service
Dependencies map[string]string
GrpcNet string
GrpcAddr string
AdminHost string
AdminPort string
OtlpHost string
OtlpPort string
OtlpTLS bool
OtlpSampleRatio float64
PbPath string
}

type Service struct {
Expand Down Expand Up @@ -113,13 +125,16 @@ const (
)

type Options struct {
writer io.Writer
grpcNet string
grpcAddr string
adminHost string
adminPort string
pbPath string
format bool
writer io.Writer
grpcNet string
grpcAddr string
adminHost string
adminPort string
otlpHost string
otlpPort string
otlpTLS bool
otlpSampleRatio float64
pbPath string
}

var ServerTemplate string
Expand All @@ -141,13 +156,17 @@ func generateServer(protos []*descriptorpb.FileDescriptorProto, opt *Options) er
deps := resolveDependencies(protos)

param := generatorParam{
Services: services,
Dependencies: deps,
GrpcNet: opt.grpcNet,
GrpcAddr: opt.grpcAddr,
AdminHost: opt.adminHost,
AdminPort: opt.adminPort,
PbPath: opt.pbPath,
Services: services,
Dependencies: deps,
GrpcNet: opt.grpcNet,
GrpcAddr: opt.grpcAddr,
AdminHost: opt.adminHost,
AdminPort: opt.adminPort,
OtlpHost: opt.otlpHost,
OtlpPort: opt.otlpPort,
OtlpTLS: opt.otlpTLS,
OtlpSampleRatio: opt.otlpSampleRatio,
PbPath: opt.pbPath,
}

if opt == nil {
Expand Down
2 changes: 2 additions & 0 deletions protoc-gen-gripmock/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ go 1.21

require (
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1
golang.org/x/text v0.14.0
golang.org/x/tools v0.16.1
google.golang.org/protobuf v1.31.0
)

require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand Down
4 changes: 4 additions & 0 deletions protoc-gen-gripmock/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/envoyproxy/protoc-gen-validate v1.0.2 h1:QkIBuU5k+x7/QXPvPPnWXWlCdaBFApVqftFV6k087DA=
github.com/envoyproxy/protoc-gen-validate v1.0.2/go.mod h1:GpiZQP3dDbg4JouG/NNS7QWXpgx6x8QiMKdmN72jogE=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.3.0 h1:2y3SDp0ZXuc6/cjLSZ+Q3ir+QB9T/iG5yYRXqsagWSY=
github.com/go-logr/logr v1.3.0/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
Expand All @@ -25,6 +27,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1 h1:SpGay3w+nEwMpfVnbqOLH5gY52/foP8RE8UzTZ1pdSE=
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.46.1/go.mod h1:4UoMYEZOC0yN/sPGH76KPkkU7zgiEWYWL9vwmbnTJPE=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1 h1:aFJWCqJMNjENlcleuuOkGAPH82y0yULBScfXcIEdS24=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.46.1/go.mod h1:sEGXWArGqc3tVa+ekntsN65DmVbVeW+7lTKTjZF3/Fo=
go.opentelemetry.io/otel v1.21.0 h1:hzLeKBZEL7Okw2mGzZ0cc4k/A7Fta0uoPgaJCr8fsFc=
go.opentelemetry.io/otel v1.21.0/go.mod h1:QZzNPQPm1zLX4gZK4cMi+71eaorMSGT3A4znnUvNNEo=
go.opentelemetry.io/otel/metric v1.21.0 h1:tlYWfeo+Bocx5kLEloTjbcDwBuELRrIFxwdQ36PlJu4=
Expand Down
Loading

0 comments on commit 659d695

Please sign in to comment.