Basic Features
Trace IDs & Distributed Tracing
11 min
abv allows you to bring your own trace ids (e g , messageid, traceid, correlationid) for distributed tracing and linking traces across services for lookups between services by default, abv assigns random ids (uuid, cuid) to all logged events for the new otel based sdks (python v3), abv assigns random 32 hexchar trace ids and 16 hexchar observation ids it is recommended to use your own domain specific ids (e g , messageid, traceid, correlationid) as it helps with downstream use cases like deeplinking to the trace from your own ui or logs evaluating and adding custom metrics to the trace fetching the trace from the api data model trace ids in abv must be unique within a project are used to identify and group related observations can be used for distributed tracing across services support upsert operations (creating or updating based on id) for the new otel based sdks (python v3), trace ids are 32 hexchar lowercase strings and observation ids are 16 hexchar lowercase strings usage python sdk the python sdk uses w3c trace context ids by default, which are 32 character lowercase hexadecimal string for trace ids 16 character lowercase hexadecimal string for observation (span) ids using the decorator from abvdev import observe, get client import uuid @observe() def process user request(user id, request data) \# function logic here pass \# use custom trace id by passing it as special keyword argument external trace id = "custom " + str(uuid uuid4()) \# get a consistent trace id for the same user abv = get client() trace id = abv create trace id(seed=external trace id) # 32 hexchar lowercase string, deterministic with seed process user request( user id="user 123", request data={"query" "hello"}, abv trace id=trace id ) deterministic trace ids you can generate deterministic trace ids from any string using create trace id() from abvdev import get client abv = get client() \# generate deterministic trace id from an external id external id = "request 12345" trace id = abv create trace id(seed=external id) \# use this trace id in a span with abv start as current span( name="process request", trace context={"trace id" trace id} ) as span \# your code here pass manually creating spans with custom trace context from abvdev import get client abv = get client() \# use a predefined trace id with trace context parameter with abv start as current span( name="my operation", trace context={ "trace id" "abcdef1234567890abcdef1234567890", # must be 32 hex chars "parent span id" "fedcba0987654321" # optional, 16 hex chars } ) as span print(f"this span has trace id {span trace id}") \# your code here accessing current trace id from abvdev import get client abv = get client() with abv start as current span(name="outer operation") as span \# access the trace id of the current span current trace id = abv get current trace id() current span id = abv get current observation id() print(f"current trace id {current trace id}") print(f"direct access {span trace id}") js/ts sdk the js/ts sdk uses w3c trace context ids by default, which are 32 character lowercase hexadecimal string for trace ids 16 character lowercase hexadecimal string for observation (span) ids accessing the current trace id import { startobservation, getactivetraceid } from "@abvdev/tracing"; await startobservation("run", async (span) => { const traceid = getactivetraceid(); console log(`current trace id ${traceid}`); }); you may access the current active trace id via the getactivetraceid function deterministic trace ids when starting a new trace with a predetermined traceid, you must also provide an arbitrary parent spanid for the parent observation the parent span id value is irrelevant as long as it is a valid 16 hexchar string as the span does not actually exist within the trace but is only used for trace id inheritance of the created observation you can create valid, deterministic trace ids from a seed string using createtraceid this is useful for correlating abv traces with ids from external systems, like a support ticket id import { createtraceid, startobservation } from "@abvdev/tracing"; const externalid = "support ticket 54321"; // generate a valid, deterministic traceid from the external id const abvtraceid = await createtraceid(externalid); // you can now start a new trace with this id const rootspan = startobservation( "process ticket", {}, { parentspancontext { traceid abvtraceid, spanid "0123456789abcdef", // a valid 16 hexchar string; value is irrelevant as parent span does not exist but only used for inheritance traceflags 1, // mark trace as sampled }, } ); // later, you can regenerate the same traceid to score or retrieve the trace const scoringtraceid = await createtraceid(externalid); // scoringtraceid will be the same as abvtraceid setting a parentspancontext will detach the created span from the active span context as it no longer inherits from the current active span in the context learn more in the typescript sdk overview docid\ j4sdnlmdmnfmk99ootgn7 opentelemetry when using opentelemetry opentelemetry , trace ids are handled automatically by the opentelemetry sdk you can access and set trace ids using the opentelemetry context from opentelemetry import trace from opentelemetry trace import status, statuscode tracer = trace get tracer( name ) with tracer start as current span("my operation") as span \# get the trace id trace id = format(span get span context() trace id, "032x") \# set custom attributes span set attribute("custom trace id", trace id)