Skip to content

Commit

Permalink
Set host.name on ingested events. (elastic#2531)
Browse files Browse the repository at this point in the history
Use information from `system.name` when provided, otherwise derive data from `host.hostname`.`

fixes elastic#2502
  • Loading branch information
simitt committed Aug 7, 2019
1 parent ee24fea commit fc2895f
Show file tree
Hide file tree
Showing 20 changed files with 104 additions and 21 deletions.
7 changes: 7 additions & 0 deletions _meta/fields.common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,13 @@
The hostname of the host the event was recorded on.
overwrite: true

- name: name
type: keyword
description: >
Name of the host the event was recorded on.
It can contain same information as host.hostname or a name specified by the user.
overwrite: true

- name: ip
type: ip
description: >
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@
"architecture": "x64",
"hostname": "node-name",
"ip": "127.0.0.1",
"name": "prod.example",
"os": {
"platform": "darwin"
}
Expand Down Expand Up @@ -340,6 +341,7 @@
"architecture": "x64",
"hostname": "node-name",
"ip": "127.0.0.1",
"name": "prod.example",
"os": {
"platform": "darwin"
}
Expand Down Expand Up @@ -433,6 +435,7 @@
"architecture": "x64",
"hostname": "node-name",
"ip": "127.0.0.1",
"name": "prod.example",
"os": {
"platform": "darwin"
}
Expand Down Expand Up @@ -531,6 +534,7 @@
"architecture": "x64",
"hostname": "node-name",
"ip": "127.0.0.1",
"name": "prod.example",
"os": {
"platform": "darwin"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
},
"host": {
"hostname": "node-name",
"ip": "127.0.0.1"
"ip": "127.0.0.1",
"name": "node-name"
},
"kubernetes": {
"namespace": "namespace1",
Expand Down Expand Up @@ -143,7 +144,8 @@
},
"host": {
"hostname": "node-name",
"ip": "127.0.0.1"
"ip": "127.0.0.1",
"name": "node-name"
},
"kubernetes": {
"namespace": "namespace1",
Expand Down Expand Up @@ -227,7 +229,8 @@
},
"host": {
"hostname": "node-name",
"ip": "127.0.0.1"
"ip": "127.0.0.1",
"name": "node-name"
},
"kubernetes": {
"namespace": "namespace1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"architecture": "x64",
"hostname": "node-name",
"ip": "127.0.0.1",
"name": "node-name",
"os": {
"platform": "darwin"
}
Expand Down Expand Up @@ -126,6 +127,7 @@
"architecture": "x64",
"hostname": "node-name",
"ip": "127.0.0.1",
"name": "node-name",
"os": {
"platform": "darwin"
}
Expand Down Expand Up @@ -317,6 +319,7 @@
"architecture": "x64",
"hostname": "node-name",
"ip": "127.0.0.1",
"name": "node-name",
"os": {
"platform": "darwin"
}
Expand Down
4 changes: 4 additions & 0 deletions docs/data/elasticsearch/generated/errors.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@
"architecture": "x64",
"hostname": "node-name",
"ip": "192.0.0.1",
"name": "prod.example",
"os": {
"platform": "darwin"
}
Expand Down Expand Up @@ -314,6 +315,7 @@
"architecture": "x64",
"hostname": "node-name",
"ip": "192.0.0.1",
"name": "prod.example",
"os": {
"platform": "darwin"
}
Expand Down Expand Up @@ -390,6 +392,7 @@
"architecture": "x64",
"hostname": "node-name",
"ip": "192.0.0.1",
"name": "prod.example",
"os": {
"platform": "darwin"
}
Expand Down Expand Up @@ -471,6 +474,7 @@
"architecture": "x64",
"hostname": "node-name",
"ip": "192.0.0.1",
"name": "prod.example",
"os": {
"platform": "darwin"
}
Expand Down
3 changes: 3 additions & 0 deletions docs/data/elasticsearch/generated/transactions.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"architecture": "x64",
"hostname": "node-name",
"ip": "192.0.0.1",
"name": "node-name",
"os": {
"platform": "darwin"
}
Expand Down Expand Up @@ -100,6 +101,7 @@
"architecture": "x64",
"hostname": "node-name",
"ip": "192.0.0.1",
"name": "node-name",
"os": {
"platform": "darwin"
}
Expand Down Expand Up @@ -274,6 +276,7 @@
"architecture": "x64",
"hostname": "node-name",
"ip": "192.0.0.1",
"name": "node-name",
"os": {
"platform": "darwin"
}
Expand Down
10 changes: 10 additions & 0 deletions docs/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,16 @@ type: keyword
The hostname of the host the event was recorded on.
type: keyword
--
*`host.name`*::
+
--
Name of the host the event was recorded on. It can contain same information as host.hostname or a name specified by the user.
type: keyword
--
Expand Down
7 changes: 6 additions & 1 deletion docs/spec/system.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
"maxLength": 1024
},
"hostname": {
"description": "Hostname of the system the agent is running on.",
"description": "Hostname of the system the agent is running on. Will be ignored if kubernetes information is set.",
"type": ["string", "null"],
"maxLength": 1024
},
"name": {
"description": "Name of the system the agent is running on. Will be set to hostname or derived from kubernetes information if not provided.",
"type": ["string", "null"],
"maxLength": 1024
},
Expand Down
2 changes: 1 addition & 1 deletion include/fields.go

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion model/metadata/generated/schema/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ const ModelSchema = `{
"maxLength": 1024
},
"hostname": {
"description": "Hostname of the system the agent is running on.",
"description": "Hostname of the system the agent is running on. Will be ignored if kubernetes information is set.",
"type": ["string", "null"],
"maxLength": 1024
},
"name": {
"description": "Name of the system the agent is running on. Will be set to hostname or derived from kubernetes information if not provided.",
"type": ["string", "null"],
"maxLength": 1024
},
Expand Down
2 changes: 1 addition & 1 deletion model/metadata/metadata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func TestMetadata_Set(t *testing.T) {
output: common.MapStr{
"foo": "bar",
"agent": common.MapStr{"version": "1.0.0", "name": "elastic-node"},
"host": common.MapStr{"hostname": host},
"host": common.MapStr{"hostname": host, "name": host},
"process": common.MapStr{"pid": pid},
"service": common.MapStr{
"name": "myservice",
Expand Down
10 changes: 10 additions & 0 deletions model/metadata/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (

type System struct {
Hostname *string
Name *string
Architecture *string
Platform *string
IP *string
Expand All @@ -45,6 +46,7 @@ func DecodeSystem(input interface{}, err error) (*System, error) {
decoder := utility.ManualDecoder{}
system := System{
Hostname: decoder.StringPtr(raw, "hostname"),
Name: decoder.StringPtr(raw, "name"),
Platform: decoder.StringPtr(raw, "platform"),
Architecture: decoder.StringPtr(raw, "architecture"),
IP: decoder.StringPtr(raw, "ip"),
Expand All @@ -58,6 +60,13 @@ func DecodeSystem(input interface{}, err error) (*System, error) {
return &system, decoder.Err
}

func (s *System) name() *string {
if s != nil && s.Name != nil {
return s.Name
}
return s.hostname()
}

func (s *System) hostname() *string {
if s == nil {
return nil
Expand Down Expand Up @@ -88,6 +97,7 @@ func (s *System) fields() common.MapStr {
}
system := common.MapStr{}
utility.Set(system, "hostname", s.hostname())
utility.Set(system, "name", s.name())
utility.Set(system, "architecture", s.Architecture)
if s.Platform != nil {
utility.Set(system, "os", common.MapStr{"platform": s.Platform})
Expand Down
19 changes: 16 additions & 3 deletions model/metadata/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
func TestSystemTransform(t *testing.T) {

architecture := "x64"
hostname := "a.b.com"
hostname, name := "a.b.com", "foo"
platform := "darwin"
ip := "127.0.0.1"
empty := ""
Expand All @@ -55,12 +55,14 @@ func TestSystemTransform(t *testing.T) {
System: System{
Architecture: &architecture,
Hostname: &hostname,
Name: &name,
Platform: &platform,
IP: &ip,
},
Output: common.MapStr{
"architecture": architecture,
"hostname": hostname,
"name": name,
"ip": ip,
"os": common.MapStr{
"platform": platform,
Expand All @@ -75,6 +77,7 @@ func TestSystemTransform(t *testing.T) {
Output: common.MapStr{
"architecture": architecture,
"hostname": hostname,
"name": hostname,
},
},
// nodename and podname
Expand All @@ -88,17 +91,19 @@ func TestSystemTransform(t *testing.T) {
},
Output: common.MapStr{
"hostname": nodename,
"name": nodename,
},
},
// podname
{
System: System{
Hostname: &hostname,
Name: &name,
Kubernetes: &Kubernetes{
PodName: &podname,
},
},
Output: common.MapStr{},
Output: common.MapStr{"name": name},
},
// poduid
{
Expand Down Expand Up @@ -128,6 +133,7 @@ func TestSystemTransform(t *testing.T) {
},
Output: common.MapStr{
"hostname": hostname,
"name": hostname,
},
},
}
Expand All @@ -139,7 +145,7 @@ func TestSystemTransform(t *testing.T) {
}

func TestSystemDecode(t *testing.T) {
host, arch, platform, ip := "host", "amd", "osx", "127.0.0.1"
host, name, arch, platform, ip := "host", "custom hostname", "amd", "osx", "127.0.0.1"
inpErr := errors.New("some error")
for _, test := range []struct {
input interface{}
Expand All @@ -161,6 +167,13 @@ func TestSystemDecode(t *testing.T) {
err: nil,
s: &System{Hostname: &host, Architecture: &arch, Platform: &platform, IP: &ip},
},
{
input: map[string]interface{}{
"hostname": host, "name": name,
},
err: nil,
s: &System{Hostname: &host, Name: &name},
},
} {
sys, err := DecodeSystem(test.input, test.inputErr)
assert.Equal(t, test.s, sys)
Expand Down
16 changes: 10 additions & 6 deletions model/transaction/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,12 @@ func TestEventsTransformWithMetadata(t *testing.T) {
serviceName := "myservice"
metadataLabels := common.MapStr{"a": true}
service := metadata.Service{Name: &serviceName}
system := &metadata.System{
Hostname: &hostname,
Architecture: &architecture,
Platform: &platform,
system := func() *metadata.System {
return &metadata.System{
Hostname: &hostname,
Architecture: &architecture,
Platform: &platform,
}
}

txValid := Event{Timestamp: timestamp}
Expand All @@ -331,6 +333,7 @@ func TestEventsTransformWithMetadata(t *testing.T) {
"host": common.MapStr{
"architecture": architecture,
"hostname": hostname,
"name": hostname,
"os": common.MapStr{
"platform": platform,
},
Expand Down Expand Up @@ -370,6 +373,7 @@ func TestEventsTransformWithMetadata(t *testing.T) {
"host": common.MapStr{
"architecture": "darwin",
"hostname": "a.b.c",
"name": "jane",
"os": common.MapStr{
"platform": "x64",
},
Expand Down Expand Up @@ -441,7 +445,7 @@ func TestEventsTransformWithMetadata(t *testing.T) {

{
Metadata: metadata.NewMetadata(
&service, system,
&service, system(),
nil, nil, metadataLabels,
),
Event: &txValid,
Expand All @@ -450,7 +454,7 @@ func TestEventsTransformWithMetadata(t *testing.T) {
},
{
Metadata: metadata.NewMetadata(
&service, system,
&service, func() *metadata.System { s := system(); s.Name = &name; return s }(),
nil, nil, metadataLabels,
),
Event: &txWithContext,
Expand Down
Loading

0 comments on commit fc2895f

Please sign in to comment.