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

[Serverless] support DD_ENHANCED_METRICS env in lambda extension #8161

Merged

Conversation

maxday
Copy link
Contributor

@maxday maxday commented May 14, 2021

What does this PR do?

Support DD_ENHANCED_METRICS env variable in Lambda Extension

Motivation

Consistency with dd-lambda-*

Describe how to test your changes

  • Unit testing
  • Manual integration testing by building and publishing a new extension layer (v103) + set variable to true, then false, then true. We can see that
    1- aws.lambda.invocations are sent no matter what
    2-aws.lambda.enhanced.duration are sent only when the variable was set to true

enhanced

invocation

@maxday maxday requested a review from a team as a code owner May 14, 2021 19:34
@maxday maxday changed the title Maxday/respect dd enhanced metrics env [Serverless] respect dd enhanced metrics env May 14, 2021
@maxday maxday changed the title [Serverless] respect dd enhanced metrics env [Serverless] respect DD_ENHANCED_METRICS env May 14, 2021
@maxday maxday changed the title [Serverless] respect DD_ENHANCED_METRICS env [Serverless] support DD_ENHANCED_METRICS env in lambda extension May 14, 2021
@@ -846,6 +846,7 @@ func InitConfig(config Config) {

// Serverless Agent
config.BindEnvAndSetDefault("serverless.logs_enabled", true)
config.BindEnvAndSetDefault("enhanced_metrics", true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should consider making this serverless.enhanced_metrics. If I understand correctly, this will allow us to configure using the environment variable DD_SERVERLESS_ENHANCED_METRICS but also the configuration file like so:

serverless:
   logs_enabled: false
   enhanced_metrics: true

The downside is that the environment variable would be different from what is used in the Lambda Library :/

What are your thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to respect both values? And give DD_SERVERLESS_ENHANCED_METRICS higher precedence?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@@ -130,3 +130,13 @@ func calculateEstimatedCost(billedDurationMs float64, memorySizeMb float64) floa
// on some arch. (i.e. 1.00000000000002 values)
return math.Round((baseLambdaInvocationPrice+(gbSeconds*lambdaPricePerGbSecond))*10e12) / 10e12
}

// generateEnhancedMetrics generates enhanced metrics from logs and dispatch them to the chan
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good refactor 👍

@@ -335,6 +316,33 @@ func (l *LogsCollection) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}

// processMessage performs logic about metrics and tags on the message
func enhanceMessage(message aws.LogMessage, arn string, lastRequestID string, functionName string, computeEnhancedMetrics bool, metricTags []string, metricsChan chan []metrics.MetricSample) {
Copy link
Contributor

@nhinsch nhinsch May 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring says processMessage but the function is called enhanceMessage...

I think processMessage is a better name. This function does other things besides enhancing the message -- it also sets the global request ID, cold start variable, among other things...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sendLogsToIntake := config.Datadog.GetBool("serverless.logs_enabled")
computeEnhancedMetrics := config.Datadog.GetBool("enhanced_metrics")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rename computeEnhancedMetrics to enhancedMetricsEnabled and rename sendLogsToIntake to logsEnabled -- to make it more clear that they are not functions but booleans.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


func areEnhancedMetricsEnabled() bool {
priorityEnv := "serverless.enhanced_metrics"
nonPriorityEnv := "enhanced_metrics"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this approach of allowing either environment variable could make sense. I think we should consider the pros and cons of which one should be prioritized.

I do think that in terms of documentation, we probably want to document only the DD_ENHANCED_METRICS environment variable:

  • Most customers will likely prefer to configure the extension using environment variables rather than a config file
  • DD_ENHANCED_METRICS will enable/disable enhanced metrics in both the Lambda Library and the Extension, whereas if they used DD_SERVERLESS_ENHANCED_METRICS, it would only affect the extension and they would need to add DD_ENHANCED_METRICS anyway.

Because of that, maybe we should prioritize DD_ENHANCED_METRICS? And then fall back to DD_SERVERLESS_ENHANCED_METRICS if that is present in the config file?

Another option would be to ditch this logic of two different environment variables and just support DD_ENHANCED_METRICS. The only downside is that the config file would have to look like this:

enhanced_metrics: true
serverless:
     logs_enabled: true

However, most customers won't use the config file anyway -- and if they do, it won't apply to the Library, so it's not the best way to change this setting.

Sorry for all of the debating and back-and-forth around how to best handle this -- I do think it makes sense to think this through very carefully because once we add these environment variables/config options, they cannot be easily changed or removed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for raising this flag, this is indeed an important decision.
IMO we should

  • leave the two env variables because some customers are used to DD_ENHANCED_METRICS but we also have consistency in the config file
  • set DD_SERVERLESS_ENHANCED_METRICS with a higher priority than DD_ENHANCED_METRICS since I agree that not a lot of customers will use the configuration file but if they do, I think they might not expect any override be applied afterwards
    Does it make sense ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's go with only one env variable

@maxday maxday requested a review from nhinsch May 17, 2021 22:11
@maxday maxday merged commit 5a00534 into release/lambda-extension-v8 May 18, 2021
@maxday maxday deleted the maxday/respect-dd-enhanced-metrics-env branch May 18, 2021 13:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants