Skip to content

Commit

Permalink
Merge pull request #1364 from Permify/refactor/replace-slog-with-slog…
Browse files Browse the repository at this point in the history
…-context

refactor: slog with context
  • Loading branch information
tolgaOzen authored Jul 17, 2024
2 parents f730b38 + 2f0abcf commit f7f8931
Show file tree
Hide file tree
Showing 25 changed files with 199 additions and 412 deletions.
2 changes: 1 addition & 1 deletion docs/api-reference/apidocs.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"info": {
"title": "Permify API",
"description": "Permify is an open source authorization service for creating fine-grained and scalable authorization systems.",
"version": "v0.9.9",
"version": "v0.10.0",
"contact": {
"name": "API Support",
"url": "https://github.com/Permify/permify/issues",
Expand Down
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ require (
go.opentelemetry.io/otel/exporters/zipkin v1.27.0
go.opentelemetry.io/otel/metric v1.28.0
go.opentelemetry.io/otel/sdk v1.28.0
go.opentelemetry.io/otel/sdk/log v0.4.0
go.opentelemetry.io/otel/sdk/metric v1.27.0
go.opentelemetry.io/otel/trace v1.28.0
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
Expand Down Expand Up @@ -83,7 +82,6 @@ require (
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sethvargo/go-retry v0.2.4 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel/log v0.4.0 // indirect
)

require (
Expand Down Expand Up @@ -160,8 +158,6 @@ require (
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240709073659-82184aa44bb8
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.4.0
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.25.0 // indirect
Expand Down
8 changes: 0 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -428,10 +428,6 @@ go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4=
go.opentelemetry.io/otel/exporters/jaeger v1.17.0 h1:D7UpUy2Xc2wsi1Ras6V40q806WM07rqoCWzXu7Sqy+4=
go.opentelemetry.io/otel/exporters/jaeger v1.17.0/go.mod h1:nPCqOnEH9rNLKqH/+rrUjiMzHJdV1BlpKcTwRTyKkKI=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240709073659-82184aa44bb8 h1:aBQ+PceJa8k6jpjr+E6TSOJxISLFXF8rbALAnKx+IhA=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240709073659-82184aa44bb8/go.mod h1:wg1IdCvTM9S+8Z3cy1150R1xO/YTQqEeJ4RO9bt9rHI=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.4.0 h1:zBPZAISA9NOc5cE8zydqDiS0itvg/P/0Hn9m72a5gvM=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.4.0/go.mod h1:gcj2fFjEsqpV3fXuzAA+0Ze1p2/4MJ4T7d77AmkvueQ=
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.27.0 h1:bFgvUr3/O4PHj3VQcFEuYKvRZJX1SJDQ+11JXuSB3/w=
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc v1.27.0/go.mod h1:xJntEd2KL6Qdg5lwp97HMLQDVeAhrYxmzFseAMDPQ8I=
go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp v1.27.0 h1:CIHWikMsN3wO+wq1Tp5VGdVRTcON+DmOJSfDjXypKOc=
Expand All @@ -444,14 +440,10 @@ go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.27.0 h1:QY7/0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.27.0/go.mod h1:HVkSiDhTM9BoUJU8qE6j2eSWLLXvi1USXjyd2BXT8PY=
go.opentelemetry.io/otel/exporters/zipkin v1.27.0 h1:aXcxb7F6ZDC1o2Z52LDfS2g6M2FB5CrxdR2gzY4QRNs=
go.opentelemetry.io/otel/exporters/zipkin v1.27.0/go.mod h1:+WMURoi4KmVB7ypbFPx3xtZTWen2Ca3lRK9u6DVTO5M=
go.opentelemetry.io/otel/log v0.4.0 h1:/vZ+3Utqh18e8TPjuc3ecg284078KWrR8BRz+PQAj3o=
go.opentelemetry.io/otel/log v0.4.0/go.mod h1:DhGnQvky7pHy82MIRV43iXh3FlKN8UUKftn0KbLOq6I=
go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q=
go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s=
go.opentelemetry.io/otel/sdk v1.28.0 h1:b9d7hIry8yZsgtbmM0DKyPWMMUMlK9NEKuIG4aBqWyE=
go.opentelemetry.io/otel/sdk v1.28.0/go.mod h1:oYj7ClPUA7Iw3m+r7GeEjz0qckQRJK2B8zjcZEfu7Pg=
go.opentelemetry.io/otel/sdk/log v0.4.0 h1:1mMI22L82zLqf6KtkjrRy5BbagOTWdJsqMY/HSqILAA=
go.opentelemetry.io/otel/sdk/log v0.4.0/go.mod h1:AYJ9FVF0hNOgAVzUG/ybg/QttnXhUePWAupmCqtdESo=
go.opentelemetry.io/otel/sdk/metric v1.27.0 h1:5uGNOlpXi+Hbo/DRoI31BSb1v+OGcpv2NemcCrOL8gI=
go.opentelemetry.io/otel/sdk/metric v1.27.0/go.mod h1:we7jJVrYN2kh3mVBlswtPU22K0SA+769l93J6bsyvqw=
go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g=
Expand Down
10 changes: 5 additions & 5 deletions internal/authn/oidc/authn.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ func (oidc *Authn) getKeyWithRetry(keyID string, ctx context.Context) (interface
// fetchKey attempts to fetch the JWKS and retrieve the key for the given keyID.
func (oidc *Authn) fetchKey(keyID string, ctx context.Context) (interface{}, error) {
// Log the attempt to find the key.
slog.Debug("attempting to find key in JWKS", "kid", keyID)
slog.DebugContext(ctx, "attempting to find key in JWKS", "kid", keyID)

// Fetch the JWKS from the configured URI.
jwks, err := oidc.jwksSet.Fetch(ctx, oidc.JwksURI)
Expand All @@ -316,22 +316,22 @@ func (oidc *Authn) fetchKey(keyID string, ctx context.Context) (interface{}, err
}

// Log a successful fetch of the JWKS.
slog.Info("successfully fetched JWKS")
slog.InfoContext(ctx, "successfully fetched JWKS")

// Attempt to find the key in the fetched JWKS using the key ID.
if key, found := jwks.LookupKeyID(keyID); found {
var k interface{}
// Convert the key to a usable format.
if err := key.Raw(&k); err != nil {
slog.Error("failed to get raw public key", "kid", keyID, "error", err)
slog.ErrorContext(ctx, "failed to get raw public key", "kid", keyID, "error", err)
return nil, fmt.Errorf("failed to get raw public key: %w", err)
}
// Log a successful retrieval of the raw public key.
slog.Debug("successfully obtained raw public key", "key", k)
slog.DebugContext(ctx, "successfully obtained raw public key", "key", k)
return k, nil // Return the public key for JWT signature verification.
}
// Log an error if the key ID is not found in the JWKS.
slog.Error("key ID not found in JWKS", "kid", keyID)
slog.ErrorContext(ctx, "key ID not found in JWKS", "kid", keyID)
return nil, fmt.Errorf("kid %s not found", keyID)
}

Expand Down
8 changes: 4 additions & 4 deletions internal/engines/balancer/balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (c *Balancer) Check(ctx context.Context, request *base.PermissionCheckReque
// Fetch the EntityDefinition for the given tenant, entity type, and schema version.
en, _, err := c.schemaReader.ReadEntityDefinition(ctx, request.GetTenantId(), request.GetEntity().GetType(), request.GetMetadata().GetSchemaVersion())
if err != nil {
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
// If an error occurs while reading the entity definition, deny permission and return the error.
return &base.PermissionCheckResponse{
Can: base.CheckResult_CHECK_RESULT_DENIED,
Expand All @@ -119,7 +119,7 @@ func (c *Balancer) Check(ctx context.Context, request *base.PermissionCheckReque
// This key helps in distributing the request.
_, err = h.Write([]byte(engines.GenerateKey(request, isRelational)))
if err != nil {
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return &base.PermissionCheckResponse{
Can: base.CheckResult_CHECK_RESULT_DENIED,
Metadata: &base.PermissionCheckResponseMetadata{
Expand All @@ -134,13 +134,13 @@ func (c *Balancer) Check(ctx context.Context, request *base.PermissionCheckReque
defer cancel()

// Logging the intention to forward the request to the underlying client.
slog.Debug("Forwarding request with key to the underlying client", slog.String("key", k))
slog.DebugContext(ctx, "Forwarding request with key to the underlying client", slog.String("key", k))

// Perform the actual permission check by making a call to the underlying client.
response, err := c.client.Check(withTimeout, request)
if err != nil {
// Log the error and return it.
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return &base.PermissionCheckResponse{
Can: base.CheckResult_CHECK_RESULT_DENIED,
Metadata: &base.PermissionCheckResponseMetadata{
Expand Down
2 changes: 1 addition & 1 deletion internal/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var Identifier = ""
*/
const (
// Version is the last release of the Permify (e.g. v0.1.0)
Version = "v0.9.9"
Version = "v0.10.0"
)

// Function to create a single line of the ASCII art with centered content and color
Expand Down
6 changes: 3 additions & 3 deletions internal/servers/bundleServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (r *BundleServer) Write(ctx context.Context, request *v1.BundleWriteRequest
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand All @@ -85,7 +85,7 @@ func (r *BundleServer) Read(ctx context.Context, request *v1.BundleReadRequest)
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand All @@ -108,7 +108,7 @@ func (r *BundleServer) Delete(ctx context.Context, request *v1.BundleDeleteReque
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand Down
16 changes: 8 additions & 8 deletions internal/servers/dataServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (r *DataServer) ReadRelationships(ctx context.Context, request *v1.Relation
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand Down Expand Up @@ -136,7 +136,7 @@ func (r *DataServer) ReadAttributes(ctx context.Context, request *v1.AttributeRe
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand Down Expand Up @@ -237,7 +237,7 @@ func (r *DataServer) Write(ctx context.Context, request *v1.DataWriteRequest) (*
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand Down Expand Up @@ -306,7 +306,7 @@ func (r *DataServer) WriteRelationships(ctx context.Context, request *v1.Relatio
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand Down Expand Up @@ -338,7 +338,7 @@ func (r *DataServer) Delete(ctx context.Context, request *v1.DataDeleteRequest)
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand Down Expand Up @@ -370,7 +370,7 @@ func (r *DataServer) DeleteRelationships(ctx context.Context, request *v1.Relati
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand All @@ -397,7 +397,7 @@ func (r *DataServer) RunBundle(ctx context.Context, request *v1.BundleRunRequest
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand All @@ -410,7 +410,7 @@ func (r *DataServer) RunBundle(ctx context.Context, request *v1.BundleRunRequest
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand Down
12 changes: 6 additions & 6 deletions internal/servers/permissionServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (r *PermissionServer) Check(ctx context.Context, request *v1.PermissionChec
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand All @@ -60,7 +60,7 @@ func (r *PermissionServer) Expand(ctx context.Context, request *v1.PermissionExp
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand All @@ -81,7 +81,7 @@ func (r *PermissionServer) LookupEntity(ctx context.Context, request *v1.Permiss
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand All @@ -102,7 +102,7 @@ func (r *PermissionServer) LookupEntityStream(request *v1.PermissionLookupEntity
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return status.Error(GetStatus(err), err.Error())
}

Expand All @@ -123,7 +123,7 @@ func (r *PermissionServer) LookupSubject(ctx context.Context, request *v1.Permis
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand All @@ -144,7 +144,7 @@ func (r *PermissionServer) SubjectPermission(ctx context.Context, request *v1.Pe
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand Down
6 changes: 3 additions & 3 deletions internal/servers/schemaServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (r *SchemaServer) Write(ctx context.Context, request *v1.SchemaWriteRequest
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand Down Expand Up @@ -220,7 +220,7 @@ func (r *SchemaServer) Read(ctx context.Context, request *v1.SchemaReadRequest)
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand All @@ -242,7 +242,7 @@ func (r *SchemaServer) List(ctx context.Context, request *v1.SchemaListRequest)
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand Down
6 changes: 3 additions & 3 deletions internal/servers/tenancyServer.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (t *TenancyServer) Create(ctx context.Context, request *v1.TenantCreateRequ
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand All @@ -55,7 +55,7 @@ func (t *TenancyServer) Delete(ctx context.Context, request *v1.TenantDeleteRequ
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand All @@ -73,7 +73,7 @@ func (t *TenancyServer) List(ctx context.Context, request *v1.TenantListRequest)
if err != nil {
span.RecordError(err)
span.SetStatus(otelCodes.Error, err.Error())
slog.Error(err.Error())
slog.ErrorContext(ctx, err.Error())
return nil, status.Error(GetStatus(err), err.Error())
}

Expand Down
6 changes: 3 additions & 3 deletions internal/storage/postgres/bundleReader.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (b *BundleReader) Read(ctx context.Context, tenantID, name string) (bundle
ctx, span := tracer.Start(ctx, "bundle-reader.read-bundle")
defer span.End()

slog.Debug("reading bundle", slog.Any("tenant_id", tenantID), slog.Any("name", name))
slog.DebugContext(ctx, "reading bundle", slog.Any("tenant_id", tenantID), slog.Any("name", name))

builder := b.database.Builder.Select("payload").From(BundlesTable).Where(squirrel.Eq{"name": name, "tenant_id": tenantID})

Expand All @@ -45,7 +45,7 @@ func (b *BundleReader) Read(ctx context.Context, tenantID, name string) (bundle
return nil, utils.HandleError(ctx, span, err, base.ErrorCode_ERROR_CODE_SQL_BUILDER)
}

slog.Debug("executing sql query", slog.Any("query", query), slog.Any("arguments", args))
slog.DebugContext(ctx, "executing sql query", slog.Any("query", query), slog.Any("arguments", args))

var row pgx.Row
row = b.database.ReadPool.QueryRow(ctx, query, args...)
Expand All @@ -66,7 +66,7 @@ func (b *BundleReader) Read(ctx context.Context, tenantID, name string) (bundle
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())

slog.Error("failed to convert the value to bundle", slog.Any("error", err))
slog.ErrorContext(ctx, "failed to convert the value to bundle", slog.Any("error", err))

return nil, errors.New(base.ErrorCode_ERROR_CODE_INVALID_ARGUMENT.String())
}
Expand Down
Loading

0 comments on commit f7f8931

Please sign in to comment.