Skip to content

Instantly share code, notes, and snippets.

@fredgido
Created September 29, 2023 11:35
Show Gist options
  • Select an option

  • Save fredgido/f36dd078767885746fdde8447cc96cf2 to your computer and use it in GitHub Desktop.

Select an option

Save fredgido/f36dd078767885746fdde8447cc96cf2 to your computer and use it in GitHub Desktop.
import time
from flask import Flask, jsonify
from opentelemetry import trace
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
PORT = 8100
ENDPOINT = "http://localhost:4317"
app = Flask(__name__)
resource = Resource.create({"service.name": "flask_test3"})
otlp_exporter = OTLPSpanExporter(
endpoint=ENDPOINT,
insecure=True,
)
trace.set_tracer_provider(TracerProvider(resource=resource))
trace.get_tracer_provider().add_span_processor(SimpleSpanProcessor(otlp_exporter))
FlaskInstrumentor().instrument_app(app)
@app.route("/")
def test():
start_time = time.perf_counter()
for i in range(10000):
with trace.get_tracer("top").start_as_current_span("ba") as span:
span.set_attribute("slice_id", i)
pass
print(time.perf_counter() - start_time, " for 10000") # 5sec for 10000
return jsonify({"message": "ok"})
if __name__ == "__main__":
app.run(debug=False, host="0.0.0.0", port=PORT)
import time
import sentry_sdk
from flask import Flask, jsonify
from sentry_sdk.integrations.flask import FlaskIntegration
PORT = 8100
DSN = "https://XXX@domain/1"
sentry_sdk.init(
dsn=DSN,
integrations=[
FlaskIntegration(),
],
)
app = Flask(__name__)
@app.route("/")
def test():
start_time = time.perf_counter()
for i in range(10000):
with sentry_sdk.start_transaction(op="custom_transaction", name="Custom Transaction") as tr:
tr.set_tag("slice_id", i)
pass
print(time.perf_counter() - start_time, " for 10000") # 420ms for 10000
return jsonify({"message": "ok"})
if __name__ == "__main__":
app.run(debug=True, port=8100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment