Skip to content

Commit

Permalink
Map updated HTTP server semantic conventions (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevejgordon authored May 16, 2024
1 parent 0f3c385 commit 2c1119f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 15 deletions.
36 changes: 27 additions & 9 deletions input/otlp/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ const (
attributeServerAddress = "server.address"
attributeServerPort = "server.port"
attributeUrlFull = "url.full"
attributeUrlScheme = "url.scheme"
attributeUrlPath = "url.path"
attributeUrlQuery = "url.query"
attributeUserAgentOriginal = "user_agent.original"
attributeDbElasticsearchClusterName = "db.elasticsearch.cluster.name"
attributeStackTrace = "code.stacktrace" // semconv 1.24 or later
Expand Down Expand Up @@ -255,6 +258,8 @@ func TranslateTransaction(
http modelpb.HTTP
httpRequest modelpb.HTTPRequest
httpResponse modelpb.HTTPResponse
urlPath string
urlQuery string
)

var isHTTP, isRPC, isMessaging bool
Expand Down Expand Up @@ -304,7 +309,7 @@ func TranslateTransaction(
event.Source = modelpb.SourceFromVTPool()
}
event.Source.Port = uint32(v.Int())
case semconv.AttributeNetHostPort:
case semconv.AttributeNetHostPort, attributeServerPort:
netHostPort = int(v.Int())
case semconv.AttributeRPCGRPCStatusCode:
isRPC = true
Expand All @@ -324,10 +329,16 @@ func TranslateTransaction(
case semconv.AttributeHTTPURL, semconv.AttributeHTTPTarget, "http.path":
isHTTP = true
httpURL = stringval
case attributeUrlPath:
isHTTP = true
urlPath = stringval
case attributeUrlQuery:
isHTTP = true
urlQuery = stringval
case semconv.AttributeHTTPHost:
isHTTP = true
httpHost = stringval
case semconv.AttributeHTTPScheme:
case semconv.AttributeHTTPScheme, attributeUrlScheme:
isHTTP = true
httpScheme = stringval
case semconv.AttributeHTTPStatusCode, attributeHttpResponseStatusCode:
Expand Down Expand Up @@ -376,7 +387,7 @@ func TranslateTransaction(
event.Source = modelpb.SourceFromVTPool()
}
event.Source.Domain = stringval
case semconv.AttributeNetHostName:
case semconv.AttributeNetHostName, attributeServerAddress:
netHostName = stringval
case attributeNetworkConnectionType:
if event.Network == nil {
Expand Down Expand Up @@ -450,11 +461,6 @@ func TranslateTransaction(
case semconv.AttributeRPCService:
case semconv.AttributeRPCMethod:

// URL
case attributeUrlFull:
isHTTP = true
httpURL = stringval

// miscellaneous
case "type":
event.Transaction.Type = stringval
Expand Down Expand Up @@ -515,7 +521,6 @@ func TranslateTransaction(
}
}

// Build the modelpb.URL from http{URL,Host,Scheme}.
httpHost := httpHost
if httpHost == "" {
httpHost = httpServerName
Expand All @@ -529,8 +534,20 @@ func TranslateTransaction(
httpHost = net.JoinHostPort(httpHost, strconv.Itoa(netHostPort))
}
}

// Build a relative url from the UrlPath and UrlQuery.
httpURL := httpURL
if httpURL == "" && urlPath != "" {
httpURL = urlPath
if urlQuery != "" {
httpURL += "?" + urlQuery
}
}

// Build the modelpb.URL from http{URL,Host,Scheme}.
event.Url = modelpb.ParseURL(httpURL, httpHost, httpScheme)
}

if isMessaging {
// Overwrite existing event.Transaction.Message
event.Transaction.Message = nil
Expand All @@ -555,6 +572,7 @@ func TranslateTransaction(
if event.Transaction.Result == "" {
event.Transaction.Result = spanStatusResult(spanStatus)
}

if name := library.Name(); name != "" {
if event.Service == nil {
event.Service = modelpb.ServiceFromVTPool()
Expand Down
27 changes: 21 additions & 6 deletions input/otlp/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,17 +253,32 @@ func TestHTTPTransactionURL(t *testing.T) {
"http.target": "/foo",
})
})
t.Run("url.full", func(t *testing.T) {
t.Run("url_attributes", func(t *testing.T) {
test(t, &modelpb.URL{
Scheme: "https",
Original: "https://testing.invalid:80/foo?bar",
Full: "https://testing.invalid:80/foo?bar",
Original: "/foo",
Full: "https://test.domain/foo",
Path: "/foo",
Domain: "test.domain",
}, map[string]interface{}{
"url.scheme": "https",
"server.address": "test.domain",
"url.path": "/foo",
})
})
t.Run("url_attributes_with_query", func(t *testing.T) {
test(t, &modelpb.URL{
Scheme: "https",
Original: "/foo?bar",
Full: "https://test.domain/foo?bar",
Path: "/foo",
Query: "bar",
Domain: "testing.invalid",
Port: 80,
Domain: "test.domain",
}, map[string]interface{}{
"url.full": "https://testing.invalid:80/foo?bar",
"url.scheme": "https",
"server.address": "test.domain",
"url.path": "/foo",
"url.query": "bar",
})
})
}
Expand Down

0 comments on commit 2c1119f

Please sign in to comment.