serverless / dashboard

MIT License
26 stars 10 forks source link

SDK error when working with slack_bolt application #833

Open martmichals opened 1 year ago

martmichals commented 1 year ago

Hello, I've run into the following error when developing an application w/slack_bolt:

{
  "message": "Cannot set tag: Tag http.status_code is already set",
  "name": "DuplicateTraceSpanName",
  "stacktrace": "Traceback (most recent call last):\n  File \"/var/runtime/urllib3/connectionpool.py\", line 440, in _make_request\n    httplib_response = conn.getresponse(buffering=True)\n  File \"/opt/python/sls_sdk/lib/instrumentation/http.py\", line 306, in _func\n    response = self._original_getresponse(_self, *args, **kwargs)\nTypeError: getresponse() got an unexpected keyword argument 'buffering'\n\nDuring handling of the above exception, another exception occurred:\n\nTraceback (most recent call last):\n  File \"/opt/python/sls_sdk/lib/tags.py\", line 60, in set\n    self._set(key, value)\n  File \"/opt/python/sls_sdk/lib/tags.py\", line 56, in _set\n    raise DuplicateTraceSpanName(f\"Cannot set tag: Tag {name} is already set\")\nsls_sdk.exceptions.DuplicateTraceSpanName: Cannot set tag: Tag http.status_code is already set\n",
  "type": "ERROR_TYPE_CAUGHT_SDK_INTERNAL"
}

Here is the stacktrace value in a more readable form:

Traceback (most recent call last):
File "/var/runtime/urllib3/connectionpool.py", line 440, in _make_request
httplib_response = conn.getresponse(buffering=True)
File "/opt/python/sls_sdk/lib/instrumentation/http.py", line 306, in _func
response = self._original_getresponse(_self, *args, **kwargs)
TypeError: getresponse() got an unexpected keyword argument 'buffering'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/opt/python/sls_sdk/lib/tags.py", line 60, in set
self._set(key, value)
File "/opt/python/sls_sdk/lib/tags.py", line 56, in _set
raise DuplicateTraceSpanName(f"Cannot set tag: Tag {name} is already set")
sls_sdk.exceptions.DuplicateTraceSpanName: Cannot set tag: Tag http.status_code is already set

Here is code for the lambda application defining a simple slack bot that accepts one slash command:

import os
import logging

# Slack bolt
import sys
sys.path.insert(0, 'deps')
from slack_bolt import App
from slack_bolt.adapter.aws_lambda import SlackRequestHandler

# Set up the bolt application
app = App(
    process_before_response = True,
    token = os.environ['SLACK_BOT_TOKEN'],
    signing_secret = os.environ['SLACK_SIGNING_SECRET']
)

# Log incoming requests
@app.middleware
def log_req(logger, body, next):
    logger.debug(body)
    return next()

# Acknowledge incoming slack command
def acknowledge_command(body, ack):
    # Acknowledge command
    if (body_text := body.get('text')) is None or body_text.strip() == '':
        ack(f':x: `{body.get("command")}` needs more context')

    # Got command, no response
    ack()

# Actual processing of the command
def process_command(respond, body):
    respond(f'Completed! (task: {body.get("command")})')

# Register command with application
app.command('/test')(
    ack = acknowledge_command,
    lazy = [process_command]
)

# Set up logging format and level
SlackRequestHandler.clear_all_log_handlers()
logging.basicConfig(
    format = '%(asctime)s %(message)s', 
    level = logging.WARN
)

# Set up lambda function
def handler(event, context):
    return SlackRequestHandler(app=app).handle(event, context)

Please let me know if any additional information is required.