Skip to content

Commit

Permalink
Add hostname tag
Browse files Browse the repository at this point in the history
  • Loading branch information
AkhigbeEromo committed Dec 3, 2024
1 parent a8b67a6 commit 17c525b
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions exporter/sematextexporter/es.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"fmt"
"reflect"
"strings"

"os"
"github.com/olivere/elastic"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
Expand All @@ -15,7 +15,8 @@ import (

json "github.com/json-iterator/go"
)

// artificialDocType designates a syntenic doc type for ES documents
const artificialDocType = "_doc"
type group struct {
client *elastic.Client
token string
Expand Down Expand Up @@ -58,6 +59,7 @@ func NewClient(config *Config, logger *logrus.Logger, writer FlatWriter) (Client
}, nil
}


func (c *client) Bulk(body interface{}, config *Config) error {
// Lookup for client by endpoint
if grp, ok := c.clients[config.LogsEndpoint]; ok {
Expand All @@ -67,9 +69,18 @@ func (c *client) Bulk(body interface{}, config *Config) error {
if reflect.TypeOf(body).Kind() == reflect.Slice {
v := reflect.ValueOf(body)
for i := 0; i < v.Len(); i++ {
doc := v.Index(i).Interface()

// Ensure the document is a map to add the hostname tag
if docMap, ok := doc.(map[string]interface{}); ok {
docMap["os.host"] = getHostname()

}

req := elastic.NewBulkIndexRequest().
Index(grp.token).
Doc(v.Index(i).Interface())
Index(grp.token).
Type(artificialDocType).
Doc(doc)
bulkRequest.Add(req)
}
}
Expand Down Expand Up @@ -130,4 +141,11 @@ func Formatl(payload string, status string) string {
s = fmt.Sprintf("%s...", s[:i])
}
return fmt.Sprintf("%s %s", strings.TrimSpace(payload), s)
}
}
func getHostname() (string) {
hostname, err := os.Hostname()
if err != nil {
return "None"
}
return hostname
}

1 comment on commit 17c525b

@otisg
Copy link
Member

@otisg otisg commented on 17c525b Dec 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@boratanrikulu do you know if this is how we get the hostname in STA, too? Asking because we want hostname (resolution) to be the same across all data collectors - STA, Exporter, etc. to cross-linking via os.host in SC works.

Please sign in to comment.