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

Aws faas #3340

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
bd6bf29
Factor out lifecycle listener and remove tracer arguments as those ca…
raphw Sep 27, 2023
b37db58
Move metrics and log reporting to ReportingTracer, and remove obsolet…
raphw Sep 28, 2023
f92b941
Add data writer abstraction to decouple plugins from Json writer impl…
raphw Sep 28, 2023
d8b89ab
Merge branch 'main' into metric-api
raphw Sep 28, 2023
2d6e4ac
Remove unused import after merge.
raphw Sep 28, 2023
fa6181b
Remove ASCII method as it can be replaced by more convenient calls.
raphw Sep 28, 2023
da91e15
Simplify API for JSON writing.
raphw Sep 28, 2023
c01c2fa
Further simplify API for JSON writing.
raphw Sep 28, 2023
b136b3d
Remove test code.
raphw Sep 28, 2023
72e5079
Add toString function to support tests.
raphw Sep 28, 2023
e3de0e4
Avoid escaping of suffix.
raphw Sep 28, 2023
d7887c3
Avoid duplicate escaping of suffix.
raphw Sep 28, 2023
ae642f8
Add support for direct tracing.
raphw Sep 28, 2023
742e76e
Fix mock in test.
raphw Sep 28, 2023
3ea33c8
Fix configuration exporter.
raphw Sep 28, 2023
6830206
Fix incorrect API port.
raphw Sep 29, 2023
020de77
Add direct transaction equivalent.
raphw Sep 29, 2023
9fefdb5
Remove dependency of AWS tracer onto agent-core module.
raphw Sep 29, 2023
1ff99be
Update apm-agent-core/src/main/java/co/elastic/apm/agent/context/Init…
raphw Oct 9, 2023
ca6ee7f
Update apm-agent-tracer/src/main/java/co/elastic/apm/agent/tracer/Lif…
raphw Oct 9, 2023
9925508
Rename init lifecycle listener.
raphw Oct 9, 2023
c823e5b
Merge remote-tracking branch 'rafael/aws-faas' into aws-faas
raphw Oct 9, 2023
f06060f
Add test for header caching.
raphw Oct 9, 2023
b0bb5c0
Merge branch 'main' into aws-faas
raphw Nov 20, 2023
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 @@ -21,6 +21,7 @@
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.tracer.GlobalTracer;
import co.elastic.apm.agent.impl.Tracer;
import co.elastic.apm.agent.tracer.reporting.ReportingTracer;
import org.stagemonitor.configuration.ConfigurationOptionProvider;
import org.stagemonitor.configuration.ConfigurationRegistry;

Expand All @@ -38,7 +39,7 @@ public class ConfigurationExporter {

public static void main(String[] args) throws Exception {
ElasticApmTracer tracer = mock(ElasticApmTracer.class);
doReturn(tracer).when(tracer).require(ElasticApmTracer.class);
doReturn(tracer).when(tracer).require(ReportingTracer.class);
doReturn(Tracer.TracerState.UNINITIALIZED).when(tracer).getState();
GlobalTracer.init(tracer);
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import co.elastic.apm.agent.tracer.GlobalTracer;
import co.elastic.apm.agent.impl.Tracer;
import co.elastic.apm.agent.sdk.ElasticApmInstrumentation;
import co.elastic.apm.agent.tracer.reporting.ReportingTracer;
import co.elastic.apm.agent.util.DependencyInjectingServiceLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;
Expand Down Expand Up @@ -53,6 +54,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* This is not an actual test.
Expand Down Expand Up @@ -88,7 +90,7 @@ class ConfigurationExporterTest {
void setUp() {
renderedDocumentationPath = Paths.get("../../docs/configuration.asciidoc");
ElasticApmTracer tracer = mock(ElasticApmTracer.class);
doReturn(tracer).when(tracer).require(ElasticApmTracer.class);
doReturn(tracer).when(tracer).require(ReportingTracer.class);
doReturn(Tracer.TracerState.UNINITIALIZED).when(tracer).getState();
GlobalTracer.init(tracer);
configurationRegistry = ConfigurationRegistry.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@
package co.elastic.apm.agent.bci;

import co.elastic.apm.agent.bci.bytebuddy.MatcherTimer;
import co.elastic.apm.agent.context.AbstractLifecycleListener;
import co.elastic.apm.agent.context.InitLifecycleListener;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;

import java.util.ArrayList;
import java.util.Collections;

public class InstrumentationStatsLifecycleListener extends AbstractLifecycleListener {
public class InstrumentationStatsLifecycleListener extends AbstractLifecycleListener implements InitLifecycleListener {
private static final Logger logger = LoggerFactory.getLogger(InstrumentationStatsLifecycleListener.class);

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@
*/
package co.elastic.apm.agent.collections;

import co.elastic.apm.agent.context.AbstractLifecycleListener;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.util.ExecutorUtils;
import co.elastic.apm.agent.sdk.logging.Logger;
import co.elastic.apm.agent.sdk.logging.LoggerFactory;
Expand All @@ -41,7 +40,7 @@ public WeakMapCleaner() {
}

@Override
public void start(ElasticApmTracer tracer) {
public void start() {
scheduler.scheduleWithFixedDelay(this, 1, 1, TimeUnit.SECONDS);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/
package co.elastic.apm.agent.configuration;

import co.elastic.apm.agent.context.LifecycleListener;
import co.elastic.apm.agent.context.InitLifecycleListener;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.report.ApmServerClient;
import co.elastic.apm.agent.report.serialize.DslJsonSerializer;
Expand All @@ -44,7 +44,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ApmServerConfigurationSource extends AbstractConfigurationSource implements LifecycleListener {
public class ApmServerConfigurationSource extends AbstractConfigurationSource implements InitLifecycleListener {

// log correlation is enabled by default in Java agent, thus removing it from warnings
private static final Set<String> IGNORED_REMOTE_KEYS = Collections.singleton("enable_log_correlation");
Expand All @@ -62,6 +62,9 @@ public class ApmServerConfigurationSource extends AbstractConfigurationSource im
private final byte[] buffer = new byte[4096];
private final DslJsonSerializer.Writer payloadSerializer;
private final ApmServerClient apmServerClient;

@Nullable
private ElasticApmTracer tracer;
@Nullable
private String etag;
private volatile Map<String, String> config = Collections.emptyMap();
Expand Down Expand Up @@ -111,12 +114,12 @@ public void reload() {
}

@Override
public void init(ElasticApmTracer tracer) throws Exception {
// noop
public void init(ElasticApmTracer tracer) {
this.tracer = tracer;
}

@Override
public void start(final ElasticApmTracer tracer) {
public void start() {
threadPool = ExecutorUtils.createSingleThreadDaemonPool("remote-config-poller", 1);
threadPool.execute(new Runnable() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -887,6 +887,7 @@ public ConfigurationOption<String> getServiceNameConfig() {
}

@Nullable
@Override
public String getServiceNodeName() {
String nodeName = serviceNodeName.get();
if (nodeName == null || nodeName.trim().isEmpty()) {
Expand All @@ -900,6 +901,7 @@ public long getDelayTracerStartMs() {
}

@Nullable
@Override
public String getServiceVersion() {
return serviceVersion.get();
}
Expand All @@ -910,6 +912,7 @@ public String getHostname() {
}

@Nullable
@Override
public String getEnvironment() {
return environment.get();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,16 @@
import org.stagemonitor.configuration.ConfigurationOption;
import org.stagemonitor.configuration.ConfigurationOptionProvider;

import javax.annotation.Nullable;

public class ServerlessConfiguration extends ConfigurationOptionProvider implements co.elastic.apm.agent.tracer.configuration.ServerlessConfiguration {
public static final String SERVERLESS_CATEGORY = "Serverless";

private final boolean runsOnAwsLambda;

@Nullable
private final String awsLambdaLogStreamName = PrivilegedActionUtils.getEnv("AWS_LAMBDA_LOG_STREAM_NAME");

public ServerlessConfiguration() {
String lambdaName = PrivilegedActionUtils.getEnv("AWS_LAMBDA_FUNCTION_NAME");
this.runsOnAwsLambda = null != lambdaName && !lambdaName.isEmpty();
Expand Down Expand Up @@ -65,4 +70,9 @@ public boolean runsOnAwsLambda() {
return runsOnAwsLambda;
}

@Override
@Nullable
public String awsLambdaLogStreamName() {
return awsLambdaLogStreamName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@
*/
package co.elastic.apm.agent.configuration;

import co.elastic.apm.agent.context.InitLifecycleListener;
import co.elastic.apm.agent.tracer.configuration.TimeDuration;
import co.elastic.apm.agent.context.AbstractLifecycleListener;
import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.impl.stacktrace.StacktraceConfiguration;
import co.elastic.apm.agent.util.VersionUtils;
Expand All @@ -38,7 +39,7 @@
* under Apache license 2.0.
* </p>
*/
public class StartupInfo extends AbstractLifecycleListener {
public class StartupInfo extends AbstractLifecycleListener implements InitLifecycleListener {

private static final Logger logger = LoggerFactory.getLogger(StartupInfo.class);
private final String elasticApmVersion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
*/
package co.elastic.apm.agent.context;

import co.elastic.apm.agent.tracer.AbstractLifecycleListener;
import co.elastic.apm.agent.tracer.LifecycleListener;

import java.io.Closeable;

public class ClosableLifecycleListenerAdapter extends AbstractLifecycleListener {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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.context;

import co.elastic.apm.agent.impl.ElasticApmTracer;
import co.elastic.apm.agent.tracer.LifecycleListener;

public interface InitLifecycleListener extends LifecycleListener {

/**
* Callback for tracer initialization. As opposed to {@link LifecycleListener#start()}, which may
* be called in a delay, this callback is called at the bootstrap of the JVM, before anything else starts.
* This may be useful for listeners that need to operate very early on, for example such that setup class loading
* requirement to support OSGi systems.
*/
void init(ElasticApmTracer tracer) throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,24 @@
import co.elastic.apm.agent.configuration.CoreConfiguration;
import co.elastic.apm.agent.configuration.MetricsConfiguration;
import co.elastic.apm.agent.configuration.ServerlessConfiguration;
import co.elastic.apm.agent.context.InitLifecycleListener;
import co.elastic.apm.agent.impl.metadata.FaaSMetaDataExtension;
import co.elastic.apm.agent.impl.metadata.Framework;
import co.elastic.apm.agent.impl.metadata.NameAndIdField;
import co.elastic.apm.agent.report.serialize.DslJsonDataWriter;
import co.elastic.apm.agent.tracer.reporting.DataWriter;
import co.elastic.apm.agent.tracer.reporting.DoubleSupplier;
import co.elastic.apm.agent.tracer.reporting.Labels;
import co.elastic.apm.agent.tracer.service.ServiceInfo;
import co.elastic.apm.agent.configuration.SpanConfiguration;
import co.elastic.apm.agent.context.ClosableLifecycleListenerAdapter;
import co.elastic.apm.agent.context.LifecycleListener;
import co.elastic.apm.agent.tracer.LifecycleListener;
import co.elastic.apm.agent.impl.baggage.Baggage;
import co.elastic.apm.agent.impl.baggage.W3CBaggagePropagation;
import co.elastic.apm.agent.impl.error.ErrorCapture;
import co.elastic.apm.agent.impl.metadata.MetaDataFuture;
import co.elastic.apm.agent.impl.sampling.ProbabilitySampler;
import co.elastic.apm.agent.impl.sampling.Sampler;
import co.elastic.apm.agent.tracer.direct.ProbabilitySampler;
import co.elastic.apm.agent.tracer.direct.Sampler;
import co.elastic.apm.agent.impl.stacktrace.StacktraceConfiguration;
import co.elastic.apm.agent.impl.transaction.AbstractSpan;
import co.elastic.apm.agent.impl.transaction.ElasticContext;
Expand All @@ -62,6 +70,7 @@
import co.elastic.apm.agent.tracer.reference.ReferenceCountedMap;
import co.elastic.apm.agent.util.DependencyInjectingServiceLoader;
import co.elastic.apm.agent.util.ExecutorUtils;
import com.dslplatform.json.DslJson;
import org.stagemonitor.configuration.ConfigurationOption;
import org.stagemonitor.configuration.ConfigurationOptionProvider;
import org.stagemonitor.configuration.ConfigurationRegistry;
Expand All @@ -80,6 +89,7 @@
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
Expand All @@ -106,6 +116,8 @@ public class ElasticApmTracer implements Tracer {

private static volatile boolean classloaderCheckOk = false;

private final DslJson<Object> dslJson = new DslJson<>(new DslJson.Settings<>());

private final ConfigurationRegistry configurationRegistry;
private final StacktraceConfiguration stacktraceConfiguration;
private final ApmServerClient apmServerClient;
Expand Down Expand Up @@ -299,6 +311,16 @@ public <T, C> Transaction startChildTransaction(@Nullable C headerCarrier, Heade
return startChildTransaction(headerCarrier, headersGetter, sampler, epochMicros, initiatingClassLoader);
}

@Nullable
@Override
public <T, C> co.elastic.apm.agent.tracer.TraceContext startChildTransaction(@Nullable C headerCarrier, HeaderGetter<T, C> textHeadersGetter) {
TraceContext childTraceContext = TraceContext.with64BitId(this);
if (childTraceContext.asChildOf(headerCarrier, textHeadersGetter)) {
return childTraceContext;
}
return null;
}

@Override
@Nullable
public <T, C> Transaction startChildTransaction(@Nullable C headerCarrier, HeaderGetter<T, C> headersGetter, Sampler sampler,
Expand Down Expand Up @@ -673,10 +695,12 @@ public List<ActivationListener> getActivationListeners() {
void init(List<LifecycleListener> lifecycleListeners) {
this.lifecycleListeners.addAll(lifecycleListeners);
for (LifecycleListener lifecycleListener : lifecycleListeners) {
try {
lifecycleListener.init(this);
} catch (Exception e) {
logger.error("Failed to init " + lifecycleListener.getClass().getName(), e);
if (lifecycleListener instanceof InitLifecycleListener) {
try {
((InitLifecycleListener) lifecycleListener).init(this);
} catch (Exception e) {
logger.error("Failed to init " + lifecycleListener.getClass().getName(), e);
}
}
}
}
Expand Down Expand Up @@ -733,7 +757,7 @@ private synchronized void startSync() {
reporter.start();
for (LifecycleListener lifecycleListener : lifecycleListeners) {
try {
lifecycleListener.start(this);
lifecycleListener.start();
} catch (Exception e) {
logger.error("Failed to start " + lifecycleListener.getClass().getName(), e);
}
Expand Down Expand Up @@ -945,10 +969,37 @@ public MetaDataFuture getMetaDataFuture() {
return metaDataFuture;
}

@Override
public void addMetric(String name, Labels labels, DoubleSupplier metric) {
metricRegistry.add(name, labels, metric);
}

@Override
public void removeMetric(String name, Labels labels) {
metricRegistry.removeGauge(name, labels);
}

@Override
public void reportLog(String log) {
reporter.reportLog(log);
}

@Override
public void reportLog(byte[] log) {
reporter.reportLog(log);
}

@Override
public DataWriter newWriter(int maxSerializedSize) {
return new DslJsonDataWriter(dslJson.newWriter(maxSerializedSize), reporter);
}

@Override
public ScheduledThreadPoolExecutor getSharedSingleThreadedPool() {
return sharedPool;
}

@Override
public void addShutdownHook(Closeable closeable) {
lifecycleListeners.add(ClosableLifecycleListenerAdapter.of(closeable));
}
Expand Down Expand Up @@ -981,4 +1032,18 @@ public Set<String> getTraceHeaderNames() {
public ServiceInfo autoDetectedServiceInfo() {
return AutoDetectedServiceInfo.autoDetected();
}

@Override
public boolean flush(long timeout, TimeUnit timeUnit) {
return reporter.flush(timeout, timeUnit, true);
}

@Override
public void notifyFaasMetaData(String frameworkName, String frameworkVersion, @Nullable String accountName, @Nullable String accountId, @Nullable String region) {
metaDataFuture.getFaaSMetaDataExtensionFuture().complete(new FaaSMetaDataExtension(
new Framework(frameworkName, frameworkVersion),
new NameAndIdField(accountName, accountId),
region
));
}
}
Loading
Loading