Thread
#prefect-community
    Ajith Kumara Beragala Acharige Lal

    Ajith Kumara Beragala Acharige Lal

    1 year ago
    Hello Experts, am trying to configure email notifications(alerts) , but am stuck due to this error
    Exception raised while calling state handlers: ClientError('400 Client Error: Bad Request for url: <http://10.110.0.7:8081/graphql>\n\nThe following error messages were provided by the GraphQL server:\n\n    GRAPHQL_VALIDATION_FAILED: Cannot query field "secret_value" on type "Query".\n\nThe GraphQL query was:\n\n    query($name: String!) {\n                secret_value(name: $name)\n    }\n\nThe passed variables were:\n\n    {"name": "EMAIL_USERNAME"}\n',)
    `Traceback (most recent call last):
      File "/usr/local/lib/python3.6/dist-packages/prefect/client/secrets.py", line 137, in get
        value = secrets[self.name]
    KeyError: 'EMAIL_USERNAME'
    
    During handling of the above exception, another exception occurred:
    
    Traceback (most recent call last):
      File "/usr/local/lib/python3.6/dist-packages/prefect/client/client.py", line 361, in _send_request
        response.raise_for_status()
      File "/usr/local/lib/python3.6/dist-packages/requests/models.py", line 943, in raise_for_status
        raise HTTPError(http_error_msg, response=self)
    requests.exceptions.HTTPError: 400 Client Error: Bad Request for url: <http://10.110.0.7:8081/graphql>
    
    The above exception was the direct cause of the following exception:
    
    Traceback (most recent call last):
      File "/usr/local/lib/python3.6/dist-packages/prefect/engine/cloud/flow_runner.py", line 120, in call_runner_target_handlers
        old_state=old_state, new_state=new_state
      File "/usr/local/lib/python3.6/dist-packages/prefect/engine/flow_runner.py", line 116, in call_runner_target_handlers
        new_state = handler(self.flow, old_state, new_state) or new_state
      File "punctualityDailySummariesPrefectShell.py", line 40, in email_on_failure
      File "/usr/local/lib/python3.6/dist-packages/prefect/utilities/tasks.py", line 450, in method
        return run_method(self, *args, **kwargs)
      File "/usr/local/lib/python3.6/dist-packages/prefect/tasks/notifications/email_task.py", line 98, in run
        username = cast(str, Secret("EMAIL_USERNAME").get())
      File "/usr/local/lib/python3.6/dist-packages/prefect/client/secrets.py", line 157, in get
        raise exc
      File "/usr/local/lib/python3.6/dist-packages/prefect/client/secrets.py", line 147, in get
        variables=dict(name=self.name),
      File "/usr/local/lib/python3.6/dist-packages/prefect/client/client.py", line 304, in graphql
        retry_on_api_error=retry_on_api_error,
      File "/usr/local/lib/python3.6/dist-packages/prefect/client/client.py", line 220, in post
        retry_on_api_error=retry_on_api_error,
      File "/usr/local/lib/python3.6/dist-packages/prefect/client/client.py", line 446, in _request
        session=session, method=method, url=url, params=params, headers=headers
      File "/usr/local/lib/python3.6/dist-packages/prefect/client/client.py", line 374, in _send_request
        raise ClientError(f"{exc}\n{graphql_msg}") from exc
    prefect.utilities.exceptions.ClientError: 400 Client Error: Bad Request for url: <http://10.110.0.7:8081/graphql>
    
    The following error messages were provided by the GraphQL server:
    
        GRAPHQL_VALIDATION_FAILED: Cannot query field "secret_value" on type "Query".
    
    The GraphQL query was:
    
        query($name: String!) {
                    secret_value(name: $name)
        }
    
    The passed variables were:
    {"name": "EMAIL_USERNAME"}`
    I tried both adding prefect email username passwords to bash file and in the code itself
    with Flow("flow_punctualityDailySummaries",state_handlers=[email_on_failure]) as f:
        context.secrets["PREFECT__CONTEXT__SECRETS__EMAIL_USERNAME"] = "ab@xxxx"
        context.secrets["PREFECT__CONTEXT__SECRETS__EMAIL_PASSWORD"] = "xxxxxxxx"
        context.secrets["PREFECT__CONTEXT__SECRETS__EMAIL_HOST"] = "<http://smtp.gmail.com|smtp.gmail.com>"
        context.secrets["PREFECT__CONTEXT__SECRETS__EMAIL_PORT"] = "587"
        context.secrets["PREFECT__CONTEXT__SECRETS__EMAIL_FAIL_SILENT"] = "False"
        context.secrets["EMAIL_USERNAME"] = "ab@xxxx"
        context.secrets["EMAIL_PASSWORD"] = "xxxxxxxx"
        context.secrets["EMAIL_HOST"] = "<http://smtp.gmail.com|smtp.gmail.com>"
        context.secrets["EMAIL_PORT"] = "587"
        context.secrets["EMAIL_FAIL_SILENT"] = "False"
    where to set ? am running
    SERVER
    mode ( local test works but SERVER execution fails
    am triggering via EmailTask
    Chris White

    Chris White

    1 year ago
    Hi Ajith! I think you’re very close — a few notes: • Prefect Secrets via the GraphQL API are only compatible with Prefect Cloud; we should improve the error message here to make that more clear • you are correct that setting context values is another way to pass secrets; however, you are mixing up some concepts at this step. In particular, you ultimately want to set
    PREFECT__CONTEXT__SECRETS__EMAIL_USERNAME
    as an environment variable on your flow run. The easiest way to do this is via the
    -e
    CLI flag on your agent - this will ensure that the context value is set appropriately for each run that your agent submits
    Ajith Kumara Beragala Acharige Lal

    Ajith Kumara Beragala Acharige Lal

    1 year ago
    Hi @Chris White thanks a lot for the quick response , lemme try it out
    and @Chris White - is there any documentation which mentioned same? 🤔
    Chris White

    Chris White

    1 year ago
    Ajith Kumara Beragala Acharige Lal

    Ajith Kumara Beragala Acharige Lal

    1 year ago
    Thank you @Chris White
    Chris White

    Chris White

    1 year ago
    anytime!