1// Copyright 2017 Google LLC 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15package spanner 16 17import ( 18 "context" 19 20 "go.opencensus.io/stats" 21 "go.opencensus.io/stats/view" 22 "go.opencensus.io/tag" 23) 24 25const statsPrefix = "cloud.google.com/go/spanner/" 26 27var ( 28 tagClientID = tag.MustNewKey("client_id") 29 tagDatabase = tag.MustNewKey("database") 30 tagInstance = tag.MustNewKey("instance_id") 31 tagLibVersion = tag.MustNewKey("library_version") 32 tagAllKeys = []tag.Key{tagClientID, tagDatabase, tagInstance, tagLibVersion} 33) 34 35func recordStat(ctx context.Context, m *stats.Int64Measure, n int64) { 36 stats.Record(ctx, m.M(n)) 37} 38 39var ( 40 // OpenSessionCount is a measure of the number of sessions currently opened. 41 // It is EXPERIMENTAL and subject to change or removal without notice. 42 OpenSessionCount = stats.Int64( 43 statsPrefix+"open_session_count", 44 "Number of sessions currently opened", 45 stats.UnitDimensionless, 46 ) 47 48 // OpenSessionCountView is a view of the last value of OpenSessionCount. 49 // It is EXPERIMENTAL and subject to change or removal without notice. 50 OpenSessionCountView = &view.View{ 51 Measure: OpenSessionCount, 52 Aggregation: view.LastValue(), 53 TagKeys: tagAllKeys, 54 } 55 56 // MaxAllowedSessionsCount is a measure of the maximum number of sessions 57 // allowed. Configurable by the user. 58 MaxAllowedSessionsCount = stats.Int64( 59 statsPrefix+"max_allowed_sessions", 60 "The maximum number of sessions allowed. Configurable by the user.", 61 stats.UnitDimensionless, 62 ) 63 64 // MaxAllowedSessionsCountView is a view of the last value of 65 // MaxAllowedSessionsCount. 66 MaxAllowedSessionsCountView = &view.View{ 67 Measure: MaxAllowedSessionsCount, 68 Aggregation: view.LastValue(), 69 TagKeys: tagAllKeys, 70 } 71 72 // InUseSessionsCount is a measure of the number of sessions currently 73 // opened. 74 InUseSessionsCount = stats.Int64( 75 statsPrefix+"in_use_sessions", 76 "The number of sessions currently in use.", 77 stats.UnitDimensionless, 78 ) 79 80 // InUseSessionsCountView is a view of the last value of 81 // InUseSessionsCount. 82 InUseSessionsCountView = &view.View{ 83 Measure: InUseSessionsCount, 84 Aggregation: view.LastValue(), 85 TagKeys: tagAllKeys, 86 } 87 88 // MaxInUseSessionsCount is a measure of the maximum number of sessions 89 // in use during the last 10 minute interval. 90 MaxInUseSessionsCount = stats.Int64( 91 statsPrefix+"max_in_use_sessions", 92 "The maximum number of sessions in use during the last 10 minute interval.", 93 stats.UnitDimensionless, 94 ) 95 96 // MaxInUseSessionsCountView is a view of the last value of 97 // MaxInUseSessionsCount. 98 MaxInUseSessionsCountView = &view.View{ 99 Measure: MaxInUseSessionsCount, 100 Aggregation: view.LastValue(), 101 TagKeys: tagAllKeys, 102 } 103 104 // GetSessionTimeoutsCount is a measure of the number of get sessions 105 // timeouts due to pool exhaustion. 106 GetSessionTimeoutsCount = stats.Int64( 107 statsPrefix+"get_session_timeouts", 108 "The number of get sessions timeouts due to pool exhaustion.", 109 stats.UnitDimensionless, 110 ) 111 112 // GetSessionTimeoutsCountView is a view of the last value of 113 // GetSessionTimeoutsCount. 114 GetSessionTimeoutsCountView = &view.View{ 115 Measure: GetSessionTimeoutsCount, 116 Aggregation: view.Count(), 117 TagKeys: tagAllKeys, 118 } 119 120 // AcquiredSessionsCount is the number of sessions acquired from 121 // the session pool. 122 AcquiredSessionsCount = stats.Int64( 123 statsPrefix+"num_acquired_sessions", 124 "The number of sessions acquired from the session pool.", 125 stats.UnitDimensionless, 126 ) 127 128 // AcquiredSessionsCountView is a view of the last value of 129 // AcquiredSessionsCount. 130 AcquiredSessionsCountView = &view.View{ 131 Measure: AcquiredSessionsCount, 132 Aggregation: view.Count(), 133 TagKeys: tagAllKeys, 134 } 135 136 // ReleasedSessionsCount is the number of sessions released by the user 137 // and pool maintainer. 138 ReleasedSessionsCount = stats.Int64( 139 statsPrefix+"num_released_sessions", 140 "The number of sessions released by the user and pool maintainer.", 141 stats.UnitDimensionless, 142 ) 143 144 // ReleasedSessionsCountView is a view of the last value of 145 // ReleasedSessionsCount. 146 ReleasedSessionsCountView = &view.View{ 147 Measure: ReleasedSessionsCount, 148 Aggregation: view.Count(), 149 TagKeys: tagAllKeys, 150 } 151) 152 153// EnableStatViews enables all views of metrics relate to session management. 154func EnableStatViews() error { 155 return view.Register( 156 OpenSessionCountView, 157 MaxAllowedSessionsCountView, 158 InUseSessionsCountView, 159 MaxInUseSessionsCountView, 160 GetSessionTimeoutsCountView, 161 AcquiredSessionsCountView, 162 ReleasedSessionsCountView, 163 ) 164} 165