Skip to content

Commit e1f6fc4

Browse files
authored
Merge pull request #1754 from Permify/ufuk/monitoring-histograms
refactor: update OTLP Histogram Records
2 parents 7679460 + eba5a1f commit e1f6fc4

File tree

10 files changed

+64
-129
lines changed

10 files changed

+64
-129
lines changed

docs/api-reference/apidocs.swagger.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"info": {
44
"title": "Permify API",
55
"description": "Permify is an open source authorization service for creating fine-grained and scalable authorization systems.",
6-
"version": "v1.1.9",
6+
"version": "v1.2.0",
77
"contact": {
88
"name": "API Support",
99
"url": "https://github.com/Permify/permify/issues",

docs/api-reference/openapiv2/apidocs.swagger.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"info": {
44
"title": "Permify API",
55
"description": "Permify is an open source authorization service for creating fine-grained and scalable authorization systems.",
6-
"version": "v1.1.9",
6+
"version": "v1.2.0",
77
"contact": {
88
"name": "API Support",
99
"url": "https://github.com/Permify/permify/issues",

internal/engines/cache/check.go

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package cache
33
import (
44
"context"
55
"encoding/hex"
6-
"time"
76

87
api "go.opentelemetry.io/otel/metric"
98

@@ -26,8 +25,7 @@ type CheckEngineWithCache struct {
2625
cache cache.Cache
2726

2827
// Metrics
29-
cacheCounter api.Int64Counter
30-
cacheHitDurationHistogram api.Int64Histogram
28+
cacheHitHistogram api.Int64Histogram
3129
}
3230

3331
// NewCheckEngineWithCache creates a new instance of EngineKeyManager by initializing an EngineKeys
@@ -38,11 +36,10 @@ func NewCheckEngineWithCache(
3836
cache cache.Cache,
3937
) invoke.Check {
4038
return &CheckEngineWithCache{
41-
schemaReader: schemaReader,
42-
checker: checker,
43-
cache: cache,
44-
cacheCounter: telemetry.NewCounter(internal.Meter, "cache_check_count", "Number of permission cached checks performed"),
45-
cacheHitDurationHistogram: telemetry.NewHistogram(internal.Meter, "cache_hit_duration", "microseconds", "Duration of cache hits in microseconds"),
39+
schemaReader: schemaReader,
40+
checker: checker,
41+
cache: cache,
42+
cacheHitHistogram: telemetry.NewHistogram(internal.Meter, "cache_hit", "amount", "Number of cache hits"),
4643
}
4744
}
4845

@@ -67,15 +64,8 @@ func (c *CheckEngineWithCache) Check(ctx context.Context, request *base.Permissi
6764

6865
// If a cached result is found, handle exclusion and return the result.
6966
if found {
70-
ctx, span := internal.Tracer.Start(ctx, "hit")
71-
defer span.End()
72-
start := time.Now()
73-
74-
// Increase the check count in the metrics.
75-
c.cacheCounter.Add(ctx, 1)
76-
77-
duration := time.Now().Sub(start)
78-
c.cacheHitDurationHistogram.Record(ctx, duration.Microseconds())
67+
// Increase the hit count in the metrics.
68+
c.cacheHitHistogram.Record(ctx, 1)
7969

8070
// If the request doesn't have the exclusion flag set, return the cached result.
8171
return &base.PermissionCheckResponse{

internal/engines/cache/check_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ var _ = Describe("cache", func() {
4141
Expect(err).ShouldNot(HaveOccurred())
4242

4343
// Initialize a new EngineKeys struct with a new cache.Cache instance
44-
engineKeys := CheckEngineWithCache{nil, nil, cache, nil, nil}
44+
engineKeys := CheckEngineWithCache{nil, nil, cache, nil}
4545

4646
// Create a new PermissionCheckRequest and PermissionCheckResponse
4747
checkReq := &base.PermissionCheckRequest{
@@ -91,7 +91,7 @@ var _ = Describe("cache", func() {
9191
Expect(err).ShouldNot(HaveOccurred())
9292

9393
// Initialize a new EngineKeys struct with a new cache.Cache instance
94-
engineKeys := CheckEngineWithCache{nil, nil, cache, nil, nil}
94+
engineKeys := CheckEngineWithCache{nil, nil, cache, nil}
9595

9696
// Create a new PermissionCheckRequest and PermissionCheckResponse
9797
checkReq := &base.PermissionCheckRequest{
@@ -141,7 +141,7 @@ var _ = Describe("cache", func() {
141141
Expect(err).ShouldNot(HaveOccurred())
142142

143143
// Initialize a new EngineKeys struct with a new cache.Cache instance
144-
engineKeys := CheckEngineWithCache{nil, nil, cache, nil, nil}
144+
engineKeys := CheckEngineWithCache{nil, nil, cache, nil}
145145

146146
// Create a new PermissionCheckRequest and PermissionCheckResponse
147147
checkReq := &base.PermissionCheckRequest{
@@ -285,7 +285,7 @@ var _ = Describe("cache", func() {
285285
Expect(err).ShouldNot(HaveOccurred())
286286

287287
// Initialize a new EngineKeys struct with a new cache.Cache instance
288-
engineKeys := CheckEngineWithCache{nil, nil, cache, nil, nil}
288+
engineKeys := CheckEngineWithCache{nil, nil, cache, nil}
289289

290290
// Create a new PermissionCheckRequest
291291
checkReq := &base.PermissionCheckRequest{
@@ -320,7 +320,7 @@ var _ = Describe("cache", func() {
320320
Expect(err).ShouldNot(HaveOccurred())
321321

322322
// Initialize a new EngineKeys struct with a new cache.Cache instance
323-
engineKeys := CheckEngineWithCache{nil, nil, cache, nil, nil}
323+
engineKeys := CheckEngineWithCache{nil, nil, cache, nil}
324324

325325
// Create some new PermissionCheckRequests and PermissionCheckResponses
326326
checkReq1 := &base.PermissionCheckRequest{

internal/info.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ var Identifier = ""
2323
*/
2424
const (
2525
// Version is the last release of the Permify (e.g. v0.1.0)
26-
Version = "v1.1.9"
26+
Version = "v1.2.0"
2727
)
2828

2929
// Function to create a single line of the ASCII art with centered content and color

internal/invoke/invoke.go

Lines changed: 27 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package invoke
33
import (
44
"context"
55
"sync/atomic"
6-
"time"
76

87
"go.opentelemetry.io/otel/attribute"
98
otelCodes "go.opentelemetry.io/otel/codes"
9+
"go.opentelemetry.io/otel/metric"
1010
api "go.opentelemetry.io/otel/metric"
1111
"go.opentelemetry.io/otel/trace"
1212

@@ -68,16 +68,10 @@ type DirectInvoker struct {
6868
// LookupSubject
6969
sp SubjectPermission
7070

71-
// Metrics
72-
checkCounter api.Int64Counter
73-
lookupEntityCounter api.Int64Counter
74-
lookupSubjectCounter api.Int64Counter
75-
subjectPermissionCounter api.Int64Counter
76-
77-
checkDurationHistogram api.Int64Histogram
78-
lookupEntityDurationHistogram api.Int64Histogram
79-
lookupSubjectDurationHistogram api.Int64Histogram
80-
subjectPermissionDurationHistogram api.Int64Histogram
71+
checkHistogram api.Int64Histogram
72+
lookupEntityHistogram api.Int64Histogram
73+
lookupSubjectHistogram api.Int64Histogram
74+
subjectPermissionHistogram api.Int64Histogram
8175
}
8276

8377
// NewDirectInvoker is a constructor for DirectInvoker.
@@ -92,20 +86,17 @@ func NewDirectInvoker(
9286
sp SubjectPermission,
9387
) *DirectInvoker {
9488
return &DirectInvoker{
95-
schemaReader: schemaReader,
96-
dataReader: dataReader,
97-
cc: cc,
98-
ec: ec,
99-
lo: lo,
100-
sp: sp,
101-
checkCounter: telemetry.NewCounter(internal.Meter, "check_count", "Number of permission checks performed"),
102-
lookupEntityCounter: telemetry.NewCounter(internal.Meter, "lookup_entity_count", "Number of permission lookup entity performed"),
103-
lookupSubjectCounter: telemetry.NewCounter(internal.Meter, "lookup_subject_count", "Number of permission lookup subject performed"),
104-
subjectPermissionCounter: telemetry.NewCounter(internal.Meter, "subject_permission_count", "Number of subject permission performed"),
105-
checkDurationHistogram: telemetry.NewHistogram(internal.Meter, "check_duration", "microseconds", "Duration of checks in microseconds"),
106-
lookupEntityDurationHistogram: telemetry.NewHistogram(internal.Meter, "lookup_entity_duration", "microseconds", "Duration of lookup entity duration in microseconds"),
107-
lookupSubjectDurationHistogram: telemetry.NewHistogram(internal.Meter, "lookup_subject_duration", "microseconds", "Duration of lookup subject duration in microseconds"),
108-
subjectPermissionDurationHistogram: telemetry.NewHistogram(internal.Meter, "subject_permission_duration", "microseconds", "Duration of subject permission duration in microseconds"),
89+
schemaReader: schemaReader,
90+
dataReader: dataReader,
91+
cc: cc,
92+
ec: ec,
93+
lo: lo,
94+
sp: sp,
95+
checkHistogram: telemetry.NewHistogram(internal.Meter, "check", "amount", "Number of checks"),
96+
97+
lookupEntityHistogram: telemetry.NewHistogram(internal.Meter, "lookup_entity", "amount", "Number of lookup entity"),
98+
lookupSubjectHistogram: telemetry.NewHistogram(internal.Meter, "lookup_subject", "amount", "Number of lookup subject"),
99+
subjectPermissionHistogram: telemetry.NewHistogram(internal.Meter, "subject_permission", "amount", "Number of subject permission"),
109100
}
110101
}
111102

@@ -120,8 +111,13 @@ func (invoker *DirectInvoker) Check(ctx context.Context, request *base.Permissio
120111
attribute.KeyValue{Key: "subject", Value: attribute.StringValue(tuple.SubjectToString(request.GetSubject()))},
121112
))
122113
defer span.End()
123-
124-
start := time.Now()
114+
invoker.checkHistogram.Record(ctx, 1,
115+
metric.WithAttributeSet(
116+
attribute.NewSet(
117+
attribute.KeyValue{Key: "subject_id", Value: attribute.StringValue(request.GetSubject().GetId())},
118+
attribute.KeyValue{Key: "subject_type", Value: attribute.StringValue(request.GetSubject().GetType())},
119+
)),
120+
)
125121

126122
// Validate the depth of the request.
127123
err = checkDepth(request)
@@ -186,15 +182,10 @@ func (invoker *DirectInvoker) Check(ctx context.Context, request *base.Permissio
186182
},
187183
}, err
188184
}
189-
duration := time.Since(start)
190-
invoker.checkDurationHistogram.Record(ctx, duration.Microseconds())
191185

192186
// increaseCheckCount increments the CheckCount value in the response metadata by 1.
193187
atomic.AddInt32(&response.GetMetadata().CheckCount, +1)
194188

195-
// Increase the check count in the metrics.
196-
invoker.checkCounter.Add(ctx, 1)
197-
198189
span.SetAttributes(attribute.KeyValue{Key: "can", Value: attribute.StringValue(response.GetCan().String())})
199190
return
200191
}
@@ -245,8 +236,6 @@ func (invoker *DirectInvoker) LookupEntity(ctx context.Context, request *base.Pe
245236
))
246237
defer span.End()
247238

248-
start := time.Now()
249-
250239
// Set SnapToken if not provided
251240
if request.GetMetadata().GetSnapToken() == "" { // Check if the request has a SnapToken.
252241
var st token.SnapToken
@@ -271,11 +260,7 @@ func (invoker *DirectInvoker) LookupEntity(ctx context.Context, request *base.Pe
271260

272261
resp, err := invoker.lo.LookupEntity(ctx, request)
273262

274-
duration := time.Since(start)
275-
invoker.lookupEntityDurationHistogram.Record(ctx, duration.Microseconds())
276-
277-
// Increase the lookup entity count in the metrics.
278-
invoker.lookupEntityCounter.Add(ctx, 1)
263+
invoker.lookupEntityHistogram.Record(ctx, 1)
279264

280265
return resp, err
281266
}
@@ -292,8 +277,6 @@ func (invoker *DirectInvoker) LookupEntityStream(ctx context.Context, request *b
292277
))
293278
defer span.End()
294279

295-
start := time.Now()
296-
297280
// Set SnapToken if not provided
298281
if request.GetMetadata().GetSnapToken() == "" { // Check if the request has a SnapToken.
299282
var st token.SnapToken
@@ -318,11 +301,7 @@ func (invoker *DirectInvoker) LookupEntityStream(ctx context.Context, request *b
318301

319302
resp := invoker.lo.LookupEntityStream(ctx, request, server)
320303

321-
duration := time.Since(start)
322-
invoker.lookupEntityDurationHistogram.Record(ctx, duration.Microseconds())
323-
324-
// Increase the lookup entity count in the metrics.
325-
invoker.lookupEntityCounter.Add(ctx, 1)
304+
invoker.lookupEntityHistogram.Record(ctx, 1)
326305

327306
return resp
328307
}
@@ -338,8 +317,6 @@ func (invoker *DirectInvoker) LookupSubject(ctx context.Context, request *base.P
338317
))
339318
defer span.End()
340319

341-
start := time.Now()
342-
343320
// Check if the request has a SnapToken. If not, a SnapToken is set.
344321
if request.GetMetadata().GetSnapToken() == "" {
345322
// Create an instance of SnapToken
@@ -370,11 +347,7 @@ func (invoker *DirectInvoker) LookupSubject(ctx context.Context, request *base.P
370347

371348
resp, err := invoker.lo.LookupSubject(ctx, request)
372349

373-
duration := time.Now().Sub(start)
374-
invoker.lookupSubjectDurationHistogram.Record(ctx, duration.Microseconds())
375-
376-
// Increase the lookup subject count in the metrics.
377-
invoker.lookupSubjectCounter.Add(ctx, 1)
350+
invoker.lookupSubjectHistogram.Record(ctx, 1)
378351

379352
// Call the LookupSubject function of the ls field in the invoker, pass the context and request,
380353
// and return its response and error
@@ -391,8 +364,6 @@ func (invoker *DirectInvoker) SubjectPermission(ctx context.Context, request *ba
391364
))
392365
defer span.End()
393366

394-
start := time.Now()
395-
396367
// Check if the request has a SnapToken. If not, a SnapToken is set.
397368
if request.GetMetadata().GetSnapToken() == "" {
398369
// Create an instance of SnapToken
@@ -422,11 +393,7 @@ func (invoker *DirectInvoker) SubjectPermission(ctx context.Context, request *ba
422393
}
423394
resp, err := invoker.sp.SubjectPermission(ctx, request)
424395

425-
duration := time.Now().Sub(start)
426-
invoker.subjectPermissionDurationHistogram.Record(ctx, duration.Microseconds())
427-
428-
// Increase the subject permission count in the metrics.
429-
invoker.subjectPermissionCounter.Add(ctx, 1)
396+
invoker.subjectPermissionHistogram.Record(ctx, 1)
430397

431398
// Call the SubjectPermission function of the ls field in the invoker, pass the context and request,
432399
// and return its response and error

0 commit comments

Comments
 (0)