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

Auto-inference of destination.service.resource #1898

Merged
merged 8 commits into from
Jul 7, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@ private void setResourceValue(String newValue) {
resource.append(newValue);
}

public boolean isResourceSetByUser() {
return resourceSetByUser;
}

public StringBuilder getResource() {
return resource;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@

import co.elastic.apm.agent.configuration.CoreConfiguration;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.impl.context.Db;
import co.elastic.apm.agent.impl.context.Destination;
import co.elastic.apm.agent.impl.context.Message;
import co.elastic.apm.agent.impl.context.SpanContext;
import co.elastic.apm.agent.impl.context.Url;
import co.elastic.apm.agent.impl.context.web.ResultUtil;
import co.elastic.apm.agent.objectpool.Recyclable;
import org.slf4j.Logger;
Expand Down Expand Up @@ -248,6 +252,35 @@ public void beforeEnd(long epochMicros) {
withOutcome(outcome);
}

// auto-infer context.destination.service.resource as per spec:
// https://github.com/elastic/apm/blob/master/specs/agents/tracing-spans-destination.md#contextdestinationserviceresource
Destination.Service service = getContext().getDestination().getService();
StringBuilder serviceResource = service.getResource();
if (isExit() && serviceResource.length() == 0 && !service.isResourceSetByUser()) {
String resourceType = (subtype != null) ? subtype : type;
Db db = context.getDb();
Message message = context.getMessage();
Url internalUrl = context.getHttp().getInternalUrl();
if (db.hasContent()) {
serviceResource.append(resourceType);
if (db.getInstance() != null) {
serviceResource.append('/').append(db.getInstance());
}
} else if (message.hasContent()) {
serviceResource.append(resourceType);
if (message.getQueueName() != null) {
serviceResource.append('/').append(message.getQueueName());
}
} else if (internalUrl.hasContent()) {
serviceResource.append(internalUrl.getHostname());
if (internalUrl.getPort() > 0) {
serviceResource.append(':').append(internalUrl.getPort());
}
} else {
serviceResource.append(resourceType);
}
}

if (transaction != null) {
transaction.incrementTimer(type, subtype, getSelfDuration());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
* 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 co.elastic.apm.agent.impl.context;

import co.elastic.apm.agent.MockTracer;
import co.elastic.apm.agent.impl.transaction.Span;
import co.elastic.apm.agent.impl.transaction.Transaction;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import specs.TestJsonSpec;

import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.Objects;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static org.assertj.core.api.Assertions.assertThat;

public class ServiceResourceTest {

private static Transaction root;

@BeforeAll
static void startRootTransaction() {
root = Objects.requireNonNull(MockTracer.createRealTracer().startRootTransaction(null));
}

@AfterAll
static void endTransaction() {
root.end();
}

@ParameterizedTest
@MethodSource("getTestCases")
void testServiceResourceInference(JsonNode testCase) {
Span span = createSpan(testCase);
span.end();
String expected = getTextValueOrNull(testCase, "inferred_resource");
if (expected == null) {
expected = "";
}
String actual = span.getContext().getDestination().getService().getResource().toString();
assertThat(actual)
.withFailMessage(String.format("%s, expected: %s, actual: `%s`", getTextValueOrNull(testCase, "failure_message"), expected, actual))
.isEqualTo(expected);
}

private Span createSpan(JsonNode testCase) {
Span span = root.createSpan();
JsonNode spanJson = testCase.get("span");
span.withType(spanJson.get("type").textValue());
JsonNode subtypeJsonNode = spanJson.get("subtype");
if (subtypeJsonNode != null) {
span.withSubtype(subtypeJsonNode.textValue());
}
if (spanJson.get("exit").asBoolean(false)) {
span.asExit();
}
JsonNode contextJson = spanJson.get("context");
if (contextJson != null) {
SpanContext context = span.getContext();
JsonNode dbJson = contextJson.get("db");
if (dbJson != null) {
Db db = context.getDb();
db.withType(getTextValueOrNull(dbJson, "type"));
db.withInstance(getTextValueOrNull(dbJson, "instance"));
}
JsonNode messageJson = contextJson.get("message");
if (messageJson != null) {
Message message = context.getMessage();
message.withBody(getTextValueOrNull(messageJson, "body"));
JsonNode queueJson = messageJson.get("queue");
if (queueJson != null) {
message.withQueue(queueJson.get("name").asText());
}
}
JsonNode httpJson = contextJson.get("http");
if (httpJson != null) {
JsonNode urlJson = httpJson.get("url");
if (urlJson != null) {
Url url = context.getHttp().getInternalUrl();
url.withHostname(getTextValueOrNull(urlJson, "host"));
JsonNode portJson = urlJson.get("port");
if (portJson != null) {
url.withPort(portJson.intValue());
}
}
}
}
return span;
}

@Nullable
private String getTextValueOrNull(JsonNode dbJson, String type) {
JsonNode jsonNode = dbJson.get(type);
if (jsonNode == null || jsonNode.isNull()) {
return null;
}
return jsonNode.asText();
}

private static Stream<JsonNode> getTestCases() {
Iterator<JsonNode> json = TestJsonSpec.getJson("service_resource_inference.json").iterator();
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(json, Spliterator.ORDERED), false);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
[
felixbarny marked this conversation as resolved.
Show resolved Hide resolved
{
"span": {
"exit": "true",
"type": "custom",
"subtype": "test-subtype"
},
"inferred_resource": "test-subtype",
"failure_message": "In the absence of specific context fields, subtype should used"
},
{
"span": {
"exit": "true",
"type": "custom"
},
"inferred_resource": "custom",
"failure_message": "In the absence of specific context fields and absence of subtype, the type should be used"
},
{
"span": {
"exit": "false",
"type": "custom",
"subtype": "test-subtype"
},
"inferred_resource": null,
"failure_message": "The output for non-exit spans should be `null`"
},
{
"span": {
"exit": "true",
"type": "db",
"subtype": "mysql",
"context": {
"db": {
"instance": "myInstance"
}
}
},
"inferred_resource": "mysql/myInstance",
"failure_message": "If `context.db.instance` exists, the output should be: `${subtype}/${context.db.instance}`"
},
{
"span": {
"exit": "true",
"type": "db",
"subtype": "mysql",
"context": {
"db": {
"type": "sql"
}
}
},
"inferred_resource": "mysql",
"failure_message": "If `context.db` exists without `context.db.instance`, the subtype should be used"
},
{
"span": {
"exit": "true",
"type": "db",
"context": {
"db": {
"instance": "myInstance"
}
}
},
"inferred_resource": "db/myInstance",
"failure_message": "If `context.db.instance` exists and subtype is `null`, the output should be: `${type}/${context.db.instance}`"
},
{
"span": {
"exit": "true",
"type": "db",
"subtype": "elasticsearch",
"context": {
"db": {
"type": "elasticsearch"
},
"http": {
"url": {
"host": "my-cluster.com",
"port": 9200
}
}
}
},
"inferred_resource": "elasticsearch",
"failure_message": "If `context.db` exists without `context.db.instance`, the subtype should be used, even if `context.http` exists"
},
Copy link
Member

Choose a reason for hiding this comment

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

We have the following priorities when inferring resource:

  • use context.db first if available (1)
  • then try to use context.messaging if available (2)
  • then try to use context.http if available (3)

While the relative ordering or (1) vs (3) is tested with the Elasticsearch case, we don't have test that ensures that (2) comes before (3), even if having a mix of messaging and http is unlikely we'd better cover this corner case. Also, maybe having a case with all of them should default to using the db part.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, I'll add a test for messaging combined with http, but I don't think combining db and messaging makes sense

{
"span": {
"exit": "true",
"type": "http",
"context": {
"http": {
"url": {
"host": "my-cluster.com",
"port": 9200
}
}
}
},
"inferred_resource": "my-cluster.com:9200",
"failure_message": "If `context.http.url` exists, output should be `${context.http.url.host}:${context.http.url.port}"
},
{
"span": {
"exit": "true",
"type": "http",
"context": {
"http": {
"url": {
"host": "my-cluster.com",
"port": -1
}
}
}
},
"inferred_resource": "my-cluster.com",
"failure_message": "Negative `context.http.url.port` should be omitted from output"
},
{
"span": {
"exit": "true",
"type": "http",
"context": {
"http": {
"url": {
"host": "my-cluster.com"
}
}
}
},
"inferred_resource": "my-cluster.com",
"failure_message": "If `context.http.url.port` does not exist, output should be `${context.http.url.host}`"
},
{
"span": {
"exit": "true",
"type": "messaging",
"context": {
"message": {
"body": "Text message",
"queue": {
"name": "myQueue"
}
}
}
},
"inferred_resource": "messaging/myQueue",
"failure_message": "If `context.message` exists, and subtype is `null`, output should be `${type}:${context.message.queue.name}"
},
{
"span": {
"exit": "true",
"type": "messaging",
"subtype": "kafka",
"context": {
"message": {
"body": "Text message",
"queue": {
"name": "myQueue"
}
}
}
},
"inferred_resource": "kafka/myQueue",
"failure_message": "If `context.message` exists, output should be `${subtype}:${context.message.queue.name}"
},
{
"span": {
"exit": "true",
"type": "messaging",
"subtype": "kafka",
"context": {
"message": {
"body": "Text message"
}
}
},
"inferred_resource": "kafka",
"failure_message": "If `context.message` exists without `context.message.queue.name`, output should be `${subtype}`"
}
]
4 changes: 2 additions & 2 deletions docs/public-api.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ span.end(System.currentTimeMillis() * 1000);
[float]
[[api-span-start-span-with-type]]
==== `Span startSpan(String type, String subtype, String action)`
Start and return a new span with a type, a subtype and an action, as a child of this transaction.
Start and return a new span with a type, a subtype and an action, as a child of this span.

The type, subtype and action strings are used to group similar spans together, with different resolution.
For instance, all DB spans are given the type `db`; all spans of MySQL queries are given the subtype `mysql` and all spans
Expand Down Expand Up @@ -787,7 +787,7 @@ See <<api-span-activate, `span.activate()`>> on how to achieve that.
[float]
[[api-span-start-span]]
==== `Span startSpan()`
Start and return a new custom span with no type as a child of this transaction.
Start and return a new custom span with no type as a child of this span.

It is important to call <<api-span-end>> when the span has ended.
A best practice is to use the span in a try-catch-finally block.
Expand Down