github.com/rajeev159/opa@v0.45.0/topdown/instrumentation.go (about)

     1  // Copyright 2018 The OPA Authors.  All rights reserved.
     2  // Use of this source code is governed by an Apache2
     3  // license that can be found in the LICENSE file.
     4  
     5  package topdown
     6  
     7  import "github.com/open-policy-agent/opa/metrics"
     8  
     9  const (
    10  	evalOpPlug                    = "eval_op_plug"
    11  	evalOpResolve                 = "eval_op_resolve"
    12  	evalOpRuleIndex               = "eval_op_rule_index"
    13  	evalOpBuiltinCall             = "eval_op_builtin_call"
    14  	evalOpVirtualCacheHit         = "eval_op_virtual_cache_hit"
    15  	evalOpVirtualCacheMiss        = "eval_op_virtual_cache_miss"
    16  	evalOpBaseCacheHit            = "eval_op_base_cache_hit"
    17  	evalOpBaseCacheMiss           = "eval_op_base_cache_miss"
    18  	evalOpComprehensionCacheSkip  = "eval_op_comprehension_cache_skip"
    19  	evalOpComprehensionCacheBuild = "eval_op_comprehension_cache_build"
    20  	evalOpComprehensionCacheHit   = "eval_op_comprehension_cache_hit"
    21  	evalOpComprehensionCacheMiss  = "eval_op_comprehension_cache_miss"
    22  	partialOpSaveUnify            = "partial_op_save_unify"
    23  	partialOpSaveSetContains      = "partial_op_save_set_contains"
    24  	partialOpSaveSetContainsRec   = "partial_op_save_set_contains_rec"
    25  	partialOpCopyPropagation      = "partial_op_copy_propagation"
    26  )
    27  
    28  // Instrumentation implements helper functions to instrument query evaluation
    29  // to diagnose performance issues. Instrumentation may be expensive in some
    30  // cases, so it is disabled by default.
    31  type Instrumentation struct {
    32  	m metrics.Metrics
    33  }
    34  
    35  // NewInstrumentation returns a new Instrumentation object. Performance
    36  // diagnostics recorded on this Instrumentation object will stored in m.
    37  func NewInstrumentation(m metrics.Metrics) *Instrumentation {
    38  	return &Instrumentation{
    39  		m: m,
    40  	}
    41  }
    42  
    43  func (instr *Instrumentation) startTimer(name string) {
    44  	if instr == nil {
    45  		return
    46  	}
    47  	instr.m.Timer(name).Start()
    48  }
    49  
    50  func (instr *Instrumentation) stopTimer(name string) {
    51  	if instr == nil {
    52  		return
    53  	}
    54  	delta := instr.m.Timer(name).Stop()
    55  	instr.m.Histogram(name).Update(delta)
    56  }
    57  
    58  func (instr *Instrumentation) counterIncr(name string) {
    59  	if instr == nil {
    60  		return
    61  	}
    62  	instr.m.Counter(name).Incr()
    63  }