I have a struct as follows:
type PromStatsServer struct {
// To hold the total number of IGMP messages dropped by ACL check
aclCounters *prometheus.CounterVec
// To hold the total number of invalid IGMP messages
invalidIGMPCounters *prometheus.CounterVec
// To hold the number of successful JOIN request from IGMP clients
successfulJoinCounters *prometheus.CounterVec
// To hold the number of unsuccessful JOIN request from IGMP clients
unsuccessfulJoinCounters *prometheus.CounterVec
// To hold the number of JOIN request received from IGMP clients
totalJoinCounters *prometheus.CounterVec
}
And, I have a const declaration as follows:
const (
// Total Number of IGMP messages dropped by ACL check
TotalNumIGMPMessagesDroppedByACLCheck = "DroppedIGMPMessagesByACL"
// Total Number of invalid IGMP Messages
TotalNumInvalidIGMPMessages = "NumInvalidIGMPMessages"
// Total number of successful JOIN requests from IGMP clients.
TotalNumSuccessfulJoinRequests = "NumSuccessfulJoinRequests"
// Total number of unsuccessful JOIN request from IGMP clients.
TotalNumUnsuccessfulJoinRequests = "NumUnsuccessfulJoinRequests"
// Total number of JOINs received from IGMP clients
TotalNumJoinsReceived = "NumJoinsReceived"
)
This is the function that I increment the values of those counter metrics.
// IncForPacket counts the number of times the couterName happens,
// Each call increments the associated counterName's value by 1.
func (ps *PromStatsServer) IncForPacket(vlan string, counterName PacketCounter) {
switch counterName {
case TotalNumIGMPMessagesDroppedByACLCheck:
if ps.aclCounters != nil {
ps.aclCounters.WithLabelValues(vlan, string(counterName)).Inc()
}
case TotalNumInvalidIGMPMessages:
if ps.invalidIGMPCounters != nil {
ps.invalidIGMPCounters.WithLabelValues(vlan, string(counterName)).Inc()
}
case TotalNumSuccessfulJoinRequests:
if ps.successfulJoinCounters != nil {
ps.successfulJoinCounters.WithLabelValues(vlan, string(counterName)).Inc()
}
case TotalNumUnsuccessfulJoinRequests:
if ps.unsuccessfulJoinCounters != nil {
ps.unsuccessfulJoinCounters.WithLabelValues(vlan, string(counterName)).Inc()
}
case TotalNumJoinsReceived:
if ps.totalJoinCounters != nil {
ps.totalJoinCounters.WithLabelValues(vlan, string(counterName)).Inc()
}
}
}
This is the interface.
IncForPacketCounter(vlan string, counterName string)
What kind of strategy would work for me so that I can erase those many if
checks inside IncForPacket()
implementation. Becasue it looks to me that those if
checks are not considered as clean code. Does polymorphism work here with the help of interface? I'm calling it from another file and I don't think making those constants public is a good idea.
Any recommendation?
The code segments I've written seemed to me not clean enough. Hence, I'm looking for better strategies to make the code clean.