Skip to content

fix: correct usage of Resource class and attributes in examples #4579

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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/examples/basic_tracer/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
BatchSpanProcessor,
ConsoleSpanExporter,
)
from opentelemetry.semconv.resource import ResourceAttributes
Copy link
Contributor

@xrmx xrmx May 19, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are deprecated:

Use attributes defined in the opentelemetry.semconv.attributes and opentelemetry.semconv._incubating.attributes modules instead.


# Use Resource.create() instead of constructor directly
resource = Resource.create({"service.name": "basic_service"})
resource = Resource.create({ResourceAttributes.SERVICE_NAME: "basic_service"})

trace.set_tracer_provider(TracerProvider(resource=resource))

Expand Down
6 changes: 4 additions & 2 deletions docs/examples/fork-process-model/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ Gunicorn post_fork hook
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.semconv.resource import ResourceAttributes


def post_fork(server, worker):
server.log.info("Worker spawned (pid: %s)", worker.pid)

resource = Resource.create(attributes={
"service.name": "api-service"
ResourceAttributes.SERVICE_NAME: "api-service"
})

trace.set_tracer_provider(TracerProvider(resource=resource))
Expand All @@ -46,14 +47,15 @@ uWSGI postfork decorator
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.semconv.resource import ResourceAttributes
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor


@postfork
def init_tracing():
resource = Resource.create(attributes={
"service.name": "api-service"
ResourceAttributes.SERVICE_NAME: "api-service"
})

trace.set_tracer_provider(TracerProvider(resource=resource))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.semconv.resource import ResourceAttributes

bind = "127.0.0.1:8000"

Expand All @@ -48,7 +49,7 @@ def post_fork(server, worker):

resource = Resource.create(
attributes={
"service.name": "api-service",
ResourceAttributes.SERVICE_NAME: "api-service",
# If workers are not distinguished within attributes, traces and
# metrics exported from each worker will be indistinguishable. While
# not necessarily an issue for traces, it is confusing for almost
Expand Down
5 changes: 4 additions & 1 deletion docs/examples/fork-process-model/flask-uwsgi/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.semconv.resource import ResourceAttributes

application = flask.Flask(__name__)

Expand All @@ -34,7 +35,9 @@

@postfork
def init_tracing():
resource = Resource.create(attributes={"service.name": "api-service"})
resource = Resource.create(
attributes={ResourceAttributes.SERVICE_NAME: "api-service"}
)

trace.set_tracer_provider(TracerProvider(resource=resource))
# This uses insecure connection for the purpose of example. Please see the
Expand Down
5 changes: 3 additions & 2 deletions docs/examples/logs/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
BatchSpanProcessor,
ConsoleSpanExporter,
)
from opentelemetry.semconv.resource import ResourceAttributes

trace.set_tracer_provider(TracerProvider())
trace.get_tracer_provider().add_span_processor(
Expand All @@ -22,8 +23,8 @@
logger_provider = LoggerProvider(
resource=Resource.create(
{
"service.name": "shoppingcart",
"service.instance.id": "instance-12",
ResourceAttributes.SERVICE_NAME: "shoppingcart",
ResourceAttributes.SERVICE_INSTANCE_ID: "instance-12",
}
),
)
Expand Down
5 changes: 3 additions & 2 deletions docs/examples/opencensus-shim/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,16 @@
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.semconv.resource import ResourceAttributes
from opentelemetry.shim.opencensus import install_shim

DB = "example.db"

# Set up OpenTelemetry
tracer_provider = TracerProvider(
resource=Resource(
resource=Resource.create(
{
"service.name": "opencensus-shim-example-flask",
ResourceAttributes.SERVICE_NAME: "opencensus-shim-example-flask",
}
)
)
Expand Down