github.com/looshlee/beatles@v0.0.0-20220727174639-742810ab631c/pkg/maps/ctmap/metrics.go (about)

     1  // Copyright 2016-2018 Authors of Cilium
     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  
    15  package ctmap
    16  
    17  import (
    18  	"fmt"
    19  
    20  	"github.com/cilium/cilium/pkg/bpf"
    21  	"github.com/cilium/cilium/pkg/metrics"
    22  )
    23  
    24  type gcStats struct {
    25  	*bpf.DumpStats
    26  
    27  	// aliveEntries is the number of scanned entries that are still alive.
    28  	aliveEntries uint32
    29  
    30  	// deleted is the number of keys deleted
    31  	deleted uint32
    32  
    33  	// family is the address family
    34  	family gcFamily
    35  
    36  	// proto is the L4 protocol
    37  	proto gcProtocol
    38  
    39  	// dumpError records any error that occurred during the dump.
    40  	dumpError error
    41  }
    42  
    43  type gcFamily int
    44  
    45  const (
    46  	gcFamilyIPv4 = iota
    47  	gcFamilyIPv6
    48  )
    49  
    50  func (g gcFamily) String() string {
    51  	switch g {
    52  	case gcFamilyIPv4:
    53  		return "ipv4"
    54  	case gcFamilyIPv6:
    55  		return "ipv6"
    56  	default:
    57  		return "unknown"
    58  	}
    59  }
    60  
    61  type gcProtocol int
    62  
    63  const (
    64  	gcProtocolAny = iota
    65  	gcProtocolTCP
    66  )
    67  
    68  func (g gcProtocol) String() string {
    69  	switch g {
    70  	case gcProtocolAny:
    71  		return "non-TCP"
    72  	case gcProtocolTCP:
    73  		return "TCP"
    74  	default:
    75  		return fmt.Sprintf("unknown (%d)", int(g))
    76  	}
    77  }
    78  
    79  func statStartGc(m *Map) gcStats {
    80  	result := gcStats{
    81  		DumpStats: bpf.NewDumpStats(&m.Map),
    82  	}
    83  	if m.mapType.isIPv6() {
    84  		result.family = gcFamilyIPv6
    85  	} else {
    86  		result.family = gcFamilyIPv4
    87  	}
    88  	if m.mapType.isTCP() {
    89  		result.proto = gcProtocolTCP
    90  	} else {
    91  		result.proto = gcProtocolAny
    92  	}
    93  	return result
    94  }
    95  
    96  func (s *gcStats) finish() {
    97  	duration := s.Duration()
    98  	family := s.family.String()
    99  	switch s.family {
   100  	case gcFamilyIPv6:
   101  		metrics.DatapathErrors.With(labelIPv6CTDumpInterrupts).Add(float64(s.Interrupted))
   102  	case gcFamilyIPv4:
   103  		metrics.DatapathErrors.With(labelIPv4CTDumpInterrupts).Add(float64(s.Interrupted))
   104  	}
   105  	proto := s.proto.String()
   106  
   107  	var status string
   108  	if s.Completed {
   109  		status = "completed"
   110  		metrics.ConntrackGCSize.WithLabelValues(family, proto, metricsAlive).Set(float64(s.aliveEntries))
   111  		metrics.ConntrackGCSize.WithLabelValues(family, proto, metricsDeleted).Set(float64(s.deleted))
   112  	} else {
   113  		status = "uncompleted"
   114  		scopedLog := log.WithField("interrupted", s.Interrupted)
   115  		if s.dumpError != nil {
   116  			scopedLog = scopedLog.WithError(s.dumpError)
   117  		}
   118  		scopedLog.Warningf("Garbage collection on %s %s CT map failed to finish", family, proto)
   119  	}
   120  
   121  	metrics.ConntrackGCRuns.WithLabelValues(family, proto, status).Inc()
   122  	metrics.ConntrackGCDuration.WithLabelValues(family, proto, status).Observe(duration.Seconds())
   123  	metrics.ConntrackGCKeyFallbacks.WithLabelValues(family, proto).Add(float64(s.KeyFallback))
   124  }