Monitoring Athena and Data Access

Centre for Excellence

July 2024

Why monitor Athena?

center

image

athena monitoring mindmap center

image

Overview

  1. Why monitor Athena?

  2. Out-of-Scope

  3. Amazon CloudWatch

  4. AWS CloudTrail

  5. Amazon Managed Grafana

  6. (Proposed) Solution Architecture

  7. Next Steps

image

Out of Scope (for now)

  • Incident response ➡️ Application Monitoring

  • Data Quality Metrics ➡️ Data Catalogue

  • create-a-derived-table model metrics

  • create-a-pipeline non-athena metrics

  • airflow non-athena metrics ➡️ Analytical Platform

  • NLP of Athena queries ➡️ ❓

image

Amazon CloudWatch

What is happening on AWS?

image

CloudWatch Demo

Monitoring Athena Workgroups using CloudWatch Metrics

image

AWS CloudTrail

Who did what on AWS?

CloudTrail Architecture center

image

Recording / Querying CloudTrail events

CloudTrail provides three ways to record and view activity in an AWS account:
Event history

Automatically enabled by default.

Allows you to view the last 90 days of management events in a Region.

There are no CloudTrail charges for viewing the Event history.

CloudTrail Lake

Events are aggregated into event data stores based on criteria.

Can view the last 10 or 7 years of events across regions or accounts.

Events can be queried using CloudTrail Lake queries.

Data stores and queries incur charges.

Trails

Stores events in an S3 bucket & optionally to CloudWatch Logs.

Events can be queried using CloudWatch Logs Insight or Athena.

Events are flattened which can be easier to query using Athena, especially if they contain arrays.

Amazon S3 storage and querying incur charges.

image

CloudTrail Demo

Monitoring AWS Glue API and Athena API events

image

Amazon Managed Grafana

  • Grafana is an open-source analytics platform that helps you query, visualize, alert on, and understand your metrics wherever they are stored.

  • Amazon Managed Grafana is a fully managed service for Grafana.

  • You can use it with CloudWatch by adding it as a data source.

  • This table compares Amazon Managed Grafana vs CloudWatch for dashboarding:

    • Amazon Managed Grafana integrates with AWS Organizations to enable you to read data from AWS sources such as CloudWatch across all your accounts.
    • Grafana provides a larger collection of visualizations.
image

Grafana Demo

image

Proposed Athena and Data Monitoring Architecture

solution architecture

image

Proposed Athena and Data Monitoring Architecture

The architecture is split into three parts:

  1. Monitor data access using AWS Glue APIs, and aggregate/query using Athena.
    This approach still needs to be evaluated because it relies on the undocumented BatchGetTable API.

  2. Monitor Athena usage at the workgroup level using CloudWatch metrics.

  3. Monitor Athena usage at the user level using various Athena APIs, and aggregate using CloudWatch Log.
    This approach still needs to be evaluated because of the cost incurred triggering an lambda function every time an Athena query is called.

image

Next Steps

2024 Q3

More investigations
  1. Deploy workgroups for Airflow
  2. Collaborate with OP discovery
  3. Colloborate with AWS on how to monitor data usage

2024 Q4

Cross-cutting monitoring
  1. Set up DMET monitoring working group?
  2. Deploy monitoring infrastructure
  3. Update dashboards

2025 Q1

Application monitoring
  1. Hand-over Athena and data monitoring to AP?
  2. Convert dashboards to code?
  3. Build monitoring for applications?
image

Appendix

image

Using Athena workgroups in Airflow

  1. Create airflow-{folder} workgroup using athena_workgroups.tf

  2. Add mojap-athena-query-dump/{folder} to S3 read_write permissions on Airflow role

  3. Add "WR_WORKGROUP": "airflow-{folder}" to the env_vars dictionary that is passed to the Airflow task

  4. That's it!

image

center

image

Custom CloudWatch Logs

watchtower is a lightweight adapter between Python logging and CloudWatch Logs

import watchtower, logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.addHandler(watchtower.CloudWatchLogHandler(log_group_name="soumaya_test"))
logger.info(dict(table="apple", details={}))
logger.info(dict(table="banana", details={}))
logger.info(dict(table="orange", details={}))
logger.info(dict(table="apple", details={}))

CloudWatch Log Insights query stats count(@timestamp) by table returns:

orange 1
apple 2
banana 1
image

A web service that records AWS API calls for your account and delivers log files to you. The recorded information includes the identity of the API caller, the time of the API call, the source IP address of the API caller, the request parameters, and the response elements returned by the AWS service.