|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch B.V. under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch B.V. licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package co.elastic.apm.agent.awslambda.helper; |
| 20 | + |
| 21 | +import co.elastic.apm.agent.awslambda.MapTextHeaderGetter; |
| 22 | +import co.elastic.apm.agent.sdk.internal.util.PrivilegedActionUtils; |
| 23 | +import co.elastic.apm.agent.tracer.*; |
| 24 | +import co.elastic.apm.agent.tracer.metadata.CloudOrigin; |
| 25 | +import com.amazonaws.services.lambda.runtime.Context; |
| 26 | +import com.amazonaws.services.lambda.runtime.events.ApplicationLoadBalancerRequestEvent; |
| 27 | +import com.amazonaws.services.lambda.runtime.events.ApplicationLoadBalancerResponseEvent; |
| 28 | + |
| 29 | +import javax.annotation.Nonnull; |
| 30 | +import javax.annotation.Nullable; |
| 31 | +import java.util.Map; |
| 32 | + |
| 33 | +public class ApplicationLoadBalancerRequestTransactionHelper extends AbstractAPIGatewayTransactionHelper<ApplicationLoadBalancerRequestEvent, ApplicationLoadBalancerResponseEvent> { |
| 34 | + @Nullable |
| 35 | + private static ApplicationLoadBalancerRequestTransactionHelper INSTANCE; |
| 36 | + |
| 37 | + private ApplicationLoadBalancerRequestTransactionHelper(Tracer tracer) { |
| 38 | + super(tracer); |
| 39 | + } |
| 40 | + |
| 41 | + public static ApplicationLoadBalancerRequestTransactionHelper getInstance() { |
| 42 | + if (INSTANCE == null) { |
| 43 | + INSTANCE = new ApplicationLoadBalancerRequestTransactionHelper(GlobalTracer.get()); |
| 44 | + } |
| 45 | + return INSTANCE; |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected Transaction doStartTransaction(ApplicationLoadBalancerRequestEvent loadBalancerRequestEvent, Context lambdaContext) { |
| 50 | + Transaction transaction = tracer.startChildTransaction(loadBalancerRequestEvent.getHeaders(), MapTextHeaderGetter.INSTANCE, PrivilegedActionUtils.getClassLoader(loadBalancerRequestEvent.getClass())); |
| 51 | + |
| 52 | + if (transaction != null) { |
| 53 | + String host = getHost(loadBalancerRequestEvent.getHeaders()); |
| 54 | + super.fillHttpRequestData(transaction, loadBalancerRequestEvent.getHttpMethod(), loadBalancerRequestEvent.getHeaders(), host, |
| 55 | + loadBalancerRequestEvent.getPath(), getQueryString(loadBalancerRequestEvent.getQueryStringParameters()), loadBalancerRequestEvent.getBody()); |
| 56 | + } |
| 57 | + |
| 58 | + return transaction; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void captureOutputForTransaction(Transaction transaction, ApplicationLoadBalancerResponseEvent responseEvent) { |
| 63 | + fillHttpResponseData(transaction, responseEvent.getHeaders(), responseEvent.getStatusCode()); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected void setTransactionTriggerData(Transaction transaction, ApplicationLoadBalancerRequestEvent loadBalancerRequestEvent) { |
| 68 | + transaction.withType(TRANSACTION_TYPE); |
| 69 | + CloudOrigin cloudOrigin = transaction.getContext().getCloudOrigin(); |
| 70 | + cloudOrigin.withServiceName("elb"); |
| 71 | + cloudOrigin.withProvider("aws"); |
| 72 | + FaasTrigger faasTrigger = transaction.getFaas().getTrigger(); |
| 73 | + faasTrigger.withType("http"); |
| 74 | + faasTrigger.withRequestId(getHeader(loadBalancerRequestEvent, "x-amzn-trace-id")); |
| 75 | + LoadBalancerElbTargetGroupArnMetadata metadata = parseMetadata(loadBalancerRequestEvent); |
| 76 | + if (null != metadata) { |
| 77 | + ServiceOrigin serviceOrigin = transaction.getContext().getServiceOrigin(); |
| 78 | + serviceOrigin.withName(metadata.getTargetGroupName()); |
| 79 | + serviceOrigin.withId(metadata.getTargetGroupArn()); |
| 80 | + cloudOrigin.withAccountId(metadata.getAccountId()); |
| 81 | + cloudOrigin.withRegion(metadata.getCloudRegion()); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Nullable |
| 86 | + private String getHeader(@Nonnull ApplicationLoadBalancerRequestEvent loadBalancerRequestEvent, |
| 87 | + @Nonnull String headerName) { |
| 88 | + Map<String, String> headers = loadBalancerRequestEvent.getHeaders(); |
| 89 | + if (null == headers) { |
| 90 | + return null; |
| 91 | + } |
| 92 | + return headers.get(headerName); |
| 93 | + } |
| 94 | + |
| 95 | + @Nullable |
| 96 | + private LoadBalancerElbTargetGroupArnMetadata parseMetadata(ApplicationLoadBalancerRequestEvent event) { |
| 97 | + if (null == event.getRequestContext()) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + ApplicationLoadBalancerRequestEvent.Elb elb = event.getRequestContext().getElb(); |
| 101 | + if (null == elb) { |
| 102 | + return null; |
| 103 | + } |
| 104 | + String targetGroupArn = elb.getTargetGroupArn(); |
| 105 | + if (null == targetGroupArn) { |
| 106 | + return null; |
| 107 | + } |
| 108 | + LoadBalancerElbTargetGroupArnMetadata metadata = new LoadBalancerElbTargetGroupArnMetadata(targetGroupArn); |
| 109 | + String[] arnParts = targetGroupArn.split(":"); |
| 110 | + int arnPartsLength = arnParts.length; |
| 111 | + if (arnPartsLength < 4) { |
| 112 | + return metadata; |
| 113 | + } |
| 114 | + metadata.withCloudRegion(arnParts[3]); |
| 115 | + if (arnPartsLength < 5) { |
| 116 | + return metadata; |
| 117 | + } |
| 118 | + metadata.withAccountId(arnParts[4]); |
| 119 | + if (arnPartsLength < 6) { |
| 120 | + return metadata; |
| 121 | + } |
| 122 | + String targetGroup = arnParts[5]; |
| 123 | + String[] targetGroupParts = targetGroup.split("/"); |
| 124 | + if (targetGroupParts.length < 2) { |
| 125 | + return metadata; |
| 126 | + } |
| 127 | + return metadata.withTargetGroupName(targetGroupParts[2]); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + protected String getApiGatewayVersion() { |
| 132 | + throw new UnsupportedOperationException("Not supported by ELB"); |
| 133 | + } |
| 134 | + |
| 135 | + @Nullable |
| 136 | + @Override |
| 137 | + protected String getHttpMethod(ApplicationLoadBalancerRequestEvent event) { |
| 138 | + return event.getHttpMethod(); |
| 139 | + } |
| 140 | + |
| 141 | + @Nullable |
| 142 | + @Override |
| 143 | + protected String getRequestContextPath(ApplicationLoadBalancerRequestEvent event) { |
| 144 | + return event.getPath(); |
| 145 | + } |
| 146 | + |
| 147 | + @Nullable |
| 148 | + @Override |
| 149 | + protected String getStage(ApplicationLoadBalancerRequestEvent event) { |
| 150 | + throw new UnsupportedOperationException("Not supported by ELB"); |
| 151 | + } |
| 152 | + |
| 153 | + @Nullable |
| 154 | + @Override |
| 155 | + protected String getResourcePath(ApplicationLoadBalancerRequestEvent event) { |
| 156 | + return null; |
| 157 | + } |
| 158 | + |
| 159 | + @Nullable |
| 160 | + @Override |
| 161 | + String getDomainName(ApplicationLoadBalancerRequestEvent apiGatewayRequest) { |
| 162 | + return null; |
| 163 | + } |
| 164 | +} |
0 commit comments