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

Fix the issue of duplicate traceId and spanId caused by RandomIdGenerator #4377

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

lyred193
Copy link

@lyred193 lyred193 commented Jan 3, 2025

Description

Fix the issue of duplicate traceId and spanId when the user sets the global random operator seed.

Fixes #4376

Type of change

Please delete options that are not relevant.

  • Bug fix

How Has This Been Tested?

  • test_id_generator.py

Does This PR Require a Contrib Repo Change?

No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@lyred193 lyred193 requested a review from a team as a code owner January 3, 2025 06:17
Copy link

linux-foundation-easycla bot commented Jan 3, 2025

CLA Signed

The committers listed above are authorized under a signed CLA.

@xrmx xrmx changed the title Fix the issue of duplicate traceId and spanId caused by RandomIdGener… Fix the issue of duplicate traceId and spanId caused by RandomIdGenerator Jan 14, 2025
@xrmx
Copy link
Contributor

xrmx commented Jan 14, 2025

Could you please add an explanation on why this fixes things?

@lyred193
Copy link
Author

lyred193 commented Jan 16, 2025

Could you please add an explanation on why this fixes things?

In some scenarios, such as model performance evaluation, we prefer the split between training and testing data to be random. By setting a fixed random seed, we can ensure that each model uses the same dataset for training and validation, enabling fair comparisons.

In this context, integrating OpenTelemetry can lead to the duplication of traceId and spanId if the application is restarted.

Here’s a demo:

import uvicorn
from fastapi import FastAPI, HTTPException
from logging import getLogger

_logger = getLogger(__name__)

def init():
    import random
    random.seed(10)

app = FastAPI()

@app.get("/health")
async def health_check():
    return {"data": "ok"}

@app.get("/evaluation")
async def evaluation():
    # Randomly generate a list of test numbers
    random_numbers = random.sample(range(1, 101), 10)
    '''
    Other operations
    test model 1
    test model 2
    '''
    return {"data": {"score": 0.6}}

if __name__ == "__main__":
    init()
    uvicorn.run(app, host="0.0.0.0", port=8000)

In this demo, when accessing the /evaluation endpoint, the generated traceId is 164207228320579316746596838417247989971, and the spanId is 273610340023782072, with the span name being /evaluation.

After restarting the application (due to an update or a manual restart), accessing the /health endpoint generates the same traceId 164207228320579316746596838417247989971 and spanId 273610340023782072. However, the span name has changed to /health, which is clearly incorrect.

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

Successfully merging this pull request may close these issues.

When the random seed is set, it causes duplicate traceId and spanId.
2 participants