github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/mem_metrics.go (about) 1 // Copyright 2016 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package sql 12 13 import ( 14 "time" 15 16 "github.com/cockroachdb/cockroach/pkg/util/metric" 17 ) 18 19 // MemoryMetrics contains pointers to the metrics object 20 // for one of the SQL endpoints: 21 // - "client" for connections received via pgwire. 22 // - "admin" for connections received via the admin RPC. 23 // - "internal" for activities related to leases, schema changes, etc. 24 type MemoryMetrics struct { 25 MaxBytesHist *metric.Histogram 26 CurBytesCount *metric.Gauge 27 TxnMaxBytesHist *metric.Histogram 28 TxnCurBytesCount *metric.Gauge 29 SessionMaxBytesHist *metric.Histogram 30 SessionCurBytesCount *metric.Gauge 31 } 32 33 // MetricStruct implements the metrics.Struct interface. 34 func (MemoryMetrics) MetricStruct() {} 35 36 var _ metric.Struct = MemoryMetrics{} 37 38 // TODO(knz): Until #10014 is addressed, the UI graphs don't have a 39 // log scale on the Y axis and the histograms are thus displayed using 40 // a manual log scale: we store the logarithm in the value in the DB 41 // and plot that logarithm in the UI. 42 // 43 // We could, but do not, store the full value in the DB and compute 44 // the log in the UI, because the current histogram implementation 45 // does not deal well with large maxima (#10015). 46 // 47 // Since the DB stores an integer, we scale the values by 1000 so that 48 // a modicum of precision is restored when exponentiating the value. 49 // 50 51 // log10int64times1000 = log10(math.MaxInt64) * 1000, rounded up somewhat 52 const log10int64times1000 = 19 * 1000 53 54 // MakeMemMetrics instantiates the metric objects for an SQL endpoint. 55 func MakeMemMetrics(endpoint string, histogramWindow time.Duration) MemoryMetrics { 56 prefix := "sql.mem." + endpoint 57 MetaMemMaxBytes := metric.Metadata{ 58 Name: prefix + ".max", 59 Help: "Memory usage per sql statement for " + endpoint, 60 Measurement: "Memory", 61 Unit: metric.Unit_BYTES, 62 } 63 MetaMemCurBytes := metric.Metadata{ 64 Name: prefix + ".current", 65 Help: "Current sql statement memory usage for " + endpoint, 66 Measurement: "Memory", 67 Unit: metric.Unit_BYTES, 68 } 69 MetaMemMaxTxnBytes := metric.Metadata{ 70 Name: prefix + ".txn.max", 71 Help: "Memory usage per sql transaction for " + endpoint, 72 Measurement: "Memory", 73 Unit: metric.Unit_BYTES, 74 } 75 MetaMemTxnCurBytes := metric.Metadata{ 76 Name: prefix + ".txn.current", 77 Help: "Current sql transaction memory usage for " + endpoint, 78 Measurement: "Memory", 79 Unit: metric.Unit_BYTES, 80 } 81 MetaMemMaxSessionBytes := metric.Metadata{ 82 Name: prefix + ".session.max", 83 Help: "Memory usage per sql session for " + endpoint, 84 Measurement: "Memory", 85 Unit: metric.Unit_BYTES, 86 } 87 MetaMemSessionCurBytes := metric.Metadata{ 88 Name: prefix + ".session.current", 89 Help: "Current sql session memory usage for " + endpoint, 90 Measurement: "Memory", 91 Unit: metric.Unit_BYTES, 92 } 93 return MemoryMetrics{ 94 MaxBytesHist: metric.NewHistogram(MetaMemMaxBytes, histogramWindow, log10int64times1000, 3), 95 CurBytesCount: metric.NewGauge(MetaMemCurBytes), 96 TxnMaxBytesHist: metric.NewHistogram(MetaMemMaxTxnBytes, histogramWindow, log10int64times1000, 3), 97 TxnCurBytesCount: metric.NewGauge(MetaMemTxnCurBytes), 98 SessionMaxBytesHist: metric.NewHistogram(MetaMemMaxSessionBytes, histogramWindow, log10int64times1000, 3), 99 SessionCurBytesCount: metric.NewGauge(MetaMemSessionCurBytes), 100 } 101 }