github.com/galamsiva2020/kubernetes-heapster-monitoring@v0.0.0-20210823134957-3c1baa7c1e70/events/sinks/honeycomb/driver.go (about)

     1  // Copyright 2017 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 honeycomb
    16  
    17  import (
    18  	"net/url"
    19  	"sync"
    20  
    21  	"github.com/golang/glog"
    22  	kube_api "k8s.io/api/core/v1"
    23  	honeycomb_common "k8s.io/heapster/common/honeycomb"
    24  	event_core "k8s.io/heapster/events/core"
    25  )
    26  
    27  type honeycombSink struct {
    28  	client honeycomb_common.Client
    29  	sync.Mutex
    30  }
    31  
    32  type exportedData struct {
    33  	Namespace       string `json:"namespace"`
    34  	Kind            string `json:"kind"`
    35  	Name            string `json:"name"`
    36  	SubObject       string `json:"subobject"`
    37  	SourceComponent string `json:"source.component"`
    38  	SourceHost      string `json:"source.host"`
    39  	Count           int32  `json:"count"`
    40  	Type            string `json:"type"`
    41  	Reason          string `json:"reason"`
    42  	Message         string `json:"message"`
    43  }
    44  
    45  func getExportedData(e *kube_api.Event) *exportedData {
    46  	return &exportedData{
    47  		Namespace:       e.InvolvedObject.Namespace,
    48  		Kind:            e.InvolvedObject.Kind,
    49  		Name:            e.InvolvedObject.Name,
    50  		SubObject:       e.InvolvedObject.FieldPath,
    51  		SourceComponent: e.Source.Component,
    52  		SourceHost:      e.Source.Host,
    53  		Count:           e.Count,
    54  		Reason:          e.Reason,
    55  		Type:            e.Type,
    56  		Message:         e.Message,
    57  	}
    58  }
    59  
    60  func (sink *honeycombSink) ExportEvents(eventBatch *event_core.EventBatch) {
    61  	sink.Lock()
    62  	defer sink.Unlock()
    63  	exportedBatch := make(honeycomb_common.Batch, len(eventBatch.Events))
    64  	for i, event := range eventBatch.Events {
    65  		data := getExportedData(event)
    66  		exportedBatch[i] = &honeycomb_common.BatchPoint{
    67  			Data:      data,
    68  			Timestamp: event.LastTimestamp.UTC(),
    69  		}
    70  	}
    71  	err := sink.client.SendBatch(exportedBatch)
    72  	if err != nil {
    73  		glog.Warningf("Failed to send event: %v", err)
    74  		return
    75  	}
    76  }
    77  
    78  func (sink *honeycombSink) Stop() {}
    79  
    80  func (sink *honeycombSink) Name() string {
    81  	return "Honeycomb Sink"
    82  }
    83  
    84  func NewHoneycombSink(uri *url.URL) (event_core.EventSink, error) {
    85  	client, err := honeycomb_common.NewClient(uri)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	sink := &honeycombSink{
    90  		client: client,
    91  	}
    92  
    93  	return sink, nil
    94  
    95  }