Skip to content

Commit ce398e7

Browse files
committed
Temporarily bring in deleted code for the Unary interceptors to unblock the upgrade.
1 parent 3a9bf9e commit ce398e7

File tree

12 files changed

+580
-21
lines changed

12 files changed

+580
-21
lines changed

examples/gin-server/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ func setupRouter() *gin.Engine {
3030
func main() {
3131
cfg := config.Load()
3232
cfg.ServiceName = config.String("gin-example-server")
33+
cfg.Reporting.Endpoint = config.String("localhost:5442")
34+
cfg.Reporting.TraceReporterType = config.TraceReporterType_OTLP
3335

3436
flusher := hypertrace.Init(cfg)
3537
defer flusher()

examples/http-client/main.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"bytes"
77
"encoding/json"
88
"fmt"
9-
"io/ioutil"
9+
"io"
1010
"log"
1111
"net/http"
1212

@@ -22,6 +22,8 @@ type message struct {
2222
func main() {
2323
cfg := config.Load()
2424
cfg.ServiceName = config.String("client")
25+
cfg.Reporting.Endpoint = config.String("localhost:5442")
26+
cfg.Reporting.TraceReporterType = config.TraceReporterType_OTLP
2527

2628
flusher := hypertrace.Init(cfg)
2729
defer flusher()
@@ -41,7 +43,7 @@ func main() {
4143
log.Fatalf("failed to perform the request: %v", err)
4244
}
4345

44-
resBody, err := ioutil.ReadAll(res.Body)
46+
resBody, err := io.ReadAll(res.Body)
4547
if err != nil {
4648
log.Fatalf("failed to read the response body: %v", err)
4749
}

examples/mux-server/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import (
1919
func main() {
2020
cfg := config.Load()
2121
cfg.ServiceName = config.String("http-mux-server")
22+
cfg.Reporting.Endpoint = config.String("localhost:5442")
23+
cfg.Reporting.TraceReporterType = config.TraceReporterType_OTLP
2224

2325
flusher := hypertrace.Init(cfg)
2426
defer flusher()

instrumentation/hypertrace/google.golang.org/hypergrpc/client.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@ package hypergrpc // import "github.com/hypertrace/goagent/instrumentation/hyper
22

33
import (
44
"github.com/hypertrace/goagent/instrumentation/opentelemetry"
5+
"github.com/hypertrace/goagent/instrumentation/opentelemetry/grpcunaryinterceptors"
56
sdkgrpc "github.com/hypertrace/goagent/sdk/instrumentation/google.golang.org/grpc"
6-
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
77
"google.golang.org/grpc"
88
)
99

1010
// UnaryClientInterceptor returns a grpc.UnaryClientInterceptor suitable
1111
// for use in a grpc.Dial call.
12+
// Interceptor format will be replaced with the stats.Handler since instrumentation has moved to the stats.Handler.
13+
// See: https://github.com/open-telemetry/opentelemetry-go-contrib/blob/v1.36.0/instrumentation/google.golang.org/grpc/otelgrpc/example_test.go
1214
func UnaryClientInterceptor() grpc.UnaryClientInterceptor {
1315
return sdkgrpc.WrapUnaryClientInterceptor(
14-
otelgrpc.UnaryClientInterceptor(),
16+
grpcunaryinterceptors.UnaryClientInterceptor(),
1517
opentelemetry.SpanFromContext,
1618
map[string]string{},
1719
)

instrumentation/hypertrace/google.golang.org/hypergrpc/server.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,23 @@ package hypergrpc // import "github.com/hypertrace/goagent/instrumentation/hyper
22

33
import (
44
"github.com/hypertrace/goagent/instrumentation/opentelemetry"
5+
"github.com/hypertrace/goagent/instrumentation/opentelemetry/grpcunaryinterceptors"
56
sdkgrpc "github.com/hypertrace/goagent/sdk/instrumentation/google.golang.org/grpc"
6-
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
77
"google.golang.org/grpc"
88
)
99

1010
// UnaryServerInterceptor returns a grpc.UnaryServerInterceptor suitable
1111
// for use in a grpc.NewServer call.
12+
// Interceptor format will be replaced with the stats.Handler since instrumentation has moved to the stats.Handler.
13+
// See: https://github.com/open-telemetry/opentelemetry-go-contrib/blob/v1.36.0/instrumentation/google.golang.org/grpc/otelgrpc/example_test.go
1214
func UnaryServerInterceptor(opts ...Option) grpc.UnaryServerInterceptor {
1315
o := &options{}
1416
for _, opt := range opts {
1517
opt(o)
1618
}
1719

1820
return sdkgrpc.WrapUnaryServerInterceptor(
19-
otelgrpc.UnaryServerInterceptor(),
21+
grpcunaryinterceptors.UnaryServerInterceptor(),
2022
opentelemetry.SpanFromContext,
2123
o.toSDKOptions(),
2224
map[string]string{},

instrumentation/opentelemetry/google.golang.org/hypergrpc/client_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ import (
55
"testing"
66

77
"github.com/hypertrace/goagent/instrumentation/opentelemetry/google.golang.org/hypergrpc/internal/helloworld"
8+
"github.com/hypertrace/goagent/instrumentation/opentelemetry/grpcunaryinterceptors"
89
"github.com/hypertrace/goagent/instrumentation/opentelemetry/internal/tracetesting"
910
"github.com/stretchr/testify/assert"
10-
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
1111
otelcodes "go.opentelemetry.io/otel/codes"
1212
"google.golang.org/grpc"
1313
"google.golang.org/grpc/codes"
14+
"google.golang.org/grpc/credentials/insecure"
1415
"google.golang.org/grpc/metadata"
1516
"google.golang.org/grpc/status"
1617
)
@@ -34,10 +35,10 @@ func TestClientHelloWorldSuccess(t *testing.T) {
3435
ctx,
3536
"bufnet",
3637
grpc.WithContextDialer(dialer),
37-
grpc.WithInsecure(),
38+
grpc.WithTransportCredentials(insecure.NewCredentials()),
3839
grpc.WithUnaryInterceptor(
3940
WrapUnaryClientInterceptor(
40-
otelgrpc.UnaryClientInterceptor(),
41+
grpcunaryinterceptors.UnaryClientInterceptor(),
4142
),
4243
),
4344
)
@@ -108,10 +109,10 @@ func TestClientRegisterPersonFails(t *testing.T) {
108109
ctx,
109110
"bufnet",
110111
grpc.WithContextDialer(dialer),
111-
grpc.WithInsecure(),
112+
grpc.WithTransportCredentials(insecure.NewCredentials()),
112113
grpc.WithUnaryInterceptor(
113114
WrapUnaryClientInterceptor(
114-
otelgrpc.UnaryClientInterceptor(),
115+
grpcunaryinterceptors.UnaryClientInterceptor(),
115116
),
116117
),
117118
)
@@ -154,10 +155,10 @@ func BenchmarkClientRequestResponseBodyMarshaling(b *testing.B) {
154155
ctx,
155156
"bufnet",
156157
grpc.WithContextDialer(dialer),
157-
grpc.WithInsecure(),
158+
grpc.WithTransportCredentials(insecure.NewCredentials()),
158159
grpc.WithUnaryInterceptor(
159160
WrapUnaryClientInterceptor(
160-
otelgrpc.UnaryClientInterceptor(),
161+
grpcunaryinterceptors.UnaryClientInterceptor(),
161162
),
162163
),
163164
)
@@ -197,8 +198,8 @@ func BenchmarkClientRequestDefaultInterceptor(b *testing.B) {
197198
ctx,
198199
"bufnet",
199200
grpc.WithContextDialer(dialer),
200-
grpc.WithInsecure(),
201-
grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()),
201+
grpc.WithTransportCredentials(insecure.NewCredentials()),
202+
grpc.WithUnaryInterceptor(grpcunaryinterceptors.UnaryClientInterceptor()),
202203
)
203204
if err != nil {
204205
b.Fatalf("failed to dial bufnet: %v", err)

instrumentation/opentelemetry/google.golang.org/hypergrpc/server_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ import (
99
"testing"
1010

1111
"github.com/hypertrace/goagent/instrumentation/opentelemetry/google.golang.org/hypergrpc/internal/helloworld"
12+
"github.com/hypertrace/goagent/instrumentation/opentelemetry/grpcunaryinterceptors"
1213
"github.com/hypertrace/goagent/instrumentation/opentelemetry/internal/tracetesting"
1314
sdkgrpc "github.com/hypertrace/goagent/sdk/instrumentation/google.golang.org/grpc"
1415
"github.com/stretchr/testify/assert"
15-
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
1616
otelcodes "go.opentelemetry.io/otel/codes"
1717
"google.golang.org/grpc"
1818
"google.golang.org/grpc/codes"
@@ -83,7 +83,7 @@ func TestServerRegisterPersonSuccess(t *testing.T) {
8383

8484
s := grpc.NewServer(
8585
grpc.UnaryInterceptor(
86-
WrapUnaryServerInterceptor(otelgrpc.UnaryServerInterceptor(), &sdkgrpc.Options{}),
86+
WrapUnaryServerInterceptor(grpcunaryinterceptors.UnaryServerInterceptor(), &sdkgrpc.Options{}),
8787
),
8888
)
8989
defer s.Stop()
@@ -150,7 +150,7 @@ func TestServerRegisterPersonFails(t *testing.T) {
150150

151151
s := grpc.NewServer(
152152
grpc.UnaryInterceptor(
153-
WrapUnaryServerInterceptor(otelgrpc.UnaryServerInterceptor(), &sdkgrpc.Options{}),
153+
WrapUnaryServerInterceptor(grpcunaryinterceptors.UnaryServerInterceptor(), &sdkgrpc.Options{}),
154154
),
155155
)
156156
defer s.Stop()
@@ -196,7 +196,7 @@ func BenchmarkServerRequestResponseBodyMarshaling(b *testing.B) {
196196

197197
s := grpc.NewServer(
198198
grpc.UnaryInterceptor(
199-
WrapUnaryServerInterceptor(otelgrpc.UnaryServerInterceptor(), &sdkgrpc.Options{}),
199+
WrapUnaryServerInterceptor(grpcunaryinterceptors.UnaryServerInterceptor(), &sdkgrpc.Options{}),
200200
),
201201
)
202202
defer s.Stop()
@@ -237,7 +237,7 @@ func BenchmarkServerRequestDefaultInterceptor(b *testing.B) {
237237
tracetesting.InitTracer()
238238

239239
s := grpc.NewServer(
240-
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor()),
240+
grpc.UnaryInterceptor(grpcunaryinterceptors.UnaryServerInterceptor()),
241241
)
242242
defer s.Stop()
243243

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package grpcunaryinterceptors
2+
3+
import (
4+
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
5+
"go.opentelemetry.io/otel"
6+
"go.opentelemetry.io/otel/attribute"
7+
"go.opentelemetry.io/otel/metric"
8+
"go.opentelemetry.io/otel/metric/noop"
9+
"go.opentelemetry.io/otel/propagation"
10+
semconv "go.opentelemetry.io/otel/semconv/v1.17.0"
11+
"go.opentelemetry.io/otel/trace"
12+
)
13+
14+
// InterceptorFilter is a predicate used to determine whether a given request in
15+
// interceptor info should be instrumented. A InterceptorFilter must return true if
16+
// the request should be traced.
17+
//
18+
// Deprecated: Use stats handlers instead.
19+
// type InterceptorFilter func(*otelgrpc.InterceptorInfo) bool
20+
21+
// Filter is a predicate used to determine whether a given request in
22+
// should be instrumented by the attached RPC tag info.
23+
// A Filter must return true if the request should be instrumented.
24+
// type Filter func(*stats.RPCTagInfo) bool
25+
26+
// config is a group of options for this instrumentation.
27+
type config struct {
28+
Filter otelgrpc.Filter
29+
InterceptorFilter otelgrpc.InterceptorFilter
30+
Propagators propagation.TextMapPropagator
31+
TracerProvider trace.TracerProvider
32+
MeterProvider metric.MeterProvider
33+
SpanStartOptions []trace.SpanStartOption
34+
SpanAttributes []attribute.KeyValue
35+
MetricAttributes []attribute.KeyValue
36+
37+
ReceivedEvent bool
38+
SentEvent bool
39+
40+
tracer trace.Tracer
41+
meter metric.Meter
42+
43+
rpcDuration metric.Float64Histogram
44+
rpcInBytes metric.Int64Histogram
45+
rpcOutBytes metric.Int64Histogram
46+
rpcInMessages metric.Int64Histogram
47+
rpcOutMessages metric.Int64Histogram
48+
}
49+
50+
// Option applies an option value for a config.
51+
type Option interface {
52+
apply(*config)
53+
}
54+
55+
// newConfig returns a config configured with all the passed Options.
56+
func newConfig(opts []Option, role string) *config {
57+
c := &config{
58+
Propagators: otel.GetTextMapPropagator(),
59+
TracerProvider: otel.GetTracerProvider(),
60+
MeterProvider: otel.GetMeterProvider(),
61+
}
62+
for _, o := range opts {
63+
o.apply(c)
64+
}
65+
66+
c.tracer = c.TracerProvider.Tracer(
67+
otelgrpc.ScopeName,
68+
trace.WithInstrumentationVersion(otelgrpc.Version()),
69+
)
70+
71+
c.meter = c.MeterProvider.Meter(
72+
otelgrpc.ScopeName,
73+
metric.WithInstrumentationVersion(otelgrpc.Version()),
74+
metric.WithSchemaURL(semconv.SchemaURL),
75+
)
76+
77+
var err error
78+
c.rpcDuration, err = c.meter.Float64Histogram("rpc."+role+".duration",
79+
metric.WithDescription("Measures the duration of inbound RPC."),
80+
metric.WithUnit("ms"))
81+
if err != nil {
82+
otel.Handle(err)
83+
if c.rpcDuration == nil {
84+
c.rpcDuration = noop.Float64Histogram{}
85+
}
86+
}
87+
88+
rpcRequestSize, err := c.meter.Int64Histogram("rpc."+role+".request.size",
89+
metric.WithDescription("Measures size of RPC request messages (uncompressed)."),
90+
metric.WithUnit("By"))
91+
if err != nil {
92+
otel.Handle(err)
93+
if rpcRequestSize == nil {
94+
rpcRequestSize = noop.Int64Histogram{}
95+
}
96+
}
97+
98+
rpcResponseSize, err := c.meter.Int64Histogram("rpc."+role+".response.size",
99+
metric.WithDescription("Measures size of RPC response messages (uncompressed)."),
100+
metric.WithUnit("By"))
101+
if err != nil {
102+
otel.Handle(err)
103+
if rpcResponseSize == nil {
104+
rpcResponseSize = noop.Int64Histogram{}
105+
}
106+
}
107+
108+
rpcRequestsPerRPC, err := c.meter.Int64Histogram("rpc."+role+".requests_per_rpc",
109+
metric.WithDescription("Measures the number of messages received per RPC. Should be 1 for all non-streaming RPCs."),
110+
metric.WithUnit("{count}"))
111+
if err != nil {
112+
otel.Handle(err)
113+
if rpcRequestsPerRPC == nil {
114+
rpcRequestsPerRPC = noop.Int64Histogram{}
115+
}
116+
}
117+
118+
rpcResponsesPerRPC, err := c.meter.Int64Histogram("rpc."+role+".responses_per_rpc",
119+
metric.WithDescription("Measures the number of messages received per RPC. Should be 1 for all non-streaming RPCs."),
120+
metric.WithUnit("{count}"))
121+
if err != nil {
122+
otel.Handle(err)
123+
if rpcResponsesPerRPC == nil {
124+
rpcResponsesPerRPC = noop.Int64Histogram{}
125+
}
126+
}
127+
128+
switch role {
129+
case "client":
130+
c.rpcInBytes = rpcResponseSize
131+
c.rpcInMessages = rpcResponsesPerRPC
132+
c.rpcOutBytes = rpcRequestSize
133+
c.rpcOutMessages = rpcRequestsPerRPC
134+
case "server":
135+
c.rpcInBytes = rpcRequestSize
136+
c.rpcInMessages = rpcRequestsPerRPC
137+
c.rpcOutBytes = rpcResponseSize
138+
c.rpcOutMessages = rpcResponsesPerRPC
139+
default:
140+
c.rpcInBytes = noop.Int64Histogram{}
141+
c.rpcInMessages = noop.Int64Histogram{}
142+
c.rpcOutBytes = noop.Int64Histogram{}
143+
c.rpcOutMessages = noop.Int64Histogram{}
144+
}
145+
146+
return c
147+
}

0 commit comments

Comments
 (0)