Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refact: uuid func, new otelgrpc #638

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions envoyauth/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ func getParsedPathAndQuery(path string) ([]interface{}, map[string]interface{},
parsedPathInterface[i] = v
}

parsedQueryInterface := make(map[string]interface{})
for paramKey, paramValues := range parsedURL.Query() {
query := parsedURL.Query()
parsedQueryInterface := make(map[string]interface{}, len(query))
for paramKey, paramValues := range query {
queryValues := make([]interface{}, len(paramValues))
for i, v := range paramValues {
queryValues[i] = v
Expand Down
10 changes: 2 additions & 8 deletions envoyauth/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
ext_core_v3 "github.com/envoyproxy/go-control-plane/envoy/config/core/v3"
ext_type_v3 "github.com/envoyproxy/go-control-plane/envoy/type/v3"
_structpb "github.com/golang/protobuf/ptypes/struct"
"github.com/open-policy-agent/opa-envoy-plugin/internal/util"
"github.com/google/uuid"
"github.com/open-policy-agent/opa/metrics"
"github.com/open-policy-agent/opa/storage"
"github.com/open-policy-agent/opa/topdown/builtins"
Expand Down Expand Up @@ -41,8 +41,6 @@ func noopTransactionCloser(context.Context, error) error {

// NewEvalResult creates a new EvalResult and a StopFunc that is used to stop the timer for metrics
func NewEvalResult(opts ...func(*EvalResult)) (*EvalResult, StopFunc, error) {
var err error

er := &EvalResult{
Metrics: metrics.New(),
}
Expand All @@ -52,11 +50,7 @@ func NewEvalResult(opts ...func(*EvalResult)) (*EvalResult, StopFunc, error) {
}

if er.DecisionID == "" {
er.DecisionID, err = util.UUID4()
}

if err != nil {
return nil, nil, err
er.DecisionID = uuid.NewString()
}

er.Metrics.Timer(metrics.ServerHandler).Start()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ require (
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/flatbuffers v24.12.23+incompatible // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/google/uuid v1.6.0
github.com/gorilla/mux v1.8.1 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.25.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
3 changes: 1 addition & 2 deletions internal/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,7 @@ func New(m *plugins.Manager, cfg *Config) plugins.Plugin {
otelhttp.WithPropagators(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}, b3.New(b3.WithInjectEncoding(b3.B3MultipleHeader|b3.B3SingleHeader)))),
)
grpcOpts = append(grpcOpts,
grpc.UnaryInterceptor(otelgrpc.UnaryServerInterceptor(grpcTracingOption...)),
grpc.StreamInterceptor(otelgrpc.StreamServerInterceptor(grpcTracingOption...)),
grpc.StatsHandler(otelgrpc.NewServerHandler(grpcTracingOption...)),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please explain this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

)
}

Expand Down
19 changes: 2 additions & 17 deletions internal/util/util.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package util

import (
"crypto/rand"
"fmt"
"io"
"io/ioutil"
"os"

"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protodesc"
Expand All @@ -14,7 +11,7 @@ import (

// ReadProtoSet - Reads protobuf files from disk
func ReadProtoSet(path string) (*protoregistry.Files, error) {
protoSet, err := ioutil.ReadFile(path)
protoSet, err := os.ReadFile(path)
if err != nil {
return nil, err
}
Expand All @@ -24,15 +21,3 @@ func ReadProtoSet(path string) (*protoregistry.Files, error) {
}
return protodesc.NewFiles(&fileSet)
}

// UUID4 Generates a new universally unique identifier
func UUID4() (string, error) {
bs := make([]byte, 16)
n, err := io.ReadFull(rand.Reader, bs)
if n != len(bs) || err != nil {
return "", err
}
bs[8] = bs[8]&^0xc0 | 0x80
bs[6] = bs[6]&^0xf0 | 0x40
return fmt.Sprintf("%x-%x-%x-%x-%x", bs[0:4], bs[4:6], bs[6:8], bs[8:10], bs[10:]), nil
}