github.com/xmidt-org/webpa-common@v1.11.9/basculechecks/metrics.go (about) 1 /** 2 * Copyright 2020 Comcast Cable Communications Management, LLC 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 * 16 */ 17 18 package basculechecks 19 20 import ( 21 "fmt" 22 23 "github.com/go-kit/kit/log" 24 "github.com/go-kit/kit/log/level" 25 "github.com/go-kit/kit/metrics" 26 gokitprometheus "github.com/go-kit/kit/metrics/prometheus" 27 "github.com/go-kit/kit/metrics/provider" 28 "github.com/prometheus/client_golang/prometheus" 29 "github.com/xmidt-org/themis/xlog" 30 themisXmetrics "github.com/xmidt-org/themis/xmetrics" 31 "github.com/xmidt-org/webpa-common/xmetrics" 32 33 "go.uber.org/fx" 34 ) 35 36 // Names for our metrics 37 const ( 38 AuthCapabilityCheckOutcome = "auth_capability_check" 39 ) 40 41 // labels 42 const ( 43 OutcomeLabel = "outcome" 44 ReasonLabel = "reason" 45 ClientIDLabel = "clientid" 46 EndpointLabel = "endpoint" 47 PartnerIDLabel = "partnerid" 48 ServerLabel = "server" 49 ) 50 51 // outcomes 52 const ( 53 RejectedOutcome = "rejected" 54 AcceptedOutcome = "accepted" 55 // reasons 56 TokenMissing = "auth_missing" 57 UndeterminedPartnerID = "undetermined_partner_ID" 58 UndeterminedCapabilities = "undetermined_capabilities" 59 EmptyCapabilitiesList = "empty_capabilities_list" 60 TokenMissingValues = "auth_is_missing_values" 61 NoCapabilityChecker = "no_capability_checker" 62 NoCapabilitiesMatch = "no_capabilities_match" 63 EmptyParsedURL = "empty_parsed_URL" 64 ) 65 66 // help messages 67 const ( 68 capabilityCheckHelpMsg = "Counter for the capability checker, providing outcome information by client, partner, and endpoint" 69 ) 70 71 // Metrics returns the Metrics relevant to this package targeting our older non uber/fx applications. 72 // To initialize the metrics, use NewAuthCapabilityCheckMeasures(). 73 func Metrics() []xmetrics.Metric { 74 return []xmetrics.Metric{ 75 { 76 Name: AuthCapabilityCheckOutcome, 77 Type: xmetrics.CounterType, 78 Help: capabilityCheckHelpMsg, 79 LabelNames: []string{OutcomeLabel, ReasonLabel, ClientIDLabel, PartnerIDLabel, EndpointLabel}, 80 }, 81 } 82 } 83 84 // ProvideMetrics provides the metrics relevant to this package as uber/fx options. 85 // This is now deprecated in favor of ProvideMetricsVec. 86 func ProvideMetrics() fx.Option { 87 return fx.Provide( 88 themisXmetrics.ProvideCounter(prometheus.CounterOpts{ 89 Name: AuthCapabilityCheckOutcome, 90 Help: capabilityCheckHelpMsg, 91 ConstLabels: nil, 92 }, OutcomeLabel, ReasonLabel, ClientIDLabel, PartnerIDLabel, EndpointLabel), 93 ) 94 } 95 96 // ProvideMetricsVec provides the metrics relevant to this package as uber/fx options. 97 // The provided metrics are prometheus vectors which gives access to more advanced operations such as CurryWith(labels). 98 func ProvideMetricsVec() fx.Option { 99 return fx.Provide( 100 themisXmetrics.ProvideCounterVec(prometheus.CounterOpts{ 101 Name: AuthCapabilityCheckOutcome, 102 Help: capabilityCheckHelpMsg, 103 ConstLabels: nil, 104 }, ServerLabel, OutcomeLabel, ReasonLabel, ClientIDLabel, PartnerIDLabel, EndpointLabel), 105 ) 106 } 107 108 // AuthCapabilityCheckMeasures describes the defined metrics that will be used by clients 109 type AuthCapabilityCheckMeasures struct { 110 CapabilityCheckOutcome metrics.Counter 111 } 112 113 // NewAuthCapabilityCheckMeasures realizes desired metrics. It's intended to be used alongside Metrics() for 114 // our older non uber/fx applications. 115 func NewAuthCapabilityCheckMeasures(p provider.Provider) *AuthCapabilityCheckMeasures { 116 return &AuthCapabilityCheckMeasures{ 117 CapabilityCheckOutcome: p.NewCounter(AuthCapabilityCheckOutcome), 118 } 119 } 120 121 // BaseMeasuresIn is an uber/fx parameter with base metrics ready to be curried into child metrics based on 122 // custom labels. 123 type BaseMeasuresIn struct { 124 fx.In 125 Logger log.Logger 126 CapabilityCheckOutcome *prometheus.CounterVec `name:"auth_capability_check"` 127 } 128 129 // MeasuresFactory facilitates the creation of child metrics based on server labels. 130 type MeasuresFactory struct { 131 ServerName string 132 } 133 134 // NewMeasures builds the metric listener from the provided raw metrics. 135 func (m MeasuresFactory) NewMeasures(in BaseMeasuresIn) (*AuthCapabilityCheckMeasures, error) { 136 capabilityCheckOutcomeCounterVec, err := in.CapabilityCheckOutcome.CurryWith(prometheus.Labels{ServerLabel: m.ServerName}) 137 if err != nil { 138 return nil, err 139 } 140 in.Logger.Log(level.Key(), level.DebugValue(), xlog.MessageKey(), "building auth capability measures", ServerLabel, m.ServerName) 141 return &AuthCapabilityCheckMeasures{ 142 CapabilityCheckOutcome: gokitprometheus.NewCounter(capabilityCheckOutcomeCounterVec), 143 }, nil 144 } 145 146 // Annotated provides the measures as an annotated component with the name "[SERVER]_bascule_capability_measures" 147 func (m MeasuresFactory) Annotated() fx.Annotated { 148 return fx.Annotated{ 149 Name: fmt.Sprintf("%s_bascule_capability_measures", m.ServerName), 150 Target: m.NewMeasures, 151 } 152 }