Repository: DataDog/datadog-agent (go)

Issue:
Serverless: Lambda alias invocations do not pick up AWS tags
<!--
If you suspect your issue is a bug, please attach the output of the Agent status page,
see https://docs.datadoghq.com/agent/faq/agent-commands/#agent-information
-->

**Output of the info page (if this is a bug)**
```
(Paste the output of the info page here)
```

**Describe what happened:**

A stack was created with a single hello world lambda.  The lambda was tested twice from the AWS UI.  Once, the function was tested directly, and the other time the alias was tested.  The tags that were defined on the function were not picked up in the alias trace. Also, the service header on the span reverted back to `aws.lambda` rather than the defined `service` tag.


**Describe what you expected:**

I expected the spans and the tags to match


**Steps to reproduce the issue:**

### Define the stack

**./bin/test_datadog_enhanced.ts**
```ts
#!/usr/bin/env node
import 'source-map-support/register';
import * as cdk from '@aws-cdk/core';
import { TestDatadogEnhancedStack } from '../lib/test_datadog_enhanced-stack';

const app = new cdk.App();
new TestDatadogEnhancedStack(app, 'TestDatadogEnhancedStack', {
  /* If you don't specify 'env', this stack will be environment-agnostic.
   * Account/Region-dependent features and context lookups will not work,
   * but a single synthesized template can be deployed anywhere. */

  /* Uncomment the next line to specialize this stack for the AWS Account
   * and Region that are implied by the current CLI configuration. */
  // env: { account: process.env.CDK_DEFAULT_ACCOUNT, region: process.env.CDK_DEFAULT_REGION },

  /* Uncomment the next line if you know exactly what Account and Region you
   * want to deploy the stack to. */
  // env: { account: '123456789012', region: 'us-east-1' },

  /* For more information, see https://docs.aws.amazon.com/cdk/latest/guide/environments.html */
});
```

**./lib/handler/helloWorld.ts**
```ts
export const handler = async (event: any, context: any) => {
  return "Hello world";
};
```

**./lib/test_datadog-enhanced-stack.ts**
```ts
import { NodejsFunction } from "@aws-cdk/aws-lambda-nodejs";
import { Construct, Stack, StackProps, Tags } from "@aws-cdk/core";
import { Datadog } from "datadog-cdk-constructs";

export class TestDatadogEnhancedStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);

    Tags.of(this).add("env", "datadog-test");
    Tags.of(this).add("service", "datadog-test");

    const fn = new NodejsFunction(this, "Function", {
      entry: "./lib/handler/helloWorld.ts",
      functionName: "datadog-test",
      memorySize: 512,
    });

    fn.currentVersion.addAlias("live");

    const dd = new Datadog(this, "Datadog", {
      nodeLayerVersion: 61,
      extensionLayerVersion: 10,
      apiKey: "api-key",
    });

    dd.addLambdaFunctions([fn]);
  }
}
```

### Deploy the stack

```bash
yarn cdk deploy
```

### Test the function

![image (1)](https://user-images.githubusercontent.com/12355170/130618354-d27802c5-c53d-475a-b5ae-0919d48c5bdf.png)

### Test the alias

![image (2)](https://user-images.githubusercontent.com/12355170/130618378-ae6b76ee-fe7c-4f3e-9eea-ba1181d2d3e0.png)

### Compare the spans

![image (3)](https://user-images.githubusercontent.com/12355170/130618406-23b94e4b-2e63-4837-8320-2fe4c4710417.png)



**Additional environment details (Operating System, Cloud provider, etc):**



Pull request:
[serverless] Remove alias from function ARN
<!--
* New contributors are highly encouraged to read our
  [CONTRIBUTING](/CONTRIBUTING.md) documentation.
* The pull request:
  * Should only fix one issue or add one feature at a time.
  * Must update the test suite for the relevant functionality.
  * Should pass all status checks before being reviewed or merged.
* Commit titles should be prefixed with general area of pull request's change.
* Draft PRs should be prefixed with `[WIP]` in their title.

-->
### What does this PR do?
<!--
* A brief description of the change being made with this pull request.
* If the description here cannot be expressed in a succint form, consider
  opening multiple pull requests instead of a single one.
-->
- Remove the alias from the function ARN. This is available through `AWS_LAMBDA_FUNCTION_VERSION`.  

### Motivation
- Fixes #8980 
- Fixes issue with logs having different `hostname` when called with an alias, resulting in invocation page not showing invocations made using an alias. Traces don't show up as a result of the invocations not showing up, since we use request ids returned by the initial logs query.

**Without Alias**

![image](https://user-images.githubusercontent.com/6404023/141301490-2e68f2d6-069e-490c-87b5-2872f5c7cfc1.png)

**With Alias (before fix):**

![image](https://user-images.githubusercontent.com/6404023/141302050-db8fa3b0-df9c-49cb-9f58-f4bafde8926e.png)


**With Alias (after fix):**

![image](https://user-images.githubusercontent.com/6404023/141301229-abf6fa7c-cac7-430f-b5ae-cd4365ab09d3.png)

<!--
* What inspired you to submit this pull request?
* Link any related GitHub issues or PRs here.
-->

### Additional Notes

<!--
* Anything else we should know when reviewing?
* Include benchmarking information here whenever possible.
* Include info about alternatives that were considered and why the proposed
  version was chosen.
-->

### Possible Drawbacks / Trade-offs

<!--
* What are the possible side-effects or negative impacts of the code change?
-->

### Describe how to test/QA your changes

<!--
* Write here in detail or link to detailed instructions on how this change can
  be tested/QAd/validated, including any environment setup.
-->

### Reviewer's Checklist
<!--
* Authors can use this list as a reference to ensure that there are no problems
  during the review but the signing off is to be done by the reviewer(s).

Note: Adding GitHub labels is only possible for contributors with write access.
-->

- [x] If known, an appropriate milestone has been selected; otherwise the `Triage` milestone is set.
- [x] The appropriate `team/..` label has been applied, if known.
- [x] A [release note](https://github.com/DataDog/datadog-agent/blob/main/docs/dev/contributing.md#reno) has been added or the `changelog/no-changelog` label has been applied.
- [x] Changed code has automated tests for its functionality.
- [x] Adequate QA/testing plan information is provided if the `qa/skip-qa` label is not applied.
- [x] If applicable, docs team has been notified or [an issue has been opened on the documentation repo](https://github.com/DataDog/documentation/issues/new).
- [x] If applicable, the `need-change/operator` and `need-change/helm` labels have been applied.
- [x] If applicable, the [config template](https://github.com/DataDog/datadog-agent/blob/main/pkg/config/config_template.yaml) has been updated.


Patch:
diff --git a/pkg/serverless/serverless.go b/pkg/serverless/serverless.go
index 4583d53e59..41062fd93b 100644
--- a/pkg/serverless/serverless.go
+++ b/pkg/serverless/serverless.go
@@ -163,7 +163,8 @@ func WaitForNextInvocation(stopCh chan struct{}, daemon *daemon.Daemon, id regis
 	}
 
 	if payload.EventType == Invoke {
-		callInvocationHandler(daemon, payload.InvokedFunctionArn, payload.DeadlineMs, safetyBufferTimeout, payload.RequestID, handleInvocation)
+		functionArn := removeQualifierFromArn(payload.InvokedFunctionArn)
+		callInvocationHandler(daemon, functionArn, payload.DeadlineMs, safetyBufferTimeout, payload.RequestID, handleInvocation)
 	}
 	if payload.EventType == Shutdown {
 		log.Debug("Received shutdown event. Reason: " + payload.ShutdownReason)
@@ -238,3 +239,14 @@ func computeTimeout(now time.Time, deadlineMs int64, safetyBuffer time.Duration)
 	currentTimeInMs := now.UnixNano() / int64(time.Millisecond)
 	return time.Duration((deadlineMs-currentTimeInMs)*int64(time.Millisecond) - int64(safetyBuffer))
 }
+
+func removeQualifierFromArn(functionArn string) string {
+	functionArnTokens := strings.Split(functionArn, ":")
+	tokenLength := len(functionArnTokens)
+
+	if tokenLength > 7 {
+		functionArnTokens = functionArnTokens[:tokenLength-1]
+		return strings.Join(functionArnTokens, ":")
+	}
+	return functionArn
+}


Test patch:
diff --git a/pkg/serverless/serverless_test.go b/pkg/serverless/serverless_test.go
index e8e5df06e8..9936bebac4 100644
--- a/pkg/serverless/serverless_test.go
+++ b/pkg/serverless/serverless_test.go
@@ -65,3 +65,16 @@ func TestComputeTimeout(t *testing.T) {
 	safetyBuffer := 3 * time.Millisecond
 	assert.Equal(t, 7*time.Millisecond, computeTimeout(fakeCurrentTime, fakeDeadLineInMs, safetyBuffer))
 }
+
+func TestRemoveQualifierFromArnWithAlias(t *testing.T) {
+	invokedFunctionArn := "arn:aws:lambda:eu-south-1:601427279990:function:inferred-spans-function-urls-dev-harv-function-urls:$latest"
+	functionArn := removeQualifierFromArn(invokedFunctionArn)
+	expectedArn := "arn:aws:lambda:eu-south-1:601427279990:function:inferred-spans-function-urls-dev-harv-function-urls"
+	assert.Equal(t, functionArn, expectedArn)
+}
+
+func TestRemoveQualifierFromArnWithoutAlias(t *testing.T) {
+	invokedFunctionArn := "arn:aws:lambda:eu-south-1:601427279990:function:inferred-spans-function-urls-dev-harv-function-urls"
+	functionArn := removeQualifierFromArn(invokedFunctionArn)
+	assert.Equal(t, functionArn, invokedFunctionArn)
+}