Created
March 7, 2025 11:30
-
-
Save bartvdbraak/4d5ae022c59cc685e7326c73c791e629 to your computer and use it in GitHub Desktop.
Python Code/Notebook for getting metrics from Azure Trusted Signing using Diagnostic Settings exported to Azure Blob Storage (Codesign Transactions)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # -*- coding: utf-8 -*- | |
| """azure-codesign-metrics.ipynb | |
| Automatically generated by Colab. | |
| Original file is located at | |
| <REDACTED> | |
| # Azure Codesigning Metrics | |
| Because Microsoft is a bit slow with actually implementing a way to query the metrics of Azure Trusted Signing accounts, I had to make this Python notebook to do it for us. What they do expose is a way to let Diagnostic Settings log towards a Storage Accounts' Blob Container. This notebook uses a connection string to the Azure Storage Account and downloads all blobs in the container and measures their frequency based on the month. | |
| We can expand this functionality to retrieve other data columns from the entries in the blobs (each instance of codesign). | |
| ## Install dependencies | |
| """ | |
| !pip install azure-storage-blob pandas | |
| """## Connect to Azure Blob Storage""" | |
| from azure.storage.blob import BlobServiceClient | |
| from google.colab import userdata | |
| # Connection string from Azure Storage | |
| CONNECTION_STRING = userdata.get('connection_string') | |
| # Create a blob service client | |
| blob_service_client = BlobServiceClient.from_connection_string(CONNECTION_STRING) | |
| # Set container name | |
| CONTAINER_NAME = userdata.get('container_name') | |
| # Connect to the container | |
| container_client = blob_service_client.get_container_client(CONTAINER_NAME) | |
| # List blobs (files) in the container | |
| blobs = [blob.name for blob in container_client.list_blobs()] | |
| # Show the first 10 blob names | |
| blobs[:10] | |
| """## Download and Read a File""" | |
| import json | |
| # Pick the first blob file | |
| blob_name = blobs[0] # Change index if needed | |
| # Get blob client and download file | |
| blob_client = container_client.get_blob_client(blob_name) | |
| blob_data = blob_client.download_blob().readall() | |
| # Decode JSONL (JSON lines) file | |
| lines = blob_data.decode("utf-8").split("\n") | |
| sample_entries = [json.loads(line) for line in lines if line.strip()] | |
| # Print first few entries | |
| sample_entries[:3] | |
| """## Count Transactions per Month""" | |
| import re | |
| from collections import Counter | |
| def count_transactions(blobs): | |
| transaction_counts = Counter() | |
| for blob_name in blobs: | |
| # Extract the year and month from the blob path | |
| match = re.search(r"y=(\d{4})/m=(\d{2})", blob_name) | |
| if not match: | |
| continue # Skip if no match is found | |
| year, month = match.groups() | |
| month_key = f"{year}-{month}" | |
| # Download and read blob data | |
| blob_client = container_client.get_blob_client(blob_name) | |
| blob_data = blob_client.download_blob().readall() | |
| # Count number of lines (each line is a transaction) | |
| num_transactions = sum(1 for line in blob_data.decode("utf-8").split("\n") if line.strip()) | |
| transaction_counts[month_key] += num_transactions | |
| return transaction_counts | |
| transaction_counts = count_transactions(blobs) | |
| print(transaction_counts) | |
| """## Visualization""" | |
| import pandas as pd | |
| import matplotlib.pyplot as plt | |
| # Convert to DataFrame | |
| df = pd.DataFrame(transaction_counts.items(), columns=["Month", "TransactionCount"]) | |
| df = df.sort_values("Month") | |
| # Plot | |
| plt.figure(figsize=(10,5)) | |
| plt.bar(df["Month"], df["TransactionCount"], color="blue") | |
| plt.xlabel("Month") | |
| plt.ylabel("Transaction Count") | |
| plt.title("Transactions per Month") | |
| plt.xticks(rotation=45) | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment