Search in sources :

Example 1 with Span

use of co.elastic.apm.impl.transaction.Span in project apm-agent-java by elastic.

the class ElasticApmTracer method startSpan.

@Nonnull
@Override
public Span startSpan() {
    Transaction transaction = currentTransaction();
    final Span span;
    // even when setting active=false mid-transaction
    if (isNoop(transaction)) {
        span = noopSpan;
    } else {
        span = createRealSpan(transaction);
    }
    currentSpan.set(span);
    return span;
}
Also used : Transaction(co.elastic.apm.impl.transaction.Transaction) Span(co.elastic.apm.impl.transaction.Span) Nonnull(javax.annotation.Nonnull)

Example 2 with Span

use of co.elastic.apm.impl.transaction.Span in project apm-agent-java by elastic.

the class AbstractReporterBenchmark method fillTransaction.

private void fillTransaction(Transaction t) {
    t.setName("GET /api/types");
    t.setType("request");
    t.withResult("success");
    Context context = t.getContext();
    Request request = context.getRequest();
    request.withHttpVersion("1.1");
    request.withMethod("POST");
    request.withRawBody("Hello World");
    request.getUrl().withProtocol("https").appendToFull("https://www.example.com/p/a/t/h?query=string#hash").withHostname("www.example.com").withPort("8080").withPathname("/p/a/t/h").withSearch("?query=string");
    request.getSocket().withEncrypted(true).withRemoteAddress("12.53.12.1");
    request.addHeader("user-agent", "Mozilla Chrome Edge");
    request.addHeader("content-type", "text/html");
    request.addHeader("cookie", "c1=v1; c2=v2");
    request.addHeader("some-other-header", "foo");
    request.addHeader("array", "foo, bar, baz");
    request.getCookies().put("c1", "v1");
    request.getCookies().put("c2", "v2");
    context.getResponse().withStatusCode(200).withFinished(true).withHeadersSent(true).addHeader("content-type", "application/json");
    context.getUser().withId("99").withUsername("foo").withEmail("foo@example.com");
    context.getTags().put("organization_uuid", "9f0e9d64-c185-4d21-a6f4-4673ed561ec8");
    context.getCustom().put("my_key", 1);
    context.getCustom().put("some_other_value", "foo bar");
    context.getCustom().put("and_objects", STRINGS);
    Span span = new Span().withName("SELECT FROM product_types").withType("db.postgresql.query");
    span.getContext().getDb().withInstance("customers").withStatement("SELECT * FROM product_types WHERE user_id=?").withType("sql").withUser("readonly_user");
    t.getSpans().add(span);
    t.getSpans().add(new Span().withName("GET /api/types").withType("request"));
    t.getSpans().add(new Span().withName("GET /api/types").withType("request"));
    t.getSpans().add(new Span().withName("GET /api/types").withType("request"));
}
Also used : Context(co.elastic.apm.impl.context.Context) Request(co.elastic.apm.impl.context.Request) Span(co.elastic.apm.impl.transaction.Span)

Example 3 with Span

use of co.elastic.apm.impl.transaction.Span in project apm-agent-java by elastic.

the class ApmJdbcEventListener method onBeforeAnyExecute.

@Override
public void onBeforeAnyExecute(StatementInformation statementInformation) {
    if (isNoop(elasticApmTracer.currentTransaction())) {
        return;
    }
    Span span = elasticApmTracer.startSpan();
    span.setName(getMethod(statementInformation.getStatementQuery()));
    try {
        String dbVendor = getDbVendor(statementInformation.getConnectionInformation().getConnection().getMetaData().getURL());
        span.setType("db." + dbVendor + ".sql");
        span.getContext().getDb().withUser(statementInformation.getConnectionInformation().getConnection().getMetaData().getUserName()).withStatement(statementInformation.getStatementQuery()).withType("sql");
    } catch (SQLException e) {
        logger.warn("Ignored exception", e);
    }
}
Also used : SQLException(java.sql.SQLException) Span(co.elastic.apm.impl.transaction.Span)

Example 4 with Span

use of co.elastic.apm.impl.transaction.Span in project apm-agent-java by elastic.

the class ApmJdbcEventListenerTest method testJdbcSpan.

@Test
void testJdbcSpan() throws SQLException {
    PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM ELASTIC_APM WHERE FOO=$1");
    preparedStatement.setInt(1, 1);
    ResultSet resultSet = preparedStatement.executeQuery();
    assertThat(resultSet.next()).isTrue();
    assertThat(resultSet.getInt("foo")).isEqualTo(1);
    assertThat(resultSet.getString("BAR")).isEqualTo("APM");
    assertThat(transaction.getSpans()).hasSize(1);
    Span jdbcSpan = transaction.getSpans().get(0);
    assertThat(jdbcSpan.getName()).isEqualTo("SELECT");
    assertThat(jdbcSpan.getType()).isEqualToIgnoringCase("db.h2.sql");
    Db db = jdbcSpan.getContext().getDb();
    assertThat(db.getStatement()).isEqualTo("SELECT * FROM ELASTIC_APM WHERE FOO=$1");
    assertThat(db.getUser()).isEqualToIgnoringCase("user");
    assertThat(db.getType()).isEqualToIgnoringCase("sql");
}
Also used : ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Span(co.elastic.apm.impl.transaction.Span) Db(co.elastic.apm.impl.transaction.Db) Test(org.junit.jupiter.api.Test)

Example 5 with Span

use of co.elastic.apm.impl.transaction.Span in project apm-agent-java by elastic.

the class ElasticApmTracer method createRealSpan.

private Span createRealSpan(Transaction transaction) {
    Span span;
    span = spanPool.createInstance();
    final boolean dropped;
    if (isTransactionSpanLimitReached(transaction)) {
        dropped = true;
        transaction.getSpanCount().getDropped().increment();
    } else {
        dropped = false;
    }
    span.start(this, transaction, currentSpan(), System.nanoTime(), dropped);
    return span;
}
Also used : Span(co.elastic.apm.impl.transaction.Span)

Aggregations

Span (co.elastic.apm.impl.transaction.Span)7 Context (co.elastic.apm.impl.context.Context)2 Request (co.elastic.apm.impl.context.Request)2 Transaction (co.elastic.apm.impl.transaction.Transaction)2 ElasticApmTracer (co.elastic.apm.impl.ElasticApmTracer)1 Db (co.elastic.apm.impl.transaction.Db)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Nonnull (javax.annotation.Nonnull)1 Test (org.junit.jupiter.api.Test)1