Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

processor/otel: extend support for OpenTelemetry semantic conventions #4711

Merged
merged 5 commits into from
Feb 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/head.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ https://github.com/elastic/apm-server/compare/7.11\...master[View commits]
[float]
==== Breaking Changes
* Leading 0s are no longer removed from trace/span ids if they are created by Jaeger {pull}4671[4671]
* Jaeger spans will now have a type of "app" where they previously were "custom" {pull}4711[4711]
* Jaeger spans may now have a (more accurate) outcome of "unknown" where they previously were "success" {pull}4711[4711]

[float]
==== Bug fixes
Expand Down
9 changes: 6 additions & 3 deletions model/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,20 @@ type URL struct {
Fragment *string
}

func ParseURL(original, hostname string) *URL {
func ParseURL(original, defaultHostname, defaultScheme string) *URL {
original = truncate(original)
url, err := url.Parse(original)
if err != nil {
return &URL{Original: &original}
}
if url.Scheme == "" {
url.Scheme = "http"
url.Scheme = defaultScheme
if url.Scheme == "" {
url.Scheme = "http"
}
}
if url.Host == "" {
url.Host = hostname
url.Host = defaultHostname
}
full := truncate(url.String())
out := &URL{
Expand Down
6 changes: 3 additions & 3 deletions model/error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,7 @@ func TestErrorTransformPage(t *testing.T) {
Error: Error{
ID: &id,
Page: &Page{
URL: ParseURL(urlExample, ""),
URL: ParseURL(urlExample, "", ""),
Referer: nil,
},
},
Expand All @@ -572,9 +572,9 @@ func TestErrorTransformPage(t *testing.T) {
Error: Error{
ID: &id,
Timestamp: time.Now(),
URL: ParseURL("https://localhost:8200/", ""),
URL: ParseURL("https://localhost:8200/", "", ""),
Page: &Page{
URL: ParseURL(urlExample, ""),
URL: ParseURL(urlExample, "", ""),
Referer: nil,
},
},
Expand Down
2 changes: 1 addition & 1 deletion model/modeldecoder/rumv3/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func mapToMetricsetModel(from *metricset, metadata *model.Metadata, reqTime time

func mapToPageModel(from contextPage, out *model.Page) {
if from.URL.IsSet() {
out.URL = model.ParseURL(from.URL.Val, "")
out.URL = model.ParseURL(from.URL.Val, "", "")
}
if from.Referer.IsSet() {
referer := from.Referer.Val
Expand Down
2 changes: 1 addition & 1 deletion model/modeldecoder/v2/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ func mapToMetricsetModel(from *metricset, metadata *model.Metadata, reqTime time

func mapToPageModel(from contextPage, out *model.Page) {
if from.URL.IsSet() {
out.URL = model.ParseURL(from.URL.Val, "")
out.URL = model.ParseURL(from.URL.Val, "", "")
}
if from.Referer.IsSet() {
referer := from.Referer.Val
Expand Down
6 changes: 3 additions & 3 deletions model/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ func TestTransactionTransformPage(t *testing.T) {
Type: "tx",
Duration: 65.98,
Page: &Page{
URL: ParseURL(urlExample, ""),
URL: ParseURL(urlExample, "", ""),
Referer: nil,
},
},
Expand All @@ -259,9 +259,9 @@ func TestTransactionTransformPage(t *testing.T) {
Type: "tx",
Timestamp: time.Now(),
Duration: 65.98,
URL: ParseURL("https://localhost:8200/", ""),
URL: ParseURL("https://localhost:8200/", "", ""),
Page: &Page{
URL: ParseURL(urlExample, ""),
URL: ParseURL(urlExample, "", ""),
Referer: nil,
},
},
Expand Down
121 changes: 121 additions & 0 deletions processor/otel/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package otel

import (
"fmt"

"github.com/elastic/apm-server/model"
)

type transactionBuilder struct {
*model.Transaction
httpURL string
httpHost string
httpScheme string
}

func (tx *transactionBuilder) setFramework(name, version string) {
if name == "" {
return
}
tx.Metadata.Service.Framework.Name = name
tx.Metadata.Service.Framework.Version = version
}

func (tx *transactionBuilder) setHTTPMethod(method string) {
tx.ensureHTTPRequest()
tx.HTTP.Request.Method = method
}

func (tx *transactionBuilder) setHTTPScheme(scheme string) {
tx.ensureHTTP()
tx.httpScheme = scheme
}

func (tx *transactionBuilder) setHTTPURL(httpURL string) {
tx.ensureHTTP()
tx.httpURL = httpURL
}

func (tx *transactionBuilder) setHTTPHost(hostport string) {
tx.ensureHTTP()
tx.httpHost = hostport
}

func (tx transactionBuilder) setHTTPVersion(version string) {
tx.ensureHTTP()
tx.HTTP.Version = &version
}

func (tx transactionBuilder) setHTTPRemoteAddr(remoteAddr string) {
tx.ensureHTTPRequest()
if tx.HTTP.Request.Socket == nil {
tx.HTTP.Request.Socket = &model.Socket{}
}
tx.HTTP.Request.Socket.RemoteAddress = &remoteAddr
}

func (tx *transactionBuilder) setHTTPStatusCode(statusCode int) {
tx.ensureHTTP()
if tx.HTTP.Response == nil {
tx.HTTP.Response = &model.Resp{}
}
tx.HTTP.Response.MinimalResp.StatusCode = &statusCode
if tx.Outcome == outcomeUnknown {
if statusCode >= 500 {
tx.Outcome = outcomeFailure
} else {
tx.Outcome = outcomeSuccess
}
}
if tx.Result == "" {
tx.Result = httpStatusCodeResult(statusCode)
}
}

func (tx *transactionBuilder) ensureHTTPRequest() {
tx.ensureHTTP()
if tx.HTTP.Request == nil {
tx.HTTP.Request = &model.Req{}
}
}

func (tx *transactionBuilder) ensureHTTP() {
if tx.HTTP == nil {
tx.HTTP = &model.Http{}
}
}

var standardStatusCodeResults = [...]string{
"HTTP 1xx",
"HTTP 2xx",
"HTTP 3xx",
"HTTP 4xx",
"HTTP 5xx",
}

// httpStatusCodeResult returns the transaction result value to use for the
// given HTTP status code.
func httpStatusCodeResult(statusCode int) string {
switch i := statusCode / 100; i {
case 1, 2, 3, 4, 5:
return standardStatusCodeResults[i-1]
}
return fmt.Sprintf("HTTP %d", statusCode)
}
Loading