Skip to content

Commit

Permalink
fix: log.Println log.Printf usage
Browse files Browse the repository at this point in the history
  • Loading branch information
katepangLiu committed Dec 21, 2023
1 parent 788e566 commit 0a8221f
Show file tree
Hide file tree
Showing 15 changed files with 44 additions and 41 deletions.
2 changes: 1 addition & 1 deletion inputs/greenplum/greenplum.go
Expand Up @@ -48,7 +48,7 @@ func (ins *Greenplum) Gather(slist *types.SampleList) {
stateValue = strings.TrimSpace(stateValue)
gpstate := strings.Fields(stateValue)
if len(gpstate)%7 != 0 {
log.Println("E! failed to parse gpstate -m output: %v", gpstate)
log.Printf("E! failed to parse gpstate -m output: %v\n", gpstate)
return
}
line := len(gpstate) / 7
Expand Down
12 changes: 7 additions & 5 deletions inputs/mtail/internal/metrics/store_test.go
Expand Up @@ -36,7 +36,7 @@ func TestDuplicateMetric(t *testing.T) {
}

_ = s.Add(NewMetric("foo", "prog", Counter, Float))
log.Println("Store: %v", s)
log.Println("Store: ", s)
expectedMetrics++
if len(s.Metrics["foo"]) != expectedMetrics {
t.Fatalf("should add metric of a different type: %v", s)
Expand All @@ -63,10 +63,12 @@ func TestDuplicateMetric(t *testing.T) {
}
}

/* A program can add a metric with the same name and
of different type.
Prometheus behavior in this case is undefined.
@see https://github.com/google/mtail/issues/130
/*
A program can add a metric with the same name and
of different type.
Prometheus behavior in this case is undefined.
@see https://github.com/google/mtail/issues/130
*/
func TestAddMetricDifferentType(t *testing.T) {
expected := 2
Expand Down
2 changes: 1 addition & 1 deletion inputs/mtail/internal/testutil/expvar.go
Expand Up @@ -15,7 +15,7 @@ import (
func TestGetExpvar(tb testing.TB, name string) expvar.Var {
tb.Helper()
v := expvar.Get(name)
log.Println("Var %q is %v", name, v)
log.Printf("Var %s is %v\n", name, v)
return v
}

Expand Down
9 changes: 5 additions & 4 deletions inputs/smart/instances.go
Expand Up @@ -4,7 +4,6 @@ import (
"bufio"
"context"
"errors"
"flashcat.cloud/categraf/pkg/stringx"
"fmt"
"log"
"os"
Expand All @@ -17,6 +16,8 @@ import (
"syscall"
"time"

"flashcat.cloud/categraf/pkg/stringx"

"flashcat.cloud/categraf/config"
"flashcat.cloud/categraf/types"
)
Expand Down Expand Up @@ -343,7 +344,7 @@ func gatherIntelNVMeDisk(slist *types.SampleList, timeout config.Duration, usesu

_, er := exitStatus(e)
if er != nil {
log.Printf("E! failed to run command '%s %s': %w - %s", nvme, strings.Join(args, " "), e, outStr)
log.Printf("E! failed to run command '%s %s': %v - %s\n", nvme, strings.Join(args, " "), e, outStr)
return
}

Expand Down Expand Up @@ -412,7 +413,7 @@ func (m *Instance) gatherDisk(slist *types.SampleList, device string, wg *sync.W
// Ignore all exit statuses except if it is a command line parse error
exitStatus, er := exitStatus(e)
if er != nil {
log.Printf("E! failed to run command '%s %s': %w - %s", m.PathSmartctl, strings.Join(args, " "), e, outStr)
log.Printf("E! failed to run command '%s %s': %v - %s\n", m.PathSmartctl, strings.Join(args, " "), e, outStr)
return
}

Expand Down Expand Up @@ -547,7 +548,7 @@ func (m *Instance) gatherDisk(slist *types.SampleList, device string, wg *sync.W
}

if err := parse(fields, deviceFields, metric, matches[2]); err != nil {
log.Printf("E!error parsing %s: %q: %w", attr.Name, matches[2], err)
log.Printf("E!error parsing %s: %q: %v\n", attr.Name, matches[2], err)
continue
}
// if the field is classified as an attribute, only add it
Expand Down
6 changes: 3 additions & 3 deletions inputs/vsphere/client.go
Expand Up @@ -200,7 +200,7 @@ func NewClient(ctx context.Context, vSphereURL *url.URL, vs *Instance) (*Client,
log.Println("D! vCenter says max_query_metrics should be ", n)
}
if n < vs.MaxQueryMetrics {
log.Println("W! Configured max_query_metrics is %d, but server limits it to %d. Reducing.", vs.MaxQueryMetrics, n)
log.Printf("W! Configured max_query_metrics is %d, but server limits it to %d. Reducing.\n", vs.MaxQueryMetrics, n)
vs.MaxQueryMetrics = n
}
return client, nil
Expand Down Expand Up @@ -253,7 +253,7 @@ func (c *Client) GetMaxQueryMetrics(ctx context.Context) (int, error) {
v, err := strconv.Atoi(s)
if err == nil {
if config.Config.DebugMode {
log.Println("D! vCenter maxQueryMetrics is defined: %d", v)
log.Printf("D! vCenter maxQueryMetrics is defined: %d\n", v)
}
if v == -1 {
// Whatever the server says, we never ask for more metrics than this.
Expand All @@ -272,7 +272,7 @@ func (c *Client) GetMaxQueryMetrics(ctx context.Context) (int, error) {
ver := c.Client.Client.ServiceContent.About.Version
parts := strings.Split(ver, ".")
if len(parts) < 2 {
log.Println("W! vCenter returned an invalid version string: %s. Using default query size=64", ver)
log.Printf("W! vCenter returned an invalid version string: %s. Using default query size=64\n", ver)
return 64, nil
}
log.Println("I! vCenter version is: ", ver)
Expand Down
14 changes: 7 additions & 7 deletions inputs/vsphere/endpoint.go
Expand Up @@ -256,7 +256,7 @@ func anythingEnabled(ex []string) bool {
func newFilterOrPanic(include []string, exclude []string) filter.Filter {
f, err := filter.NewIncludeExcludeFilter(include, exclude)
if err != nil {
log.Printf("E! Include/exclude filters are invalid: ", err)
log.Println("E! Include/exclude filters are invalid: ", err)
return nil
}
return f
Expand Down Expand Up @@ -571,9 +571,9 @@ func (e *Endpoint) complexMetadataSelect(ctx context.Context, res *resourceKind,
}

instInfoMux := sync.Mutex{}
te,err := NewThrottledExecutor(e.Parent.DiscoverConcurrency)
if err !=nil {
log.Println("E! NewThrottledExecutor",err.Error())
te, err := NewThrottledExecutor(e.Parent.DiscoverConcurrency)
if err != nil {
log.Println("E! NewThrottledExecutor", err.Error())
return
}
for _, obj := range sampledObjects {
Expand Down Expand Up @@ -953,9 +953,9 @@ func submitChunkJob(ctx context.Context, te *ThrottledExecutor, job queryJob, pq
}

func (e *Endpoint) chunkify(ctx context.Context, res *resourceKind, now time.Time, latest time.Time, job queryJob) {
te, err:= NewThrottledExecutor(e.Parent.CollectConcurrency)
if err!= nil {
log.Println("E! NewThrottledExecutor",err.Error())
te, err := NewThrottledExecutor(e.Parent.CollectConcurrency)
if err != nil {
log.Println("E! NewThrottledExecutor", err.Error())
return
}
maxMetrics := e.Parent.MaxQueryMetrics
Expand Down
2 changes: 1 addition & 1 deletion inputs/vsphere/vsphere.go
Expand Up @@ -214,7 +214,7 @@ func (ins *Instance) Gather(slist *types.SampleList) {
}
if err != nil {
// acc.AddError(err)
log.Printf("E! fail to gather\n", err)
log.Println("E! fail to gather: ", err)
}

}
2 changes: 1 addition & 1 deletion logs/auditor/auditor.go
Expand Up @@ -187,7 +187,7 @@ func (a *RegistryAuditor) recoverRegistry() map[string]*RegistryEntry {
mr, err := ioutil.ReadFile(a.registryPath)
if err != nil {
if os.IsNotExist(err) {
log.Println("I! Could not find state file at %q, will start with default offsets", a.registryPath)
log.Printf("I! Could not find state file at %q, will start with default offsets\n", a.registryPath)
} else {
log.Println("E!", err)
}
Expand Down
2 changes: 1 addition & 1 deletion logs/input/file/tailer_nix.go
Expand Up @@ -53,7 +53,7 @@ func (t *Tailer) read() (int, error) {
if err != nil && err != io.EOF {
// an unexpected error occurred, stop the tailor
t.file.Source.Status.Error(err)
return 0, fmt.Errorf("E! Unexpected error occurred while reading file: ", err)
return 0, fmt.Errorf("E! Unexpected error occurred while reading file: %v", err)
}
if n == 0 {
return 0, nil
Expand Down
10 changes: 5 additions & 5 deletions logs/input/kubernetes/launcher.go
Expand Up @@ -71,15 +71,15 @@ func IsAvailable() (bool, *retry.Retrier) {
log.Println("Kubernetes launcher is available")
return true, nil
}
log.Printf("Kubernetes launcher is not available: %v", retrier.LastError())
log.Println("Kubernetes launcher is not available: ", retrier.LastError())
return false, retrier
}

// NewLauncher returns a new launcher.
func NewLauncher(sources *logsconfig.LogSources, services *service.Services, collectAll bool) *Launcher {
kubeutil, err := kubelet.GetKubeUtil()
if err != nil {
log.Printf("KubeUtil not available, failed to create launcher", err)
log.Println("KubeUtil not available, failed to create launcher: ", err)
return nil
}
launcher := &Launcher{
Expand Down Expand Up @@ -153,7 +153,7 @@ func (l *Launcher) scheduleServiceForRetry(svc *service.Service) {
func (l *Launcher) delayRetry(ops *retryOps) {
delay := ops.backoff.NextBackOff()
if delay == backoff.Stop {
log.Println("Unable to add source for container %v", ops.service.GetEntityID())
log.Println("Unable to add source for container ", ops.service.GetEntityID())
delete(l.pendingRetries, ops.service.GetEntityID())
return
}
Expand All @@ -177,7 +177,7 @@ func (l *Launcher) addSource(svc *service.Service) {
if err != nil {
if errors.IsRetriable(err) {
// Attempt to reschedule the source later
log.Println("Failed to fetch pod info for container %v, will retry: %v", svc.Identifier, err)
log.Printf("Failed to fetch pod info for container %v, will retry: %v\n", svc.Identifier, err)
l.scheduleServiceForRetry(svc)
return
}
Expand All @@ -192,7 +192,7 @@ func (l *Launcher) addSource(svc *service.Service) {
source, err := l.getSource(pod, container)
if err != nil {
if err != errCollectAllDisabled {
log.Println("Invalid configuration for pod %v, container %v: %v", pod.Metadata.Name, container.Name, err)
log.Printf("Invalid configuration for pod %v, container %v: %v\n", pod.Metadata.Name, container.Name, err)
}
return
}
Expand Down
6 changes: 3 additions & 3 deletions logs/util/docker/containers.go
Expand Up @@ -235,7 +235,7 @@ func (d *DockerUtil) parseContainerNetworkAddresses(cID string, ports []types.Po
if isExposed(port) {
IP := net.ParseIP(port.IP)
if IP == nil {
log.Println("Unable to parse IP: %v for container: %s", port.IP, container)
log.Printf("Unable to parse IP: %v for container: %s\n", port.IP, container)
continue
}
addrList = append(addrList, containers.NetworkAddress{
Expand All @@ -253,12 +253,12 @@ func (d *DockerUtil) parseContainerNetworkAddresses(cID string, ports []types.Po
// Retieve IPs from network settings for the cached ports
for _, network := range netSettings.Networks {
if network.IPAddress == "" {
log.Println("No IP found for container %s in network %s", container, network.NetworkID)
log.Printf("No IP found for container %s in network %s\n", container, network.NetworkID)
continue
}
IP := net.ParseIP(network.IPAddress)
if IP == nil {
log.Println("Unable to parse IP: %v for container: %s", network.IPAddress, container)
log.Printf("Unable to parse IP: %v for container: %s\n", network.IPAddress, container)
continue
}
for _, addr := range tempAddrList {
Expand Down
2 changes: 1 addition & 1 deletion logs/util/docker/event_pull.go
Expand Up @@ -118,7 +118,7 @@ func (d *DockerUtil) LatestContainerEvents(ctx context.Context, since time.Time)
case msg := <-msgChan:
event, err := d.processContainerEvent(ctx, msg)
if err != nil {
log.Println("W! error parsing docker message: %s", err)
log.Println("W! error parsing docker message: ", err)
continue
} else if event == nil {
continue
Expand Down
4 changes: 2 additions & 2 deletions logs/util/docker/event_stream.go
Expand Up @@ -111,7 +111,7 @@ CONNECT: // Outer loop handles re-connecting in case the docker daemon closes th
log.Println("D! Got EOF, re-connecting")
} else {
// Else, let's wait 10 seconds and try reconnecting
log.Println("W! Got error from docker, waiting for 10 seconds: %s", err)
log.Println("W! Got error from docker, waiting for 10 seconds: ", err)
time.Sleep(10 * time.Second)
}
cancelFunc()
Expand All @@ -120,7 +120,7 @@ CONNECT: // Outer loop handles re-connecting in case the docker daemon closes th
latestTimestamp = msg.Time
event, err := d.processContainerEvent(ctx, msg)
if err != nil {
log.Println("D! Skipping event: %s", err)
log.Println("D! Skipping event: ", err)
continue
}
if event == nil {
Expand Down
8 changes: 4 additions & 4 deletions logs/util/docker/network.go
Expand Up @@ -52,14 +52,14 @@ func findDockerNetworks(containerID string, pid int, container types.Container)
// Check the known network modes that require specific handling.
// Other network modes will look at the docker NetworkSettings.
if netMode == containers.HostNetworkMode {
log.Println("Container %s is in network host mode, its network metrics are for the whole host", containerID)
log.Printf("Container %s is in network host mode, its network metrics are for the whole host\n", containerID)
return []dockerNetwork{hostNetwork}
} else if netMode == containers.NoneNetworkMode {
log.Println("Container %s is in network mode 'none', we will collect metrics for the whole host", containerID)
log.Printf("Container %s is in network mode 'none', we will collect metrics for the whole host\n", containerID)
return []dockerNetwork{hostNetwork}
} else if strings.HasPrefix(netMode, "container:") {
netContainerID := strings.TrimPrefix(netMode, "container:")
log.Println("Container %s uses the network namespace of container:%s", containerID, netContainerID)
log.Printf("Container %s uses the network namespace of container:%s\n", containerID, netContainerID)
return []dockerNetwork{{routingContainerID: netContainerID}}
}

Expand All @@ -75,7 +75,7 @@ func findDockerNetworks(containerID string, pid int, container types.Container)
interfaces := make(map[string]uint64)
for netName, netConf := range netSettings.Networks {
if netName == "host" {
log.Println("Container %s is in network host mode, its network metrics are for the whole host", containerID)
log.Printf("Container %s is in network host mode, its network metrics are for the whole host\n", containerID)
return []dockerNetwork{hostNetwork}
}

Expand Down
4 changes: 2 additions & 2 deletions logs/util/docker/storage.go
Expand Up @@ -89,12 +89,12 @@ func parseStorageStatsFromInfo(info types.Info) ([]*StorageStats, error) {
valueString := entry[1]
fields := strings.Fields(key)
if len(fields) != 3 || strings.ToLower(fields[1]) != "space" {
log.Println("ignoring invalid storage stat: %s", key)
log.Println("ignoring invalid storage stat: ", key)
continue
}
valueInt, err := parseDiskQuantity(valueString)
if err != nil {
log.Println("ignoring invalid value %s for stat %s: %s", valueString, key, err)
log.Printf("ignoring invalid value %s for stat %s: %s\n", valueString, key, err)
continue
}
storageType := strings.ToLower(fields[0])
Expand Down

0 comments on commit 0a8221f

Please sign in to comment.