github.com/google/cadvisor@v0.49.1/client/clientexample/main.go (about)

     1  // Copyright 2014 Google Inc. All Rights Reserved.
     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 main
    16  
    17  import (
    18  	"flag"
    19  
    20  	"github.com/google/cadvisor/client"
    21  	info "github.com/google/cadvisor/info/v1"
    22  
    23  	"k8s.io/klog/v2"
    24  )
    25  
    26  func staticClientExample() {
    27  	staticClient, err := client.NewClient("http://localhost:8080/")
    28  	if err != nil {
    29  		klog.Errorf("tried to make client and got error %v", err)
    30  		return
    31  	}
    32  	einfo, err := staticClient.EventStaticInfo("?oom_events=true")
    33  	if err != nil {
    34  		klog.Errorf("got error retrieving event info: %v", err)
    35  		return
    36  	}
    37  	for idx, ev := range einfo {
    38  		klog.Infof("static einfo %v: %v", idx, ev)
    39  	}
    40  }
    41  
    42  func streamingClientExample(url string) {
    43  	streamingClient, err := client.NewClient("http://localhost:8080/")
    44  	if err != nil {
    45  		klog.Errorf("tried to make client and got error %v", err)
    46  		return
    47  	}
    48  	einfo := make(chan *info.Event)
    49  	go func() {
    50  		err = streamingClient.EventStreamingInfo(url, einfo)
    51  		if err != nil {
    52  			klog.Errorf("got error retrieving event info: %v", err)
    53  			return
    54  		}
    55  	}()
    56  	for ev := range einfo {
    57  		klog.Infof("streaming einfo: %v\n", ev)
    58  	}
    59  }
    60  
    61  // demonstrates how to use event clients
    62  func main() {
    63  	klog.InitFlags(nil)
    64  	flag.Parse()
    65  	staticClientExample()
    66  	streamingClientExample("?creation_events=true&stream=true&oom_events=true&deletion_events=true")
    67  }