Skip to content

Commit

Permalink
refact: uuid func, new otelgrpc
Browse files Browse the repository at this point in the history
Signed-off-by: Anthony Regeda <[email protected]>
  • Loading branch information
regeda authored and ashutosh-narkar committed Jan 9, 2025
1 parent bec106e commit eafceb1
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 30 deletions.
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...)),
)
}

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
}

0 comments on commit eafceb1

Please sign in to comment.