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

How can i populate DATADOG_INSTANCE_NAME in UploadWorker.kt? #2252

Closed
ZivStep opened this issue Sep 9, 2024 · 3 comments · Fixed by #2257
Closed

How can i populate DATADOG_INSTANCE_NAME in UploadWorker.kt? #2252

ZivStep opened this issue Sep 9, 2024 · 3 comments · Fixed by #2257
Labels
question Further information is requested

Comments

@ZivStep
Copy link

ZivStep commented Sep 9, 2024

Question

When using datadog in a library, i initilize it with an instance name but the name does not get populated into DATADOG_INSTANCE_NAME in the UploadWorker.kt, this results in an exception thrown inside the worker:

SDK instance with name _dd.sdk_core.default is not found, returning no-op implementation. 
Please make sure to call Datadog.initialize([instanceName]) before getting the instance.
SDK instance was requested from: (Ask Gemini)

at com.datadog.android.Datadog.getInstance(Datadog.kt:139)
at com.datadog.android.core.internal.data.upload.UploadWorker.doWork(UploadWorker.kt:42)
at androidx.work.Worker$1.run(Worker.java:82)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
at java.lang.Thread.run(Thread.java:1012)
2024-09-09 11:32:40.574 19812-20100 Datadog 
co.onestep.android.demoapp
E  Datadog has not been initialized.

Since those lines

 val instanceName = inputData.getString(DATADOG_INSTANCE_NAME)
 val sdkCore = Datadog.getInstance(instanceName) as? InternalSdkCore

return a null instance name and the call to Datadog.getInstance(instanceName/this is always null/) throws an exception

@ZivStep ZivStep added the question Further information is requested label Sep 9, 2024
@ambushwork
Copy link
Contributor

Hi @ZivStep,

Thank you for your question!

Based on the stack trace, it appears that Datadog.initialize() has not been called before attempting to get the instance. Please ensure that you have followed the steps in this documentation. And it is recommended to call it in application's onCreate() callback.

If you have confirmed that Datadog.initialize() is being called, please share the relevant code where it is invoked, so we can better understand the issue.

Thank you!

@ZivStep
Copy link
Author

ZivStep commented Sep 9, 2024

I call Datadog.initialize() in an SDK i am developing, my SDK initilization proccess is recommended but not guaranteed to be called in the application's onCreate.

This is how i initilize datadog in my SDK

internal fun initDataDog(
    context: Context,
): SdkCore {
    
    val configuration = Configuration.Builder(
        clientToken = BuildConfig.DATA_DOG_CLIENT_TOKEN,
        env = BuildConfig.BUILD_TYPE,
        service = BuildConfig.LIBRARY_PACKAGE_NAME,
    ).build()
    
    Datadog.initialize(BuildConfig.LIBRARY_PACKAGE_NAME, context, configuration, TrackingConsent.GRANTED)
    Datadog.setVerbosity(if (BuildConfig.DEBUG) Log.VERBOSE else Log.ASSERT)

    with(Datadog.getInstance(BuildConfig.LIBRARY_PACKAGE_NAME)) {
        // Log collection
        setupLogCollection(this)

        // Tracing
        setupTracing(this)

        // Realtime user monitoring
        setupRUM(this)
    }

    return Datadog.getInstance(BuildConfig.LIBRARY_PACKAGE_NAME)
}

As you can see i am initilizing it with an instance name, to avoid any conflicts with any datadog instances that may be in use by my hosting app.

This all works fine, however, the code for doWork in UploadWorker.kt, line 42, is calling Datadog.getInstance() without my instance name, the instance name is taken from DATADOG_INSTANCE_NAME, but it is internal and i cannot populate it.

This results in the error above.

@ambushwork
Copy link
Contributor

Hi @ZivStep,

Thank you for sharing your code! We've identified an issue with the UploadWorker and are currently working on a fix.
In the meantime, the data collected by the SDK can still be uploaded while the app is in use, the error you’re seeing is just a warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants