Skip to content

Commit

Permalink
Merge pull request #1247 from Permify/next
Browse files Browse the repository at this point in the history
feat(tracing): refactor error handling and integrate tracers in database configs
  • Loading branch information
tolgaOzen authored May 8, 2024
2 parents 7b705e5 + ef22f3e commit 64cab86
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 24 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.8.3",
"version": "v0.8.4",
"contact": {
"name": "API Support",
"url": "https://github.com/Permify/permify/issues",
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ require (
require (
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/exaring/otelpgx v0.5.4 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1m
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/envoyproxy/protoc-gen-validate v1.0.4 h1:gVPz/FMfvh57HdSJQyvBtF00j8JU4zdyUgIUNhlgg0A=
github.com/envoyproxy/protoc-gen-validate v1.0.4/go.mod h1:qys6tmnRsYrQqIhm2bvKZH4Blx/1gTIZ2UKVY1M+Yew=
github.com/exaring/otelpgx v0.5.4 h1:uytSs8A9/8tpnJ4J8jsusbRtNgP6Cn5npnffCxE2Unk=
github.com/exaring/otelpgx v0.5.4/go.mod h1:DuRveXIeRNz6VJrMTj2uCBFqiocMx4msCN1mIMmbZUI=
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.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
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.8.3"
Version = "v0.8.4"
)

// Function to create a single line of the ASCII art with centered content and color
Expand Down
27 changes: 18 additions & 9 deletions internal/storage/postgres/utils/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,17 +128,26 @@ func GenerateGCQuery(table string, value uint64) squirrel.DeleteBuilder {
// This function is used for consistent error handling across different parts of the application.
func HandleError(ctx context.Context, span trace.Span, err error, errorCode base.ErrorCode) error {
// Check if the error is context-related
if IsContextRelatedError(ctx, err) || IsSerializationRelatedError(err) {
// Use debug level logging for context or serialization-related errors
slog.Debug("an error related to context or serialization was encountered during the operation", slog.String("error", err.Error()))
} else {
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())
// Use error level logging for all other errors
slog.Error("error encountered", slog.Any("error", err))
if IsContextRelatedError(ctx, err) {
slog.Debug("A context-related error occurred",
slog.String("error", err.Error()))
return errors.New(base.ErrorCode_ERROR_CODE_CANCELLED.String())
}

// Return a new standardized error with the provided error code
// Check if the error is serialization-related
if IsSerializationRelatedError(err) {
slog.Debug("A serialization-related error occurred",
slog.String("error", err.Error()))
return errors.New(base.ErrorCode_ERROR_CODE_SERIALIZATION.String())
}

// For all other types of errors, log them at the error level and record them in the span
slog.Error("An operational error occurred",
slog.Any("error", err))
span.RecordError(err)
span.SetStatus(codes.Error, err.Error())

// Return a new error with the standard error code provided
return errors.New(errorCode.String())
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/database/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"time"

"github.com/exaring/otelpgx"

"github.com/cenkalti/backoff/v4"

"golang.org/x/exp/slog"
Expand Down Expand Up @@ -88,6 +90,9 @@ func New(uri string, opts ...Option) (*Postgres, error) {
writeConfig.MaxConnLifetimeJitter = time.Duration(0.2 * float64(pg.maxConnectionLifeTime))
readConfig.MaxConnLifetimeJitter = time.Duration(0.2 * float64(pg.maxConnectionLifeTime))

writeConfig.ConnConfig.Tracer = otelpgx.NewTracer()
readConfig.ConnConfig.Tracer = otelpgx.NewTracer()

// Create connection pools for both writing and reading operations using the configured settings.
pg.WritePool, pg.ReadPool, err = createPools(
context.Background(), // Context used to control the lifecycle of the pools.
Expand Down
27 changes: 16 additions & 11 deletions pkg/pb/base/v1/errors.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/pb/base/v1/openapi.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions proto/base/v1/errors.proto
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ enum ErrorCode {
ERROR_CODE_CANNOT_CONVERT_TO_ENTITY_STATEMENT = 5016;
ERROR_CODE_CANNOT_CONVERT_TO_RELATION_STATEMENT = 5017;
ERROR_CODE_CANNOT_CONVERT_TO_ATTRIBUTE_STATEMENT = 5018;
ERROR_CODE_SERIALIZATION = 5019;
}

// ErrorResponse
Expand Down
2 changes: 1 addition & 1 deletion proto/base/v1/openapi.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
info: {
title: "Permify API";
description: "Permify is an open source authorization service for creating fine-grained and scalable authorization systems.";
version: "v0.8.3";
version: "v0.8.4";
contact: {
name: "API Support";
url: "https://github.com/Permify/permify/issues";
Expand Down

0 comments on commit 64cab86

Please sign in to comment.