### Python
>> bash
# For Flask applications
pip install elastic-apm[flask]
# For Django applications
pip install elastic-apm[django]
# For FastAPI applications
pip install elastic-apm[starlette]
>> bash
# Download latest version from Maven Central
wget https://search.maven.org/remotecontent?filepath=co/elastic/apm/elastic-apm-agent/1.36.0/elastic-apm-agent-1.36.0.jar
Note: try download from the Kibana > integrations > APM > Elastic APM in Fleet > java
>> bash
# Using npm
npm install elastic-apm-node --save
>> python
from flask import Flask
from elasticapm.contrib.flask import ElasticAPM
app = Flask(__name__)
# Method 1: Direct configuration
app.config['ELASTIC_APM'] = {
'SERVICE_NAME': 'my-service-name',
'SERVER_URL': 'http://localhost:8200',
'ENVIRONMENT': 'development',
'CAPTURE_BODY': 'all',
'TRANSACTION_SAMPLE_RATE': 1.0,
'COLLECT_LOCAL_VARIABLES': 'all'
}
apm = ElasticAPM(app)
# Set environment variables:
# ELASTIC_APM_SERVICE_NAME=my-service-name
# ELASTIC_APM_SERVER_URL=http://localhost:8200
# ELASTIC_APM_ENVIRONMENT=development
apm = ElasticAPM(app)
>> python
# settings.py
INSTALLED_APPS = [
'elasticapm.contrib.django',
# ... other apps
]
ELASTIC_APM = {
'SERVICE_NAME': 'my-service-name',
'SERVER_URL': 'http://localhost:8200',
'ENVIRONMENT': 'development',
'DJANGO_TRANSACTION_NAME_FROM_ROUTE': True,
'CAPTURE_BODY': 'all'
}
MIDDLEWARE = [
'elasticapm.contrib.django.middleware.TracingMiddleware',
# ... other middleware
]
java -javaagent:/path/to/elastic-apm-agent-1.36.0.jar \
-Delastic.apm.service_name=my-service-name \
-Delastic.apm.server_urls=http://localhost:8200 \
-Delastic.apm.environment=development \
-Delastic.apm.application_packages=com.example \
-Delastic.apm.transaction_sample_rate=1.0 \
-jar my-application.jar
// src/main/resources/elasticapm.properties
elastic.apm.service_name=my-service-name
elastic.apm.server_urls=http://localhost:8200
elastic.apm.environment=development
elastic.apm.application_packages=com.example
elastic.apm.transaction_sample_rate=1.0
@Configuration
public class ApmConfig {
@PostConstruct
public void init() {
Map<String, String> apmProps = new HashMap<>();
apmProps.put("service_name", "my-service-name");
apmProps.put("server_urls", "http://localhost:8200");
apmProps.put("environment", "development");
apmProps.put("application_packages", "com.example");
ElasticApmAttacher.attach(apmProps);
}
}
const transaction = apm.startTransaction('operation-name');
transaction.addLabels({
'user.id': userId,
'tenant.id': tenantId,
'operation.type': operationType,
'operation.status': status
});
const span = apm.startSpan('span-name');
span.addLabels({
'user.id': userId,
'tenant.id': tenantId,
'custom.label': value
});
# Method 1: Using decorator
from elasticapm import capture_span
@capture_span()
def my_function():
# your code here
from elasticapm import capture_span
with capture_span('operation_name', 'operation_type'):
# your code here
import elasticapm
span = elasticapm.capture_span('operation_name')
try:
# your code here
finally:
span.end()
import co.elastic.apm.api.CaptureSpan;
@CaptureSpan
public void myMethod() {
// your code here
}
import co.elastic.apm.api.ElasticApm;
Span span = ElasticApm.startSpan();
try {
span.setName("operation-name")
.setType("operation-type");
// your code here
} finally {
span.end();
}
const span = apm.startSpan('operation-name');
try {
// your code here
} finally {
span.end();
}
async function operationWithSpan() {
const span = apm.startSpan('operation-name');
try {
await someAsyncOperation();
} finally {
span.end();
}
}
const span = apm.startSpan('operation-name');
span.addLabels({
'operation.type': 'database',
'operation.details': 'query-users'
});
try {
// your code here
} finally {
span.end();
}