Comments (8)
Hi @arturg-acoustic! Thanks for reaching out. The recommendation is correct, but I think that the place you chose to initialize the tracer isn't quite right.
Please, check our recommended usage for AWS Lambda with github.com/DataDog/datadog-lambda-go
here: https://docs.datadoghq.com/serverless/aws_lambda/installation/go/?tab=custom
I hope this helps. Please reach again if you need more assistance.
from dd-trace-go.
this guide does not touch the subject of using tracer.Start(tracer.WithRuntimeMetrics())
You are right. Although this is our official recommendation for AWS Lambda, if you want to run dd-trace-go
as part of a lambda, I still think that starting the tracer in the HandleRequest
isn't the best approach.
According to AWS Lambda official docs, your code should include a main function where you can initialize the tracer, so all the requests handled by the lambda will reuse it. Like this:
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
)
type event struct {
// ...
}
// skipping other functions used in the HandleRequest.
func HandleRequest(ctx context.Context, _ event) (event, error) {
currentSpan, ctx := tracer.StartSpanFromContext(ctx, "HandleRequest")
currentSpan.SetTag("service", "service-name")
traceparent := getTraceparent(ctx, currentSpan)
traceID, traceIdErr := getTraceIdFromTraceparent(traceparent)
ctx = context.WithValue(ctx, "trace_id", traceID)
defer currentSpan.Finish()
doLogic()
return whateverTheFunctionReturns()
}
func main() {
tracer.Start(tracer.WithRuntimeMetrics())
defer tracer.Stop()
lambda.Start(HandleRequest)
}
Keep in mind that starting the trace will slightly increase your cold start, but it shouldn't be noticeable.
from dd-trace-go.
One day the service stopped sending traces to datadog. Might be a problem related to other external factors (lambda layer for example). Update resolved the issue.
Thank you. I agree that issue might be closed - I opened it mostly for informational value on the fix.
from dd-trace-go.
this guide does not touch the subject of using tracer.Start(tracer.WithRuntimeMetrics())
from dd-trace-go.
@darccio thank you for your reply. I did figure out that would help, but I would like to point out that before the update, the code worked perfectly. It would be a good idea to prevent users from shooting themselves in the foot / adding proper documentation for using tracer.Start() in datadog go docs.
Maybe you guys could investigate what caused the change?
from dd-trace-go.
@arturg-acoustic The docs assume that the tracer is initialized once, and the tracer itself expects to be a singleton.
I agree we could add a warning to make this more clear. Creating a tracer for each request is a code smell for us.
There are some things that are not clear so we can investigate:
- What was your previous
dd-trace-go
version? - What this the first time you used
tracer.WithRuntimeMetrics
?
from dd-trace-go.
dd-trace-go I used previously was 1.54. It was used in March of 2023. It stopped working around Summer/Autumn, but did not cause any memory leaks until the update in March of 2024.
from dd-trace-go.
@arturg-acoustic What do you mean by "it stopped working"? The tracer stopped or only the runtime metrics? In any case, using the tracer in this way - creating it in each request - isn't a best practice.
Actually, calling tracer.Stop
in that place was probably introducing latency, as stopping the tracer flushes all the spans in each request, instead of doing that asynchronously during the lambda lifespan.
I'll make sure the docs are clear. I'll also try to understand what went wrong, but one year of updates is fair amount of possibilities to consider.
As the issue at hand is solved, I'll proceed to close the issue in the meantime.
from dd-trace-go.
Related Issues (20)
- [BUG] - Unable to import tracer package HOT 6
- [BUG] Panic in contrib/gorm.io/gorm.v1 HOT 2
- [BUG] Vault trace HttpClient panic when configuring TLS HOT 3
- v2/contrib: Add datastreams support for contrib/IBM/sarama
- [BUG] Hostname providers spamming traces HOT 4
- contrib/gofiber/fiber.v2: possibility to exclude spans generation for specific requests HOT 2
- go.mod: add support for Go v1.22.0
- Add support for custom tags to gqlgen contrib HOT 6
- [BUG] contrib/labstack/echo middlewares both call and return error HOT 8
- Add option to skip Introspection tracing HOT 4
- [BUG] Data race in tracer/payload.go HOT 2
- [BUG] Update dependencies to avoid vulnerabilities HOT 3
- [Flaky test] contrib/99designs/gqlen: TestAppSec/monitoring/basic is flaky HOT 1
- [BUG] data race when accessing capabilities HOT 1
- [BUG] dd fails to unmarshal rates after writing traces HOT 7
- [BUG] agent tries to connect over http when unix socket endpoint is enabled HOT 4
- [BUG] contrib/dimfeld/httptreemux.v5: trailing slash requests are redirected with raw URL being tracked HOT 1
- use fp to get goroutine traceback. HOT 2
- [BUG] Support for http.ResponseController timeouts in WrapHandler HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dd-trace-go.