Skip to content

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

Closed
lyred193 wants to merge 4 commits into
open-telemetry:mainfrom
lyred193:fix/fix-id-generator
Closed

Fix the issue of duplicate traceId and spanId caused by RandomIdGenerator#4377
lyred193 wants to merge 4 commits into
open-telemetry:mainfrom
lyred193:fix/fix-id-generator

Conversation

@lyred193

@lyred193 lyred193 commented Jan 3, 2025

Copy link
Copy Markdown

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
@linux-foundation-easycla

linux-foundation-easycla Bot commented Jan 3, 2025

Copy link
Copy Markdown

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

xrmx commented Jan 14, 2025

Copy link
Copy Markdown
Contributor

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

@lyred193

lyred193 commented Jan 16, 2025

Copy link
Copy Markdown
Author

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.

@github-actions

Copy link
Copy Markdown

This PR has been automatically marked as stale because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 days of this comment.
If you're still working on this, please add a comment or push new commits.

@github-actions github-actions Bot added the Stale label Mar 12, 2026
@github-actions

Copy link
Copy Markdown

This PR has been closed due to inactivity. Please reopen if you would like to continue working on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

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

2 participants