Skip to content

Commit

Permalink
PMM-12468 Lint, private structs.
Browse files Browse the repository at this point in the history
  • Loading branch information
JiriCtvrtka committed Jan 26, 2025
1 parent afdf1e9 commit 1519713
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type Aggregator struct {
timeEnd time.Time
d time.Duration
t *time.Timer
mongostats *ExtendedStats
mongostats *extendedStats

// state
m sync.Mutex // Lock() to protect internal consistency of the service
Expand Down
59 changes: 37 additions & 22 deletions agent/agents/mongodb/internal/profiler/aggregator/extended.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright (C) 2023 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package aggregator

import (
Expand All @@ -14,31 +28,34 @@ import (
"github.com/percona/pmm/agent/agents/mongodb/internal/profiler/collector"
)

// Code below contains necessary code to extend stats from Percona Toolkit.
const planSummaryCollScan = "COLLSCAN"

type ExtendedStats struct {
type extendedStats struct {
// dependencies
fingerprinter stats.Fingerprinter

// internal
queryInfoAndCounters map[stats.GroupKey]*ExtendedQueryInfoAndCounters
queryInfoAndCounters map[stats.GroupKey]*extendedQueryInfoAndCounters
sync.RWMutex
}

type ExtendedQueryInfoAndCounters struct {
type extendedQueryInfoAndCounters struct {
stats.QueryInfoAndCounters
PlanSummary string
CollScanCount int
CollScanSum int64
}

type ExtendedQueryStats struct {
type extendedQueryStats struct {
stats.QueryStats
PlanSummary string
CollScanCount int
CollScanSum int64
}

type extendedQueries []extendedQueryInfoAndCounters

type totalCounters struct {
Count int
Scanned float64
Expand All @@ -47,10 +64,8 @@ type totalCounters struct {
Bytes float64
}

type ExtendedQueries []ExtendedQueryInfoAndCounters

func NewExtendedStats(fingerprinter stats.Fingerprinter) *ExtendedStats {
s := &ExtendedStats{
func NewExtendedStats(fingerprinter stats.Fingerprinter) *extendedStats {
s := &extendedStats{
fingerprinter: fingerprinter,
}

Expand All @@ -59,15 +74,15 @@ func NewExtendedStats(fingerprinter stats.Fingerprinter) *ExtendedStats {
}

// Reset clears the collection of statistics
func (s *ExtendedStats) Reset() {
func (s *extendedStats) Reset() {
s.Lock()
defer s.Unlock()

s.queryInfoAndCounters = make(map[stats.GroupKey]*ExtendedQueryInfoAndCounters)
s.queryInfoAndCounters = make(map[stats.GroupKey]*extendedQueryInfoAndCounters)
}

// Queries returns all collected statistics
func (s *ExtendedStats) Queries() ExtendedQueries {
func (s *extendedStats) Queries() extendedQueries {
s.Lock()
defer s.Unlock()

Expand All @@ -77,20 +92,20 @@ func (s *ExtendedStats) Queries() ExtendedQueries {
}
sort.Sort(keys)

queries := []ExtendedQueryInfoAndCounters{}
queries := []extendedQueryInfoAndCounters{}
for _, key := range keys {
queries = append(queries, *s.queryInfoAndCounters[key])
}
return queries
}

// Add adds collector.ExtendedSystemProfile to the collection of statistics
func (s *ExtendedStats) Add(doc collector.ExtendedSystemProfile) error {
func (s *extendedStats) Add(doc collector.ExtendedSystemProfile) error {
fp, err := s.fingerprinter.Fingerprint(doc.SystemProfile)
if err != nil {
return err // TODO &stats.StatsFingerprintError{err}
}
var qiac *ExtendedQueryInfoAndCounters
var qiac *extendedQueryInfoAndCounters
var ok bool

key := stats.GroupKey{
Expand All @@ -104,7 +119,7 @@ func (s *ExtendedStats) Add(doc collector.ExtendedSystemProfile) error {
if err != nil {
return err
}
qiac = &ExtendedQueryInfoAndCounters{
qiac = &extendedQueryInfoAndCounters{
QueryInfoAndCounters: stats.QueryInfoAndCounters{
ID: fmt.Sprintf("%x", md5.Sum([]byte(key.String()))),
Operation: fp.Operation,
Expand Down Expand Up @@ -145,24 +160,24 @@ func (s *ExtendedStats) Add(doc collector.ExtendedSystemProfile) error {
return nil
}

func (s *ExtendedStats) getQueryInfoAndCounters(key stats.GroupKey) (*ExtendedQueryInfoAndCounters, bool) {
func (s *extendedStats) getQueryInfoAndCounters(key stats.GroupKey) (*extendedQueryInfoAndCounters, bool) {
s.RLock()
defer s.RUnlock()

v, ok := s.queryInfoAndCounters[key]
return v, ok
}

func (s *ExtendedStats) setQueryInfoAndCounters(key stats.GroupKey, value *ExtendedQueryInfoAndCounters) {
func (s *extendedStats) setQueryInfoAndCounters(key stats.GroupKey, value *extendedQueryInfoAndCounters) {
s.Lock()
defer s.Unlock()

s.queryInfoAndCounters[key] = value
}

// CalcQueriesStats calculates QueryStats for given uptime
func (q ExtendedQueries) CalcQueriesStats(uptime int64) []ExtendedQueryStats {
qs := []ExtendedQueryStats{}
func (q extendedQueries) CalcQueriesStats(uptime int64) []extendedQueryStats {
qs := []extendedQueryStats{}
tc := calcTotalCounters(q)

for _, query := range q {
Expand All @@ -173,8 +188,8 @@ func (q ExtendedQueries) CalcQueriesStats(uptime int64) []ExtendedQueryStats {
return qs
}

func countersToStats(query ExtendedQueryInfoAndCounters, uptime int64, tc totalCounters) ExtendedQueryStats {
queryStats := ExtendedQueryStats{
func countersToStats(query extendedQueryInfoAndCounters, uptime int64, tc totalCounters) extendedQueryStats {
queryStats := extendedQueryStats{
QueryStats: stats.QueryStats{
Count: query.Count,
ID: query.ID,
Expand Down Expand Up @@ -213,7 +228,7 @@ func countersToStats(query ExtendedQueryInfoAndCounters, uptime int64, tc totalC
return queryStats
}

func calcTotalCounters(queries []ExtendedQueryInfoAndCounters) totalCounters {
func calcTotalCounters(queries []extendedQueryInfoAndCounters) totalCounters {
tc := totalCounters{}

for _, query := range queries {
Expand Down
15 changes: 15 additions & 0 deletions agent/agents/mongodb/internal/profiler/collector/extended.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,22 @@
// Copyright (C) 2023 Percona LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import "github.com/percona/percona-toolkit/src/go/mongolib/proto"

// ExtendedSystemProfile is extended SystemProfile from Percona Toolkit.
type ExtendedSystemProfile struct {
proto.SystemProfile `bson:",inline"`
PlanSummary string `bson:"planSummary"`
Expand Down

0 comments on commit 1519713

Please sign in to comment.