github.com/mier85/go-sensor@v1.30.1-0.20220920111756-9bf41b3bc7e0/event.go (about)

     1  // (c) Copyright IBM Corp. 2021
     2  // (c) Copyright Instana Inc. 2017
     3  
     4  package instana
     5  
     6  import (
     7  	"time"
     8  )
     9  
    10  // EventData is the construct serialized for the host agent
    11  type EventData struct {
    12  	Title string `json:"title"`
    13  	Text  string `json:"text"`
    14  	// Duration in milliseconds
    15  	Duration int `json:"duration"`
    16  	// Severity with value of -1, 5, 10 : see type severity
    17  	Severity int    `json:"severity"`
    18  	Plugin   string `json:"plugin,omitempty"`
    19  	ID       string `json:"id,omitempty"`
    20  	Host     string `json:"host"`
    21  }
    22  
    23  type severity int
    24  
    25  //Severity values for events sent to the instana agent
    26  const (
    27  	SeverityChange   severity = -1
    28  	SeverityWarning  severity = 5
    29  	SeverityCritical severity = 10
    30  )
    31  
    32  // Defaults for the Event API
    33  const (
    34  	ServicePlugin = "com.instana.forge.connection.http.logical.LogicalWebApp"
    35  	ServiceHost   = ""
    36  )
    37  
    38  // SendDefaultServiceEvent sends a default event which already contains the service and host
    39  func SendDefaultServiceEvent(title string, text string, sev severity, duration time.Duration) {
    40  	var service string
    41  	if sensor != nil {
    42  		service = sensor.serviceName
    43  	}
    44  
    45  	// If the sensor is not yet initialized, there is no default service (as
    46  	// configured on the sensor) so we will send blank instead
    47  	SendServiceEvent(service, title, text, sev, duration)
    48  }
    49  
    50  // SendServiceEvent sends an event on a specific service
    51  func SendServiceEvent(service string, title string, text string, sev severity, duration time.Duration) {
    52  	sendEvent(&EventData{
    53  		Title:    title,
    54  		Text:     text,
    55  		Severity: int(sev),
    56  		Plugin:   ServicePlugin,
    57  		ID:       service,
    58  		Host:     ServiceHost,
    59  		Duration: int(duration / time.Millisecond),
    60  	})
    61  }
    62  
    63  // SendHostEvent sends an event on the current host
    64  func SendHostEvent(title string, text string, sev severity, duration time.Duration) {
    65  	sendEvent(&EventData{
    66  		Title:    title,
    67  		Text:     text,
    68  		Duration: int(duration / time.Millisecond),
    69  		Severity: int(sev),
    70  	})
    71  }
    72  
    73  func sendEvent(event *EventData) {
    74  	if sensor == nil {
    75  		// If the sensor hasn't initialized we do so here so that we properly
    76  		// discover where the host agent may be as it varies between a
    77  		// normal host, docker, kubernetes etc..
    78  		InitSensor(&Options{})
    79  	}
    80  
    81  	// we do fire & forget here, because the whole pid dance isn't necessary to send events
    82  	go sensor.Agent().SendEvent(event)
    83  }