Skip to content

Commit ceeffdb

Browse files
committed
copy ECS fields where required
Elasticsearch aliases are leveraged for ECS compatibility wherever possible. Where that's not possible, values are written to both the original location and the one ECS dictates. Here: * copy context.tags to labels - object fields can't be aliased * cast & copy context.request.url.port to url.port - keyword -> int * copy context.request.url.protocol to url.scheme - trim the final :
1 parent 701050d commit ceeffdb

27 files changed

+168
-1
lines changed

docs/data/elasticsearch/error.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,18 @@
243243
]
244244
}
245245
},
246+
"labels": {
247+
"organization_uuid": "9f0e9d64-c185-4d21-a6f4-4673ed561ec8"
248+
},
246249
"processor": {
247250
"event": "error",
248251
"name": "error"
249252
},
250253
"transaction": {
251254
"id": "945254c5-67a5-417e-8a4e-aa29efcbfb79"
255+
},
256+
"url": {
257+
"port": 8080,
258+
"scheme": "https"
252259
}
253260
}

docs/data/elasticsearch/generated/errors.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,12 +247,19 @@
247247
]
248248
}
249249
},
250+
"labels": {
251+
"organization_uuid": "9f0e9d64-c185-4d21-a6f4-4673ed561ec8"
252+
},
250253
"processor": {
251254
"event": "error",
252255
"name": "error"
253256
},
254257
"timestamp": {
255258
"us": 1494342245999999
259+
},
260+
"url": {
261+
"port": 8080,
262+
"scheme": "https"
256263
}
257264
},
258265
{

docs/data/elasticsearch/generated/metricsets.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
"double_gauge": 3.141592653589793,
3131
"float_gauge": 9.16,
3232
"integer_gauge": 42767,
33+
"labels": {
34+
"code": "200",
35+
"some.other.code": "abc"
36+
},
3337
"long_gauge": 3147483648,
3438
"negative": {
3539
"d": {

docs/data/elasticsearch/generated/spans.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@
9393
"tag1": "value1"
9494
}
9595
},
96+
"labels": {
97+
"tag1": "value1"
98+
},
9699
"parent": {
97100
"id": "abcdefabcdef7890"
98101
},

docs/data/elasticsearch/generated/transactions.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@
176176
"username": "foo"
177177
}
178178
},
179+
"labels": {
180+
"organization_uuid": "9f0e9d64-c185-4d21-a6f4-4673ed561ec8"
181+
},
179182
"processor": {
180183
"event": "transaction",
181184
"name": "transaction"
@@ -198,6 +201,10 @@
198201
"started": 17
199202
},
200203
"type": "request"
204+
},
205+
"url": {
206+
"port": 8080,
207+
"scheme": "https"
201208
}
202209
},
203210
{

docs/data/elasticsearch/metric.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
"double_gauge": 3.141592653589793,
4444
"float_gauge": 9.16,
4545
"integer_gauge": 42767,
46+
"labels": {
47+
"code": "200",
48+
"some.other.code": "abc"
49+
},
4650
"long_gauge": 3147483648,
4751
"negative": {
4852
"d": {

docs/data/elasticsearch/span.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
"span_tag": "something"
2323
}
2424
},
25+
"labels": {
26+
"span_tag": "something"
27+
},
2528
"processor": {
2629
"event": "span",
2730
"name": "transaction"

docs/data/elasticsearch/transaction.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@
107107
"username": "foo"
108108
}
109109
},
110+
"labels": {
111+
"organization_uuid": "9f0e9d64-c185-4d21-a6f4-4673ed561ec8"
112+
},
110113
"processor": {
111114
"event": "transaction",
112115
"name": "transaction"
@@ -135,5 +138,9 @@
135138
}
136139
},
137140
"type": "request"
141+
},
142+
"url": {
143+
"port": 8080,
144+
"scheme": "https"
138145
}
139146
}

model/ecs.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Licensed to Elasticsearch B.V. under one or more contributor
2+
// license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright
4+
// ownership. Elasticsearch B.V. licenses this file to you under
5+
// the Apache License, Version 2.0 (the "License"); you may
6+
// not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package model
19+
20+
import (
21+
"strconv"
22+
"strings"
23+
24+
"github.com/elastic/apm-server/utility"
25+
"github.com/elastic/beats/libbeat/common"
26+
)
27+
28+
func CopyECS(fields common.MapStr) {
29+
// context.tags -> labels
30+
utility.Add(fields, "labels", fields["context"].(common.MapStr)["tags"])
31+
32+
// context.request.url.protocol -> url.scheme (minus trailing colon)
33+
if scheme, err := fields.GetValue("context.request.url.protocol"); err == nil {
34+
if schemeStr, ok := scheme.(string); ok {
35+
utility.MergeAdd(fields, "url", common.MapStr{"scheme": strings.TrimSuffix(schemeStr, ":")})
36+
}
37+
}
38+
39+
// context.request.url.port -> url.port (as int)
40+
if port, err := fields.GetValue("context.request.url.port"); err == nil {
41+
if portStr, ok := port.(string); ok {
42+
if portNum, err := strconv.Atoi(portStr); err == nil {
43+
utility.MergeAdd(fields, "url", common.MapStr{"port": portNum})
44+
}
45+
}
46+
}
47+
48+
// docker.container.labels -> container.labels
49+
if containerLabels, err := fields.GetValue("docker.container.labels"); err == nil {
50+
if containerLabelsMap, ok := containerLabels.(common.MapStr); ok {
51+
utility.MergeAdd(fields, "container", common.MapStr{"labels": containerLabelsMap})
52+
}
53+
}
54+
}

model/error/event.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ func (e *Event) Transform(tctx *transform.Context) []beat.Event {
198198
utility.AddId(fields, "parent", e.ParentId)
199199
utility.AddId(fields, "trace", e.TraceId)
200200

201+
m.CopyECS(fields)
202+
201203
if e.v2Event {
202204
if e.Timestamp.IsZero() {
203205
e.Timestamp = tctx.RequestTime

0 commit comments

Comments
 (0)