Manual instrumentation
You can create your custom spans, metrics, and logs, via the OpenTelemetry SDK APIs, which you can find in the OpenTelemetry object provided by the agent via its getOpenTelemetry()
method. Alternatively, for common operations, you might be able to take advantage of the convenience agent extensions to create telemetry in a less verbose way.
After completing the setup process, the agent will have configured an OpenTelemetry object for you, which is available via its getOpenTelemetry()
method. Here's an example of how to create telemetry with it.
fun myMethod() {
val agent: ElasticApmAgent
// Span example
val tracer = agent.getOpenTelemetry().getTracer("my-tracer-scope")
val span = tracer.spanBuilder("spanName").startSpan()
// ...
span.end()
// Metric example
val counter = agent.getOpenTelemetry().meterBuilder("meterScope").build().counterBuilder("myCounter").build()
counter.add(1)
// Logs example
val logger = agent.getOpenTelemetry().logsBridge["logScope"]
logger.logRecordBuilder().setBody("Log body").emit()
}
For more details on creating signals using the OpenTelemetry APIs, refer to the following pages:
For common use cases, in regards to spans and logs creation, the agent provides a couple of Kotlin extension methods to allow you to create telemetry in a less verbose way.
The convenience methods make use of the same OpenTelemetry APIs internally to create telemetry. So they are not the only way to create the following signals, they are only making them more straightforward to create.
fun myMethod() {
val agent: ElasticApmAgent
agent.span("spanName") {
// The span body.
}
}
- The span name and its body are the only mandatory parameters from this method. However, there are other optional parameters (such as one to set custom
attributes
) that you can provide if needed. Take a look at the method definition to find out more.
fun myMethod() {
val agent: ElasticApmAgent
agent.log("My log body")
}
- The log record body is the only mandatory parameter from this method. However, there are other optional parameters (such as one to set custom
attributes
), that you can provide if needed. Take a look at the method definition to find out more.