├── EventCreator ├── EventCreator │ ├── instances.txt │ ├── Configuration │ │ ├── GeneralSettings.cs │ │ ├── StorageDbSettings.cs │ │ └── QueueStorageSettings.cs │ ├── appsettings.json │ ├── Clients │ │ ├── CloudEventExtensions.cs │ │ ├── PgClient.cs │ │ └── EventsQueueClient.cs │ ├── EventCreator.csproj │ └── Program.cs └── EventCreator.sln ├── AppsMonitoring ├── .csharpierignore ├── .csharpierrc.yaml ├── .dockerignore ├── test │ └── Altinn.Apps.Monitoring.Tests │ │ ├── data │ │ └── mini.db │ │ ├── Application │ │ ├── Slack │ │ │ ├── _snapshots │ │ │ │ ├── SlackAlerterTests.Deserialization_Of_Slack_Error_Response_Succeeds.verified.txt │ │ │ │ ├── SlackAlerterTests.Deserialization_Of_Slack_Ok_Response_Succeeds.verified.txt │ │ │ │ ├── SlackAlerterTests.Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry_case=200-error_waitForRetryEvents=0.verified.txt │ │ │ │ ├── SlackAlerterTests.Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry_case=500-error_waitForRetryEvents=0.verified.txt │ │ │ │ ├── SlackAlerterTests.Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry_case=429-ratelimited_waitForRetryEvents=0.verified.txt │ │ │ │ ├── SlackAlerterTests.Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry_case=200-ok_waitForRetryEvents=0.verified.txt │ │ │ │ ├── SlackAlerterTests.Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry_case=500-error-then-ok_waitForRetryEvents=0.verified.txt │ │ │ │ ├── SlackAlerterTests.Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry_case=429-ratelimited-then-ok_waitForRetryEvents=0.verified.txt │ │ │ │ └── SlackAlerterTests.Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry_case=500-error-4-times-then-ok_waitForRetryEvents=1.verified.txt │ │ │ └── SlackAlerterTests.cs │ │ ├── Db │ │ │ ├── _snapshots │ │ │ │ ├── TelemetryEntityTests.TelemetryData_Metrics_Serialization_Roundtrip_Succeeds.verified.txt │ │ │ │ ├── TelemetryEntityTests.TelemetryData_Logs_Serialization_Roundtrip_Succeeds.verified.txt │ │ │ │ ├── TelemetryEntityTests.TelemetryData_Trace_Serialization_Roundtrip_Succeeds.verified.txt │ │ │ │ ├── IndexingTests.Db_Indexes_Are_Not_Missing_case=500-error-4-times-then-ok.verified.txt │ │ │ │ ├── SeederTests.Seeds_Db_Successfully.verified.txt │ │ │ │ └── RepositoryTests.Insert_Telemetry_Is_Idempotent.verified.txt │ │ │ ├── SeederTests.cs │ │ │ ├── IndexingTests.cs │ │ │ ├── TelemetryEntityTests.cs │ │ │ └── TestData.cs │ │ └── _snapshots │ │ │ ├── OrchestratorTests.Orchestration_Progresses_Successfully_serviceOwner=one_generator=Empty.verified.txt │ │ │ └── OrchestratorTests.Orchestration_Progresses_Successfully_serviceOwner=skd_generator=WithSeeder.verified.txt │ │ ├── ModuleInitializer.cs │ │ └── Altinn.Apps.Monitoring.Tests.csproj ├── global.json ├── src │ └── Altinn.Apps.Monitoring │ │ ├── Application │ │ ├── Db │ │ │ ├── ConnectionString.cs │ │ │ ├── IndexRecommendation.cs │ │ │ ├── QueryStateEntity.cs │ │ │ ├── Config.cs │ │ │ ├── AlertEntity.cs │ │ │ └── TelemetryEntity.cs │ │ ├── Querying │ │ │ ├── QueryType.cs │ │ │ ├── IQueryLoader.cs │ │ │ ├── QueryLibrary.cs │ │ │ ├── Query.cs │ │ │ └── StaticQueryLoader.cs │ │ ├── IServiceOwnerDiscovery.cs │ │ ├── IAlerter.cs │ │ ├── Azure │ │ │ ├── AzureClients.cs │ │ │ ├── AzureServiceOwnerDiscovery.cs │ │ │ └── AzureServiceOwnerResources.cs │ │ ├── Telemetry.cs │ │ ├── IServiceOwnerTelemetryAdapters.cs │ │ ├── AppConfiguration.cs │ │ └── DbUp │ │ │ └── Migrator.cs │ │ ├── appsettings.json │ │ ├── Properties │ │ └── launchSettings.json │ │ ├── appsettings.Development.json │ │ ├── Dockerfile │ │ ├── Domain │ │ └── ServiceOwner.cs │ │ └── Altinn.Apps.Monitoring.csproj ├── infra │ ├── deployment │ │ ├── at24 │ │ │ ├── kustomization.yaml │ │ │ └── env.yaml │ │ ├── prod │ │ │ ├── kustomization.yaml │ │ │ └── env.yaml │ │ ├── tt02 │ │ │ ├── kustomization.yaml │ │ │ └── env.yaml │ │ └── base │ │ │ ├── service.yaml │ │ │ ├── kustomization.yaml │ │ │ └── deployment.yaml │ ├── debug-profile.json │ ├── postgres_init.sql │ ├── purge.sql │ ├── servers.json │ ├── proper_at24.yaml │ ├── flux-push.sh │ ├── reset-env.sh │ ├── bootstrap-env.sh │ └── configure-env-shell.sh ├── .config │ └── dotnet-tools.json ├── AppsMonitoring.slnx ├── README.md ├── Directory.Build.props ├── Makefile └── docker-compose.yml ├── README.md ├── renovate.json ├── RepoCleanup ├── Application │ ├── Models │ │ └── Environment.cs │ ├── Commands │ │ ├── CreateDefaultTeamsCommand.cs │ │ ├── CreateRepoForOrgsCommand.cs │ │ ├── DeleteRepoForOrgsCommand.cs │ │ ├── AddTeamToRepoCommand.cs │ │ ├── MigrateAltinn2FormSchemasCommand.cs │ │ └── CreateOrgCommand.cs │ └── CommandHandlers │ │ ├── CreateOrgCommandHandler.cs │ │ ├── AddTeamToRepoCommandHandler.cs │ │ ├── DeleteRepoForOrgsCommandHandler.cs │ │ ├── CreateDefaultTeamsCommandHandler.cs │ │ └── CreateRepoForOrgsCommandHandler.cs ├── Infrastructure │ └── Clients │ │ ├── Gitea │ │ ├── TrustModel.cs │ │ ├── User.cs │ │ ├── TransferRepoOption.cs │ │ ├── GiteaResponse.cs │ │ ├── File.cs │ │ ├── Team.cs │ │ ├── Repository.cs │ │ ├── Organisation.cs │ │ ├── CreateTeamOption.cs │ │ └── CreateRepoOption.cs │ │ └── Altinn2 │ │ ├── Altinn2ReportingService.cs │ │ ├── Altinn2Form.cs │ │ ├── Altinn2Service.cs │ │ └── AltinnServiceRepository.cs ├── Utils │ ├── TeamOption.cs │ ├── NotALogger.cs │ └── StringExtensions.cs ├── appsettings.json ├── RepoCleanup.csproj ├── RepoCleanup.sln ├── Functions │ ├── CreateRepoFunction.cs │ ├── DeleteDatamodelsRepoFunction.cs │ ├── AddTeamToRepoFunction.cs │ ├── MigrateXsdSchemasFunction.cs │ ├── SetupNewServiceOwnerFunction.cs │ └── SharedFunctionSnippets.cs ├── Globals.cs └── Data │ └── orgs.json ├── .github ├── CODEOWNERS └── workflows │ ├── assign-issues-to-projects.yml │ ├── apps-monitoring-pr-build.yml │ ├── codeql.yml │ ├── apps-monitoring-promote.yml │ └── apps-monitoring-deploy.yml ├── LICENSE ├── .gitattributes └── altinn-tools.sln /EventCreator/EventCreator/instances.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /AppsMonitoring/.csharpierignore: -------------------------------------------------------------------------------- 1 | bin/ 2 | obj/ 3 | .git/ 4 | *.xml 5 | *.csproj 6 | *.props 7 | *.targets 8 | -------------------------------------------------------------------------------- /AppsMonitoring/.csharpierrc.yaml: -------------------------------------------------------------------------------- 1 | printWidth: 120 2 | useTabs: false 3 | tabWidth: 4 4 | endOfLine: auto 5 | -------------------------------------------------------------------------------- /AppsMonitoring/.dockerignore: -------------------------------------------------------------------------------- 1 | **/bin/ 2 | **/obj/ 3 | .config/ 4 | infra/ 5 | **/appsettings.Secret.json 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Altinn tools 2 | 3 | Various tools used by the Altinn 3 team to help with sporadic tasks. 4 | -------------------------------------------------------------------------------- /renovate.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://docs.renovatebot.com/renovate-schema.json", 3 | "extends": [ 4 | "local>Altinn/renovate-config" 5 | ] 6 | } 7 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/data/mini.db: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/altinn/altinn-tools/main/AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/data/mini.db -------------------------------------------------------------------------------- /AppsMonitoring/global.json: -------------------------------------------------------------------------------- 1 | { 2 | "sdk": { 3 | "version": "10.0.100", 4 | "rollForward": "latestFeature", 5 | "allowPrerelease": false 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Db/ConnectionString.cs: -------------------------------------------------------------------------------- 1 | namespace Altinn.Apps.Monitoring.Application.Db; 2 | 3 | internal sealed record ConnectionString(string Value); 4 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Slack/_snapshots/SlackAlerterTests.Deserialization_Of_Slack_Error_Response_Succeeds.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | Error: Something went wrong, 3 | Ok: false 4 | } -------------------------------------------------------------------------------- /AppsMonitoring/infra/deployment/at24/kustomization.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: kustomize.config.k8s.io/v1beta1 2 | kind: Kustomization 3 | namespace: apps-monitor 4 | resources: 5 | - ../base 6 | patches: 7 | - path: env.yaml 8 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/deployment/prod/kustomization.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: kustomize.config.k8s.io/v1beta1 2 | kind: Kustomization 3 | namespace: apps-monitor 4 | resources: 5 | - ../base 6 | patches: 7 | - path: env.yaml 8 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/deployment/tt02/kustomization.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: kustomize.config.k8s.io/v1beta1 2 | kind: Kustomization 3 | namespace: apps-monitor 4 | resources: 5 | - ../base 6 | patches: 7 | - path: env.yaml 8 | -------------------------------------------------------------------------------- /EventCreator/EventCreator/Configuration/GeneralSettings.cs: -------------------------------------------------------------------------------- 1 | namespace EventCreator.Configuration; 2 | 3 | public class GeneralSettings 4 | { 5 | public string SourceBaseAddress { get; set; } = string.Empty; 6 | } 7 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Querying/QueryType.cs: -------------------------------------------------------------------------------- 1 | namespace Altinn.Apps.Monitoring.Application; 2 | 3 | internal enum QueryType 4 | { 5 | Traces = 1, 6 | Logs = 2, 7 | Metrics = 3, 8 | } 9 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Slack/_snapshots/SlackAlerterTests.Deserialization_Of_Slack_Ok_Response_Succeeds.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | Ts: 1634160000.000100, 3 | Channel: C01UJ9G, 4 | Ok: true 5 | } -------------------------------------------------------------------------------- /AppsMonitoring/infra/debug-profile.json: -------------------------------------------------------------------------------- 1 | { 2 | "volumeMounts": [ 3 | { 4 | "mountPath": "/telemetry", 5 | "name": "telemetry", 6 | "readOnly": true 7 | } 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /RepoCleanup/Application/Models/Environment.cs: -------------------------------------------------------------------------------- 1 | namespace RepoCleanup.Application.Models 2 | { 3 | public enum Environment 4 | { 5 | Development, 6 | Staging, 7 | Production, 8 | Local 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Querying/IQueryLoader.cs: -------------------------------------------------------------------------------- 1 | namespace Altinn.Apps.Monitoring.Application; 2 | 3 | internal interface IQueryLoader 4 | { 5 | ValueTask> Load(CancellationToken cancellationToken); 6 | } 7 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @altinn/team-core 2 | AppsMonitoring/** @altinn/team-altinn-studio 3 | .github/workflows/apps-monitoring* @altinn/team-altinn-studio 4 | RepoCleanup/** @altinn/team-altinn-studio 5 | -------------------------------------------------------------------------------- /AppsMonitoring/.config/dotnet-tools.json: -------------------------------------------------------------------------------- 1 | { 2 | "version": 1, 3 | "isRoot": true, 4 | "tools": { 5 | "csharpier": { 6 | "version": "1.2.1", 7 | "commands": [ 8 | "csharpier" 9 | ], 10 | "rollForward": false 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /RepoCleanup/Infrastructure/Clients/Gitea/TrustModel.cs: -------------------------------------------------------------------------------- 1 | namespace RepoCleanup.Infrastructure.Clients.Gitea 2 | { 3 | public enum TrustModel 4 | { 5 | @default, 6 | collaborator, 7 | committer, 8 | collaboratorcommitter 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/postgres_init.sql: -------------------------------------------------------------------------------- 1 | CREATE DATABASE monitordb; 2 | \c monitordb; 3 | CREATE SCHEMA monitor; 4 | 5 | ALTER SYSTEM SET max_connections TO '200'; 6 | 7 | CREATE ROLE platform_monitoring WITH LOGIN PASSWORD 'Password'; 8 | 9 | GRANT USAGE ON SCHEMA monitor TO platform_monitoring; 10 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning", 6 | "Npgsql": "Warning" 7 | } 8 | }, 9 | "AllowedHosts": "*" 10 | } 11 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/deployment/base/service.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: v1 2 | kind: Service 3 | metadata: 4 | name: apps-monitor 5 | spec: 6 | type: ClusterIP 7 | selector: 8 | app: apps-monitor 9 | ports: 10 | - name: http 11 | port: 80 12 | protocol: TCP 13 | targetPort: http 14 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/IServiceOwnerDiscovery.cs: -------------------------------------------------------------------------------- 1 | using Altinn.Apps.Monitoring.Domain; 2 | 3 | namespace Altinn.Apps.Monitoring.Application; 4 | 5 | internal interface IServiceOwnerDiscovery 6 | { 7 | ValueTask> Discover(CancellationToken cancellationToken); 8 | } 9 | -------------------------------------------------------------------------------- /AppsMonitoring/AppsMonitoring.slnx: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/deployment/at24/env.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: apps-monitor 5 | spec: 6 | template: 7 | spec: 8 | containers: 9 | - name: apps-monitor 10 | env: 11 | - name: ASPNETCORE_ENVIRONMENT 12 | value: Staging 13 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Db/_snapshots/TelemetryEntityTests.TelemetryData_Metrics_Serialization_Roundtrip_Succeeds.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | $type: metric, 3 | name: metric-name, 4 | value: 42, 5 | attributes: { 6 | key1: value1, 7 | key2: value2 8 | }, 9 | altinn_error_id: -1 10 | } -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Db/IndexRecommendation.cs: -------------------------------------------------------------------------------- 1 | namespace Altinn.Apps.Monitoring.Application.Db; 2 | 3 | internal sealed record IndexRecommendation( 4 | string TableName, 5 | long TooMuchSeq, 6 | string Result, 7 | long TableRelSize, 8 | long SeqScan, 9 | long IndexScan 10 | ); 11 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/purge.sql: -------------------------------------------------------------------------------- 1 | DO $$ DECLARE 2 | tabname RECORD; 3 | BEGIN 4 | FOR tabname IN (SELECT tablename 5 | FROM pg_tables 6 | WHERE schemaname = 'monitor') 7 | LOOP 8 | EXECUTE 'DROP TABLE IF EXISTS monitor.' || quote_ident(tabname.tablename) || ' CASCADE'; 9 | END LOOP; 10 | END $$; 11 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Db/_snapshots/TelemetryEntityTests.TelemetryData_Logs_Serialization_Roundtrip_Succeeds.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | $type: logs, 3 | trace_id: trace-id, 4 | span_id: span-id, 5 | message: some message, 6 | attributes: { 7 | key1: value1, 8 | key2: value2 9 | }, 10 | altinn_error_id: 1 11 | } -------------------------------------------------------------------------------- /RepoCleanup/Infrastructure/Clients/Altinn2/Altinn2ReportingService.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace RepoCleanup.Infrastructure.Clients.Altinn2 4 | { 5 | public class Altinn2ReportingService 6 | { 7 | public bool RestEnabled { get; set; } 8 | 9 | public List FormsMetaData { get; set; } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /RepoCleanup/Infrastructure/Clients/Gitea/User.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json.Serialization; 2 | 3 | namespace RepoCleanup.Infrastructure.Clients.Gitea 4 | { 5 | public class User 6 | { 7 | [JsonPropertyName("id")] 8 | public int Id { get; set; } 9 | 10 | [JsonPropertyName("username")] 11 | public string Username { get; set; } 12 | 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /EventCreator/EventCreator/Configuration/StorageDbSettings.cs: -------------------------------------------------------------------------------- 1 | namespace Altinn.Platform.Storage.Configuration; 2 | 3 | /// 4 | /// Settings for Postgres database 5 | /// 6 | public class StorageDbSettings 7 | { 8 | /// 9 | /// Connection string for the postgres db 10 | /// 11 | public string ConnectionString { get; set; } = string.Empty; 12 | } 13 | -------------------------------------------------------------------------------- /RepoCleanup/Infrastructure/Clients/Gitea/TransferRepoOption.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json.Serialization; 2 | 3 | namespace RepoCleanup.Models 4 | { 5 | public class TransferRepoOption 6 | { 7 | public TransferRepoOption(string newOwner) 8 | { 9 | NewOwner = newOwner; 10 | } 11 | 12 | [JsonPropertyName("new_owner")] 13 | public string NewOwner { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/ModuleInitializer.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | 3 | namespace Altinn.Apps.Monitoring.Tests; 4 | 5 | internal static class ModuleInitializer 6 | { 7 | [ModuleInitializer] 8 | public static void Init() 9 | { 10 | VerifierSettings.AutoVerify(includeBuildServer: false); 11 | Verifier.UseSourceFileRelativeDirectory("_snapshots"); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /RepoCleanup/Infrastructure/Clients/Gitea/GiteaResponse.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net; 3 | 4 | namespace RepoCleanup.Infrastructure.Clients.Gitea 5 | { 6 | public class GiteaResponse 7 | { 8 | public Exception Exception { get; set; } 9 | public HttpStatusCode StatusCode { get; set; } 10 | 11 | public string ResponseMessage { get; set; } 12 | 13 | public bool Success { get; set; } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Db/QueryStateEntity.cs: -------------------------------------------------------------------------------- 1 | namespace Altinn.Apps.Monitoring.Application.Db; 2 | 3 | internal sealed record QueryStateEntity 4 | { 5 | public required long Id { get; init; } 6 | public required string ServiceOwner { get; init; } 7 | public required string Name { get; init; } 8 | public required string Hash { get; init; } 9 | public required Instant QueriedUntil { get; init; } 10 | } 11 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/deployment/tt02/env.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: apps-monitor 5 | spec: 6 | template: 7 | spec: 8 | containers: 9 | - name: apps-monitor 10 | resources: 11 | limits: 12 | memory: 1Gi 13 | requests: 14 | memory: 256Mi 15 | env: 16 | - name: ASPNETCORE_ENVIRONMENT 17 | value: Staging 18 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/deployment/prod/env.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: apps-monitor 5 | spec: 6 | template: 7 | spec: 8 | containers: 9 | - name: apps-monitor 10 | resources: 11 | limits: 12 | memory: 1Gi 13 | requests: 14 | memory: 256Mi 15 | env: 16 | - name: ASPNETCORE_ENVIRONMENT 17 | value: Production 18 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://json.schemastore.org/launchsettings.json", 3 | "profiles": { 4 | "http": { 5 | "commandName": "Project", 6 | "dotnetRunMessages": true, 7 | "launchBrowser": false, 8 | "applicationUrl": "http://localhost:5156", 9 | "environmentVariables": { 10 | "ASPNETCORE_ENVIRONMENT": "Development" 11 | } 12 | } 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/servers.json: -------------------------------------------------------------------------------- 1 | { 2 | "Servers": { 3 | "1": { 4 | "Group": "Servers", 5 | "Name": "Monitoring", 6 | "Host": "monitoring_postgres", 7 | "Port": 5432, 8 | "MaintenanceDB": "postgres", 9 | "Username": "platform_monitoring_admin", 10 | "Password": "Password", 11 | "SSLMode": "prefer", 12 | "Favorite": true 13 | } 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /RepoCleanup/Infrastructure/Clients/Gitea/File.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json.Serialization; 2 | 3 | namespace RepoCleanup.Infrastructure.Clients.Gitea 4 | { 5 | public class File 6 | { 7 | [JsonPropertyName("name")] 8 | public string Name { get; set; } 9 | 10 | [JsonPropertyName("path")] 11 | public string Path { get; set; } 12 | 13 | 14 | [JsonPropertyName("type")] 15 | public string Type { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /RepoCleanup/Infrastructure/Clients/Gitea/Team.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json.Serialization; 2 | 3 | namespace RepoCleanup.Infrastructure.Clients.Gitea 4 | { 5 | public class Team 6 | { 7 | [JsonPropertyName("id")] 8 | public int Id { get; set; } 9 | 10 | [JsonPropertyName("name")] 11 | public string Name { get; set; } 12 | 13 | [JsonPropertyName("organization")] 14 | public Organisation Organisation { get; set; } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /RepoCleanup/Infrastructure/Clients/Altinn2/Altinn2Form.cs: -------------------------------------------------------------------------------- 1 | namespace RepoCleanup.Infrastructure.Clients.Altinn2 2 | { 3 | public class Altinn2Form 4 | { 5 | public int FormID { get; set; } 6 | 7 | public string FormName { get; set; } 8 | 9 | public string DataFormatProviderType { get; set; } 10 | 11 | public string DataFormatID { get; set; } 12 | 13 | public int DataFormatVersion { get; set; } 14 | 15 | public string FormType { get; set; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /.github/workflows/assign-issues-to-projects.yml: -------------------------------------------------------------------------------- 1 | name: Auto Assign to Project 2 | 3 | on: 4 | issues: 5 | types: 6 | - opened 7 | 8 | jobs: 9 | add-to-project: 10 | name: Add issue to Team Platform project 11 | permissions: 12 | issues: write 13 | contents: read 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/add-to-project@main 17 | with: 18 | project-url: https://github.com/orgs/Altinn/projects/20 19 | github-token: ${{ secrets.ASSIGN_PROJECT_TOKEN }} -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Db/_snapshots/TelemetryEntityTests.TelemetryData_Trace_Serialization_Roundtrip_Succeeds.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | $type: trace, 3 | instance_owner_party_id: 2, 4 | instance_id: Guid_1, 5 | trace_id: trace-id, 6 | span_id: span-id, 7 | parent_span_id: parent-span-id, 8 | trace_name: trace-name, 9 | span_name: span-name, 10 | success: false, 11 | result: result, 12 | duration: 0:00:00.1, 13 | attributes: { 14 | key1: value1, 15 | key2: value2 16 | }, 17 | altinn_error_id: 1 18 | } -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/IAlerter.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Channels; 2 | using Altinn.Apps.Monitoring.Application.Db; 3 | 4 | namespace Altinn.Apps.Monitoring.Application; 5 | 6 | internal sealed record AlerterEvent 7 | { 8 | public required TelemetryEntity Item { get; init; } 9 | public required AlertEntity AlertBefore { get; init; } 10 | public required AlertEntity? AlertAfter { get; init; } 11 | } 12 | 13 | internal interface IAlerter : IApplicationService 14 | { 15 | ChannelReader Events { get; } 16 | } 17 | -------------------------------------------------------------------------------- /RepoCleanup/Application/Commands/CreateDefaultTeamsCommand.cs: -------------------------------------------------------------------------------- 1 | namespace RepoCleanup.Application.Commands 2 | { 3 | public class CreateDefaultTeamsCommand 4 | { 5 | public CreateDefaultTeamsCommand(string orgShortName) 6 | { 7 | if (string.IsNullOrEmpty(orgShortName)) 8 | { 9 | throw new System.ArgumentException($"'{nameof(orgShortName)}' cannot be null or empty.", nameof(orgShortName)); 10 | } 11 | 12 | OrgShortName = orgShortName; 13 | } 14 | 15 | public string OrgShortName { get; } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /AppsMonitoring/README.md: -------------------------------------------------------------------------------- 1 | ## AppsMonitoring 2 | 3 | Tool to monitor and mitigate certain errors occurring in apps. 4 | It's a single-replica stateful service that queries service owner telemetry and stores it in PostgreSQL. 5 | 6 | ```bash 7 | dotnet test 8 | ``` 9 | 10 | ### Log 11 | 12 | To bootstrap env 13 | ```bash 14 | cd infra 15 | # For tt02 we use our prod account 16 | az login --use-device-code 17 | # Populates PG variables 18 | . configure-env-shell.sh 19 | # Initializes KV, seeds DB 20 | ./bootstrap-env.sh 21 | ``` 22 | 23 | Now we can deploy/promote to environment through GH. 24 | -------------------------------------------------------------------------------- /EventCreator/EventCreator/Configuration/QueueStorageSettings.cs: -------------------------------------------------------------------------------- 1 | namespace EventCreator.Configuration; 2 | 3 | /// 4 | /// Configuration object used to hold settings for the queue storage. 5 | /// 6 | public class QueueStorageSettings 7 | { 8 | /// 9 | /// ConnectionString for the storage account 10 | /// 11 | public string ConnectionString { get; set; } = string.Empty; 12 | 13 | /// 14 | /// Name of the queue to push incoming events to, before persisting to db. 15 | /// 16 | public string RegistrationQueueName { get; set; } = string.Empty; 17 | } 18 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Querying/QueryLibrary.cs: -------------------------------------------------------------------------------- 1 | namespace Altinn.Apps.Monitoring.Application; 2 | 3 | internal static class QueryLibrary 4 | { 5 | // Checks for requests against the apps Roles API 6 | public const string GetRolesQuery = """ 7 | AppRequests 8 | | where TimeGenerated >= datetime('{0}') and TimeGenerated < datetime('{1}') 9 | | where Name == 'GET Authorization/GetRolesForCurrentParty [app/org]' 10 | | summarize ['Value'] = sum(ItemCount) by bin(TimeGenerated, 1d), App = AppRoleName, AppVersion, Name 11 | | order by TimeGenerated desc 12 | """; 13 | } 14 | -------------------------------------------------------------------------------- /EventCreator/EventCreator/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "GeneralSettings": { 3 | "SourceBaseAddress": "https://{0}.apps.at22.altinn.cloud" 4 | }, 5 | "StorageDbSettings": { 6 | "ConnectionString": "Host=localhost;Port=5432;Username=platform_storage;Password={0};Database=storagedb;Include Error Detail=True" 7 | }, 8 | "QueueStorageSettings": { 9 | "RegistrationQueueName": "events-registration", 10 | "ConnectionString": "DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1" 11 | } 12 | } 13 | 14 | -------------------------------------------------------------------------------- /RepoCleanup/Application/Commands/CreateRepoForOrgsCommand.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace RepoCleanup.Application.Commands 4 | { 5 | public class CreateRepoForOrgsCommand 6 | { 7 | public CreateRepoForOrgsCommand(List orgs, string repoName, bool prefixRepoNameWithOrg) 8 | { 9 | Orgs = orgs; 10 | RepoName = repoName; 11 | PrefixRepoNameWithOrg = prefixRepoNameWithOrg; 12 | } 13 | 14 | public List Orgs { get; private set; } 15 | public string RepoName { get; private set; } 16 | public bool PrefixRepoNameWithOrg { get; private set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /RepoCleanup/Application/Commands/DeleteRepoForOrgsCommand.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace RepoCleanup.Application.Commands 4 | { 5 | public class DeleteRepoForOrgsCommand 6 | { 7 | public DeleteRepoForOrgsCommand(List orgs, string repoName, bool prefixRepoNameWithOrg) 8 | { 9 | Orgs = orgs; 10 | RepoName = repoName; 11 | PrefixRepoNameWithOrg = prefixRepoNameWithOrg; 12 | } 13 | 14 | public List Orgs { get; private set; } 15 | public string RepoName { get; private set; } 16 | public bool PrefixRepoNameWithOrg { get; private set; } 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/appsettings.Development.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { 4 | "Default": "Information", 5 | "Microsoft.AspNetCore": "Warning" 6 | } 7 | }, 8 | "AppConfiguration": { 9 | "Db": { 10 | "Host": "localhost", 11 | "Username": "platform_monitoring", 12 | "Password": "Password", 13 | "Database": "monitordb" 14 | }, 15 | "DbAdmin": { 16 | "Host": "localhost", 17 | "Username": "platform_monitoring_admin", 18 | "Password": "Password", 19 | "Database": "monitordb" 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/deployment/base/kustomization.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: kustomize.config.k8s.io/v1beta1 2 | kind: Kustomization 3 | resources: 4 | - deployment.yaml 5 | - service.yaml 6 | replacements: 7 | - source: 8 | kind: Deployment 9 | fieldPath: metadata.annotations.[altinn.no/image] 10 | targets: 11 | - select: 12 | kind: Deployment 13 | fieldPaths: 14 | - spec.template.spec.containers.[name=apps-monitor].image 15 | - source: 16 | kind: Deployment 17 | fieldPath: metadata.annotations.[altinn.no/image-tag] 18 | targets: 19 | - select: 20 | kind: Deployment 21 | fieldPaths: 22 | - spec.template.spec.containers.[name=apps-monitor].env.[name=K8S_CONTAINER_IMAGE_TAG].value 23 | -------------------------------------------------------------------------------- /RepoCleanup/Utils/TeamOption.cs: -------------------------------------------------------------------------------- 1 | using RepoCleanup.Infrastructure.Clients.Gitea; 2 | 3 | namespace RepoCleanup.Utils 4 | { 5 | public static class TeamOption 6 | { 7 | public static CreateTeamOption GetCreateTeamOption(string name, string description, bool canCreateOrgRepo, Permission permission) 8 | { 9 | CreateTeamOption teamOption = new CreateTeamOption 10 | { 11 | CanCreateOrgRepo = canCreateOrgRepo, 12 | Description = description, 13 | IncludesAllRepositories = false, 14 | Name = name, 15 | Permission = permission 16 | }; 17 | 18 | return teamOption; 19 | } 20 | } 21 | } -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Db/_snapshots/IndexingTests.Db_Indexes_Are_Not_Missing_case=500-error-4-times-then-ok.verified.txt: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | TableName: telemetry, 4 | TooMuchSeq: 6, 5 | Result: Missing Index?, 6 | TableRelSize: 8192, 7 | SeqScan: 8, 8 | IndexScan: 2 9 | }, 10 | { 11 | TableName: alerts, 12 | TooMuchSeq: 5, 13 | Result: Missing Index?, 14 | TableRelSize: 8192, 15 | SeqScan: 5 16 | }, 17 | { 18 | TableName: queries, 19 | TooMuchSeq: 1, 20 | Result: Missing Index?, 21 | TableRelSize: 8192, 22 | SeqScan: 3, 23 | IndexScan: 2 24 | }, 25 | { 26 | TableName: schema_version, 27 | Result: OK, 28 | TableRelSize: 8192 29 | } 30 | ] -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Azure/AzureClients.cs: -------------------------------------------------------------------------------- 1 | using Azure.Identity; 2 | using Azure.Monitor.Query; 3 | using Azure.ResourceManager; 4 | 5 | namespace Altinn.Apps.Monitoring.Application.Azure; 6 | 7 | internal sealed record AzureClients 8 | { 9 | public ArmClient ArmClient { get; } 10 | 11 | public LogsQueryClient LogsQueryClient { get; } 12 | 13 | public static WorkloadIdentityCredential CreateCredential(IHostEnvironment env) 14 | { 15 | // return new AzureCliCredential(); 16 | return new WorkloadIdentityCredential(); 17 | } 18 | 19 | public AzureClients(IHostEnvironment env) 20 | { 21 | ArmClient = new(CreateCredential(env)); 22 | LogsQueryClient = new(CreateCredential(env)); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /RepoCleanup/Infrastructure/Clients/Gitea/Repository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text.Json.Serialization; 3 | 4 | namespace RepoCleanup.Infrastructure.Clients.Gitea 5 | { 6 | public class Repository 7 | { 8 | [JsonPropertyName("id")] 9 | public int Id { get; set; } 10 | 11 | [JsonPropertyName("name")] 12 | public string Name { get; set; } 13 | 14 | [JsonPropertyName("owner")] 15 | public User Owner { get; set; } 16 | 17 | [JsonPropertyName("created_at")] 18 | public DateTime Created { get; set; } 19 | 20 | [JsonPropertyName("updated_at")] 21 | public DateTime Updated { get; set; } 22 | 23 | [JsonPropertyName("empty")] 24 | public bool Empty { get; set; } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /RepoCleanup/Application/Commands/AddTeamToRepoCommand.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace RepoCleanup.Application.Commands 4 | { 5 | public class AddTeamToRepoCommand 6 | { 7 | public AddTeamToRepoCommand(List orgs, string repoName, bool prefixRepoNameWithOrg, string teamName) 8 | { 9 | Orgs = orgs; 10 | RepoName = repoName; 11 | PrefixRepoNameWithOrg = prefixRepoNameWithOrg; 12 | TeamName = teamName; 13 | } 14 | 15 | public List Orgs { get; private set; } 16 | public string RepoName { get; private set; } 17 | public bool PrefixRepoNameWithOrg { get; private set; } 18 | public string TeamName { get; private set; } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /EventCreator/EventCreator/Clients/CloudEventExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | 3 | using CloudNative.CloudEvents; 4 | using CloudNative.CloudEvents.SystemTextJson; 5 | 6 | namespace EventCreator.Clients; 7 | 8 | /// 9 | /// Extension methods for cloud events 10 | /// 11 | public static class CloudEventExtensions 12 | { 13 | /// 14 | /// Serializes the cloud event using a JsonEventFormatter 15 | /// 16 | /// The json serialized cloud event 17 | public static string Serialize(this CloudEvent cloudEvent) 18 | { 19 | CloudEventFormatter formatter = new JsonEventFormatter(); 20 | var bytes = formatter.EncodeStructuredModeMessage(cloudEvent, out _); 21 | return Encoding.UTF8.GetString(bytes.Span); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /RepoCleanup/Infrastructure/Clients/Gitea/Organisation.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json.Serialization; 2 | 3 | namespace RepoCleanup.Infrastructure.Clients.Gitea 4 | { 5 | public class Organisation 6 | { 7 | [JsonPropertyName("id")] 8 | public int Id { get; set; } 9 | 10 | [JsonPropertyName("username")] 11 | public string Username { get; set; } 12 | 13 | [JsonPropertyName("full_name")] 14 | public string Fullname { get; set; } 15 | 16 | [JsonPropertyName("website")] 17 | public string Website { get; set; } 18 | 19 | [JsonPropertyName("visibility")] 20 | public string Visibility { get; set; } 21 | 22 | [JsonPropertyName("repo_admin_change_team_access")] 23 | public bool RepoAdminChangeTeamAccess { get; set; } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/proper_at24.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: source.toolkit.fluxcd.io/v1 2 | kind: GitRepository 3 | metadata: 4 | name: altinn-tools 5 | namespace: apps-monitor 6 | spec: 7 | interval: 5m 8 | url: https://github.com/martinothamar/altinn-tools 9 | ref: 10 | branch: feat/apps-monitoring 11 | --- 12 | apiVersion: kustomize.toolkit.fluxcd.io/v1 13 | kind: Kustomization 14 | metadata: 15 | name: altinn-apps-monitor-config 16 | namespace: apps-monitor 17 | spec: 18 | interval: 10m 19 | targetNamespace: apps-monitor 20 | sourceRef: 21 | kind: GitRepository 22 | name: altinn-tools 23 | path: "./AppsMonitoring/infra/deployment/at24" 24 | prune: true 25 | timeout: 1m 26 | secretGenerator: 27 | - name: apps-monitor-appsettings 28 | disableNameSuffixHash: true 29 | files: 30 | - appsettings.at24.json 31 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Db/Config.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json; 2 | using System.Text.Json.Serialization; 3 | using NodaTime.Serialization.SystemTextJson; 4 | 5 | namespace Altinn.Apps.Monitoring.Application.Db; 6 | 7 | internal static class Config 8 | { 9 | internal const string UserMode = "user"; 10 | internal const string AdminMode = "admin"; 11 | 12 | internal static JsonSerializerOptions JsonOptions { get; } 13 | 14 | static Config() 15 | { 16 | JsonOptions = new() 17 | { 18 | PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower, 19 | AllowOutOfOrderMetadataProperties = true, 20 | Converters = { new JsonStringEnumConverter() }, 21 | }; 22 | JsonOptions = JsonOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /RepoCleanup/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "LogLevel": { // No provider, LogLevel applies to all the enabled providers. 4 | "Default": "Error", 5 | "Microsoft": "Warning", 6 | "Microsoft.Hosting.Lifetime": "Warning" 7 | }, 8 | "Debug": { // Debug provider. 9 | "LogLevel": { 10 | "Default": "Information" // Overrides preceding LogLevel:Default setting. 11 | } 12 | }, 13 | "Console": { 14 | "FormatterName": "Simple", 15 | "IncludeScopes": true, 16 | "LogLevel": { 17 | "Microsoft.AspNetCore.Mvc.Razor.Internal": "Warning", 18 | "Microsoft.AspNetCore.Mvc.Razor.Razor": "Debug", 19 | "Microsoft.AspNetCore.Mvc.Razor": "Error", 20 | "Default": "Warning" 21 | }, 22 | "FormatterOptions": { 23 | "TimestampFormat": "hh:mm:ss" 24 | } 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /RepoCleanup/Infrastructure/Clients/Altinn2/Altinn2Service.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace RepoCleanup.Infrastructure.Clients.Altinn2 5 | { 6 | public class Altinn2Service 7 | { 8 | public string ServiceOwnerCode { get; set; } 9 | 10 | public string OrganizationNumber { get; set; } 11 | 12 | public string ServiceOwnerName { get; set; } 13 | 14 | public string ServiceName { get; set; } 15 | 16 | public string ServiceCode { get; set; } 17 | 18 | public int ServiceEditionCode { get; set; } 19 | 20 | public DateTime ValidFrom { get; set; } 21 | 22 | public DateTime ValidTo { get; set; } 23 | 24 | public string ServiceType { get; set; } 25 | 26 | public bool EnterpriseUserEnabled { get; set; } 27 | 28 | public List Forms { get; set; } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /RepoCleanup/Application/Commands/MigrateAltinn2FormSchemasCommand.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace RepoCleanup.Application.Commands 4 | { 5 | public class MigrateAltinn2FormSchemasCommand 6 | { 7 | public MigrateAltinn2FormSchemasCommand(List organisations, string workPath, bool dryRun = true) 8 | { 9 | Organisations = organisations; 10 | WorkPath = workPath; 11 | DryRun = dryRun; 12 | } 13 | 14 | public List Organisations { get; private set; } 15 | 16 | public string WorkPath { get; private set; } 17 | 18 | /// 19 | /// If set to true the command won't commit and push changes to remote Altinn 2 repos, 20 | /// just leave the files in local working folder. 21 | /// 22 | public bool DryRun { get; private set; } 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /AppsMonitoring/Directory.Build.props: -------------------------------------------------------------------------------- 1 | 2 | 3 | true 4 | 5 | 6 | 7 | latest 8 | true 9 | All 10 | strict 11 | true 12 | all 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | all 21 | runtime; build; native; contentfiles; analyzers 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/flux-push.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Runs flux push 4 | # Needs to be able to log into ACR: 5 | # az acr login --name altinncr 6 | # ./flus-push.sh 7 | 8 | f() { 9 | # Function is executed in subshell to avoid changing working directory 10 | cd deployment/ 11 | 12 | flux push artifact oci://altinncr.azurecr.io/apps-monitor/configs:$(git rev-parse --short HEAD) \ 13 | --provider=generic \ 14 | --reproducible \ 15 | --path="." \ 16 | --source="$(git config --get remote.origin.url)" \ 17 | --revision="$(git branch --show-current)/$(git rev-parse HEAD)" 18 | flux tag artifact oci://altinncr.azurecr.io/apps-monitor/configs:$(git rev-parse --short HEAD) \ 19 | --provider=generic \ 20 | --tag at24 21 | 22 | # The source is created in Terraform, we just refer to it here 23 | flux reconcile source oci -n apps-monitor apps-monitor 24 | } 25 | 26 | (set -e; f) 27 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mcr.microsoft.com/dotnet/sdk:10.0-noble@sha256:d1823fecac3689a2eb959e02ee3bfe1c2142392808240039097ad70644566190 AS build 2 | WORKDIR /source 3 | ENV CI=true 4 | 5 | COPY ./Directory.Build.props ./ 6 | COPY ./.csharpierrc.yaml ./ 7 | COPY ./.csharpierignore ./ 8 | COPY ./.editorconfig ./ 9 | COPY ./src/Altinn.Apps.Monitoring/Altinn.Apps.Monitoring.csproj ./src/Altinn.Apps.Monitoring/Altinn.Apps.Monitoring.csproj 10 | WORKDIR /source/src/Altinn.Apps.Monitoring/ 11 | RUN dotnet restore 12 | 13 | COPY src/Altinn.Apps.Monitoring/. ./ 14 | RUN dotnet publish -c Release -o /app --no-restore 15 | RUN echo "{\"LogDirectory\":\"/telemetry\",\"FileSize\":32768,\"LogLevel\":\"Warning\"}" > /app/OTEL_DIAGNOSTICS.json 16 | 17 | FROM mcr.microsoft.com/dotnet/aspnet:10.0-noble-chiseled-extra@sha256:9f1788f0dc1f3db11143a80b2b68b7612bf64bd23f367801bcbbf1861cce06c0 18 | WORKDIR /app 19 | COPY --from=build --chown=$APP_UID:$APP_UID /app ./ 20 | USER $APP_UID 21 | ENTRYPOINT ["./Altinn.Apps.Monitoring"] 22 | -------------------------------------------------------------------------------- /.github/workflows/apps-monitoring-pr-build.yml: -------------------------------------------------------------------------------- 1 | name: Apps.Monitoring PR build 2 | permissions: 3 | contents: read 4 | 5 | on: 6 | pull_request: 7 | paths: 8 | - AppsMonitoring/** 9 | 10 | jobs: 11 | build: 12 | 13 | runs-on: ubuntu-latest 14 | defaults: 15 | run: 16 | working-directory: ./AppsMonitoring 17 | 18 | steps: 19 | - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 20 | with: 21 | fetch-depth: 0 22 | 23 | - name: Setup dotnet '10.0.x' 24 | uses: actions/setup-dotnet@2016bd2012dba4e32de620c46fe006a3ac9f0602 # v5.0.1 25 | with: 26 | dotnet-version: '10.0.x' 27 | 28 | - name: Display dotnet version 29 | run: dotnet --version 30 | 31 | - name: Restore 32 | run: dotnet restore 33 | 34 | - name: Build 35 | run: dotnet build -c Release --no-restore 36 | 37 | - name: Test 38 | run: dotnet test -c Release --no-restore --no-build --logger "console;verbosity=detailed" 39 | -------------------------------------------------------------------------------- /AppsMonitoring/Makefile: -------------------------------------------------------------------------------- 1 | 2 | .PHONY: build 3 | build: 4 | dotnet build 5 | 6 | .PHONY: test 7 | test: 8 | dotnet test --logger "console;verbosity=detailed" 9 | 10 | build-image: 11 | docker build -t altinncr.azurecr.io/apps-monitor/image:latest -f src/Altinn.Apps.Monitoring/Dockerfile . 12 | 13 | run-image: build-image 14 | docker run --rm altinncr.azurecr.io/apps-monitor/image:latest 15 | 16 | scan-image: 17 | trivy image altinncr.azurecr.io/apps-monitor/image:latest 18 | 19 | dive-image: 20 | dive altinncr.azurecr.io/apps-monitor/image:latest 21 | 22 | acr-login: 23 | az acr login --name altinncr 24 | 25 | push-image: build-image 26 | docker push altinncr.azurecr.io/apps-monitor/image:latest 27 | 28 | build-manifests: 29 | kubectl kustomize infra/deployment/at24 > infra/deployment/at24/result.yaml 30 | 31 | debug-container: 32 | $(eval POD := $(shell kubectl get po -o name --no-headers=true -l app=apps-monitor -n apps-monitor)) 33 | KUBECTL_DEBUG_CUSTOM_PROFILE=true kubectl debug --custom infra/debug-profile.json $(POD) -n apps-monitor -it --image=nicolaka/netshoot 34 | -------------------------------------------------------------------------------- /RepoCleanup/RepoCleanup.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net9.0 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | PreserveNewest 20 | 21 | 22 | PreserveNewest 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /EventCreator/EventCreator.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.12.35707.178 d17.12 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EventCreator", "EventCreator\EventCreator.csproj", "{E5BDEE35-DA49-4F6A-B47F-F59CF4835FDC}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {E5BDEE35-DA49-4F6A-B47F-F59CF4835FDC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {E5BDEE35-DA49-4F6A-B47F-F59CF4835FDC}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {E5BDEE35-DA49-4F6A-B47F-F59CF4835FDC}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {E5BDEE35-DA49-4F6A-B47F-F59CF4835FDC}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | EndGlobal 23 | -------------------------------------------------------------------------------- /RepoCleanup/Application/CommandHandlers/CreateOrgCommandHandler.cs: -------------------------------------------------------------------------------- 1 | using RepoCleanup.Application.Commands; 2 | using RepoCleanup.Infrastructure.Clients.Gitea; 3 | using System.Threading.Tasks; 4 | 5 | namespace RepoCleanup.Application.CommandHandlers 6 | { 7 | public class CreateOrgCommandHandler 8 | { 9 | private readonly GiteaService _giteaService; 10 | public CreateOrgCommandHandler(GiteaService giteaService) 11 | { 12 | _giteaService = giteaService; 13 | } 14 | 15 | public async Task Handle(CreateOrgCommand command) 16 | { 17 | GiteaResponse response = await _giteaService.CreateOrg( 18 | new Organisation() 19 | { 20 | Username = command.ShortName, 21 | Fullname = command.FullName, 22 | Website = command.Website, 23 | Visibility = "public", 24 | RepoAdminChangeTeamAccess = false 25 | }); 26 | 27 | return response.Success; 28 | } 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Telemetry.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics; 2 | 3 | namespace Altinn.Apps.Monitoring.Application; 4 | 5 | internal static class TelemetryExtensions 6 | { 7 | public static Activity? StartRootActivity(this ActivitySource source, string name) 8 | { 9 | var previous = Activity.Current; 10 | Activity.Current = null; 11 | var activity = source.StartActivity(name, default, parentContext: default); 12 | if (previous is not null) 13 | activity?.AddLink(new(previous.Context)); 14 | if (activity is not null) 15 | previous?.AddLink(new(activity.Context)); 16 | return activity; 17 | } 18 | } 19 | 20 | internal sealed class Telemetry : IDisposable 21 | { 22 | public const string ActivitySourceName = "Altinn.Apps.Monitoring"; 23 | 24 | public Telemetry() 25 | { 26 | Activities = new ActivitySource(ActivitySourceName); 27 | } 28 | 29 | public ActivitySource Activities { get; } 30 | 31 | public void Dispose() 32 | { 33 | Activities.Dispose(); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Altinn 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/reset-env.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Used to reset an environment by 4 | # * Updating the seed table from data.db 5 | # * Removing all tables and content that is migrated from the app 6 | # Prereqs: 7 | # * Azure CLI 8 | # * Export PGxxxx variables for psql to connect to corresponding db (see Settings -> Connect in Azure portal) 9 | # * Creds can be found in KV 10 | # * kubectl and flux CLI with current context set to env matching the PG variables 11 | # Ex: ./reset-env.sh 12 | 13 | set -e 14 | 15 | psql << EOF 16 | CREATE TABLE IF NOT EXISTS seed( 17 | id integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, 18 | data bytea NOT NULL 19 | ); 20 | EOF 21 | 22 | psql << EOF 23 | \lo_import 'data.db' 24 | SELECT :LASTOID AS lo_oid \gset 25 | INSERT INTO seed (data) VALUES (lo_get(:lo_oid)); 26 | SELECT lo_unlink(:lo_oid); 27 | EOF 28 | 29 | flux suspend ks -n apps-monitor apps-monitor 30 | kubectl scale deploy -n apps-monitor --replicas=0 apps-monitor 31 | sleep 10 # Wait for app to shutdown 32 | 33 | psql -f purge.sql 34 | 35 | flux resume ks -n apps-monitor apps-monitor 36 | flux reconcile ks -n apps-monitor apps-monitor 37 | -------------------------------------------------------------------------------- /RepoCleanup/Infrastructure/Clients/Gitea/CreateTeamOption.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Text.Json.Serialization; 3 | 4 | namespace RepoCleanup.Infrastructure.Clients.Gitea 5 | { 6 | public enum Permission 7 | { 8 | read, 9 | write, 10 | admin 11 | } 12 | 13 | public class CreateTeamOption 14 | { 15 | [JsonPropertyName("can_create_org_repo")] 16 | public bool CanCreateOrgRepo { get; set; } 17 | 18 | [JsonPropertyName("description")] 19 | public string Description { get; set; } 20 | 21 | [JsonPropertyName("includes_all_repositories")] 22 | public bool IncludesAllRepositories { get; set; } = true; 23 | 24 | [JsonPropertyName("name")] 25 | public string Name { get; set; } 26 | 27 | [JsonPropertyName("permission")] 28 | public Permission Permission { get; set; } 29 | 30 | [JsonPropertyName("units")] 31 | public List Units { get; set; } = new List { "repo.code", "repo.issues", "repo.ext_issues", "repo.wiki", "repo.pulls", "repo.releases", "repo.ext_wiki" }; 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/IServiceOwnerTelemetryAdapters.cs: -------------------------------------------------------------------------------- 1 | using Altinn.Apps.Monitoring.Application.Db; 2 | using Altinn.Apps.Monitoring.Domain; 3 | 4 | namespace Altinn.Apps.Monitoring.Application; 5 | 6 | internal interface IServiceOwnerLogsAdapter 7 | { 8 | ValueTask>> Query( 9 | ServiceOwner serviceOwner, 10 | Query query, 11 | Instant from, 12 | Instant to, 13 | CancellationToken cancellationToken 14 | ); 15 | } 16 | 17 | internal interface IServiceOwnerTraceAdapter 18 | { 19 | ValueTask>> Query( 20 | ServiceOwner serviceOwner, 21 | Query query, 22 | Instant from, 23 | Instant to, 24 | CancellationToken cancellationToken 25 | ); 26 | } 27 | 28 | internal interface IServiceOwnerMetricsAdapter 29 | { 30 | ValueTask>> Query( 31 | ServiceOwner serviceOwner, 32 | Query query, 33 | Instant from, 34 | Instant to, 35 | CancellationToken cancellationToken 36 | ); 37 | } 38 | -------------------------------------------------------------------------------- /RepoCleanup/Application/Commands/CreateOrgCommand.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace RepoCleanup.Application.Commands 4 | { 5 | public class CreateOrgCommand 6 | { 7 | public CreateOrgCommand(string shortName, string fullName, string website) 8 | { 9 | if (string.IsNullOrEmpty(shortName)) 10 | { 11 | throw new ArgumentException($"'{nameof(shortName)}' cannot be null or empty.", nameof(shortName)); 12 | } 13 | 14 | if (string.IsNullOrEmpty(fullName)) 15 | { 16 | throw new ArgumentException($"'{nameof(fullName)}' cannot be null or empty.", nameof(fullName)); 17 | } 18 | 19 | if (string.IsNullOrEmpty(website)) 20 | { 21 | throw new ArgumentException($"'{nameof(website)}' cannot be null or empty.", nameof(website)); 22 | } 23 | ShortName = shortName; 24 | FullName = fullName; 25 | Website = website; 26 | } 27 | 28 | public string ShortName { get; } 29 | public string FullName { get; } 30 | public string Website { get; } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/bootstrap-env.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Used to initialize an environment 4 | # Prereqs: 5 | # * Azure CLI - logged in using normal account for at24, prod account for tt02 & prod 6 | # * Configured shell from `configure-env-shell.sh` 7 | # Ex: ./bootstrap-env.sh 8 | 9 | set -e 10 | 11 | echo "Vault: $1" 12 | valid_environments=("at24" "tt02" "prod") 13 | 14 | altinnenv=$(echo "$1" | awk -F'-' '{print $3}') 15 | 16 | if [[ " ${valid_environments[@]} " =~ " $altinnenv " ]]; then 17 | echo "Valid environment: $altinnenv" 18 | else 19 | echo "Invalid environment: $altinnenv" 20 | exit 1 21 | fi 22 | az keyvault secret set --vault-name "$1" --name AppConfiguration--DisableSlackAlerts --value true 23 | az keyvault secret set --vault-name "$1" --name AppConfiguration--AltinnEnvironment --value "$altinnenv" 24 | 25 | psql << EOF 26 | CREATE TABLE IF NOT EXISTS seed( 27 | id integer PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY, 28 | data bytea NOT NULL 29 | ); 30 | EOF 31 | 32 | psql << EOF 33 | \lo_import 'data.db' 34 | SELECT :LASTOID AS lo_oid \gset 35 | INSERT INTO seed (data) VALUES (lo_get(:lo_oid)); 36 | SELECT lo_unlink(:lo_oid); 37 | EOF 38 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # General setting that applies Git's binary detection for file-types not specified below 2 | # Meaning, for 'text-guessed' files: 3 | # use normalization (convert crlf -> lf on commit, i.e. use `text` setting) 4 | # & do unspecified diff behavior (if file content is recognized as text & filesize < core.bigFileThreshold, do text diff on file changes) 5 | * text=auto 6 | 7 | 8 | # Override with explicit specific settings for known and/or likely text files in our repo that should be normalized 9 | # where diff{=optional_pattern} means "do text diff {with specific text pattern} and -diff means "don't do text diffs". 10 | # Unspecified diff behavior is decribed above 11 | *.cer text -diff 12 | *.cmd text 13 | *.cs text diff=csharp 14 | *.csproj text 15 | *.css text diff=css 16 | Dockerfile text 17 | *.json text 18 | *.md text diff=markdown 19 | *.msbuild text 20 | *.pem text -diff 21 | *.ps1 text 22 | *.sln text 23 | *.yaml text 24 | *.yml text 25 | 26 | # Files that should be treated as binary ('binary' is a macro for '-text -diff', i.e. "don't normalize or do text diff on content") 27 | *.jpeg binary 28 | *.pfx binary 29 | *.png binary -------------------------------------------------------------------------------- /RepoCleanup/RepoCleanup.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.30611.23 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RepoCleanup", "RepoCleanup.csproj", "{A0B1844C-C5CF-490B-BAF8-ED6CF3335938}" 7 | EndProject 8 | Global 9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 10 | Debug|Any CPU = Debug|Any CPU 11 | Release|Any CPU = Release|Any CPU 12 | EndGlobalSection 13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 14 | {A0B1844C-C5CF-490B-BAF8-ED6CF3335938}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 15 | {A0B1844C-C5CF-490B-BAF8-ED6CF3335938}.Debug|Any CPU.Build.0 = Debug|Any CPU 16 | {A0B1844C-C5CF-490B-BAF8-ED6CF3335938}.Release|Any CPU.ActiveCfg = Release|Any CPU 17 | {A0B1844C-C5CF-490B-BAF8-ED6CF3335938}.Release|Any CPU.Build.0 = Release|Any CPU 18 | EndGlobalSection 19 | GlobalSection(SolutionProperties) = preSolution 20 | HideSolutionNode = FALSE 21 | EndGlobalSection 22 | GlobalSection(ExtensibilityGlobals) = postSolution 23 | SolutionGuid = {12647254-2698-40CA-A94B-A56DD6FAAD55} 24 | EndGlobalSection 25 | EndGlobal 26 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/configure-env-shell.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | # Used to initialize the shell with PG variables 4 | # Prereqs: 5 | # * Azure CLI - logged in using normal account for at24, prod account for tt02 & prod 6 | # Ex: . configure-env-shell.sh 7 | 8 | set -e 9 | 10 | echo "Vault: $1" 11 | valid_environments=("at24" "tt02" "prod") 12 | 13 | altinnenv=$(echo "$1" | awk -F'-' '{print $3}') 14 | 15 | if [[ " ${valid_environments[@]} " =~ " $altinnenv " ]]; then 16 | echo "Valid environment: $altinnenv" 17 | else 18 | echo "Invalid environment: $altinnenv" 19 | exit 1 20 | fi 21 | 22 | host=$(az keyvault secret show --vault-name "$1" --name AppConfiguration--DbAdmin--Host --query "value" -o tsv) 23 | user=$(az keyvault secret show --vault-name "$1" --name AppConfiguration--DbAdmin--Username --query "value" -o tsv) 24 | db=$(az keyvault secret show --vault-name "$1" --name AppConfiguration--DbAdmin--Database --query "value" -o tsv) 25 | pw=$(az keyvault secret show --vault-name "$1" --name AppConfiguration--DbAdmin--Password --query "value" -o tsv) 26 | 27 | export PGHOST="$host" 28 | export PGUSER="$user" 29 | export PGPORT=5432 30 | export PGDATABASE="$db" 31 | export PGPASSWORD="$pw" 32 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Querying/Query.cs: -------------------------------------------------------------------------------- 1 | using System.Globalization; 2 | using System.IO.Hashing; 3 | using System.Text; 4 | 5 | namespace Altinn.Apps.Monitoring.Application; 6 | 7 | #pragma warning disable CA1724 // Type name conflicts with namespace name 8 | internal sealed record Query 9 | #pragma warning restore CA1724 // Type name conflicts with namespace name 10 | { 11 | public string Name { get; } 12 | public QueryType Type { get; } 13 | public string QueryTemplate { get; } 14 | public string Hash { get; } 15 | 16 | public string Format(Instant searchFrom, Instant searchTo) => 17 | // Default instant string format is ISO 8601 18 | string.Format(CultureInfo.InvariantCulture, QueryTemplate, searchFrom.ToString(), searchTo.ToString()); 19 | 20 | public Query(string name, QueryType type, string queryTemplate) 21 | { 22 | Name = name; 23 | Type = type; 24 | QueryTemplate = queryTemplate; 25 | Hash = HashQuery(queryTemplate); 26 | } 27 | 28 | private static string HashQuery(string queryTemplate) 29 | { 30 | var hash = XxHash128.Hash(Encoding.UTF8.GetBytes(queryTemplate)); 31 | return Convert.ToBase64String(hash); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Db/SeederTests.cs: -------------------------------------------------------------------------------- 1 | using Altinn.Apps.Monitoring.Application; 2 | 3 | namespace Altinn.Apps.Monitoring.Tests.Application.Db; 4 | 5 | public class SeederTests 6 | { 7 | [Fact] 8 | public async Task Seeds_Db_Successfully() 9 | { 10 | var cancellationToken = TestContext.Current.CancellationToken; 11 | 12 | await using var fixture = await HostFixture.Create( 13 | (services, _) => 14 | { 15 | services.Configure(config => 16 | { 17 | config.DisableSeeder = false; 18 | }); 19 | } 20 | ); 21 | 22 | using var _ = await fixture.Start(cancellationToken); 23 | 24 | var seeder = fixture.Seeder; 25 | var repository = fixture.Repository; 26 | 27 | await seeder.Completion; 28 | 29 | var telemetry = await repository.ListTelemetry(cancellationToken: cancellationToken); 30 | var queryStates = await repository.ListQueryStates(cancellationToken: cancellationToken); 31 | 32 | await Verify(new { Telemetry = telemetry, QueryStates = queryStates }) 33 | .DontScrubDateTimes() 34 | .DontIgnoreEmptyCollections(); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /RepoCleanup/Infrastructure/Clients/Gitea/CreateRepoOption.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json.Serialization; 2 | 3 | namespace RepoCleanup.Infrastructure.Clients.Gitea 4 | { 5 | public class CreateRepoOption 6 | { 7 | [JsonPropertyName("auto_init")] 8 | public bool AutoInit { get; set; } 9 | 10 | [JsonPropertyName("default_branch")] 11 | public string DefaultBranch { get; set; } 12 | 13 | [JsonPropertyName("description")] 14 | public string Description { get; set; } 15 | 16 | [JsonPropertyName("gitignores")] 17 | public string GitIgnores { get; set; } 18 | 19 | [JsonPropertyName("issue_labels")] 20 | public string IssueLabels { get; set; } 21 | 22 | [JsonPropertyName("license")] 23 | public string License { get; set; } 24 | 25 | [JsonPropertyName("name")] 26 | public string Name { get; set; } 27 | 28 | [JsonPropertyName("private")] 29 | public bool Private { get; set; } 30 | 31 | [JsonPropertyName("readme")] 32 | public string ReadMe { get; set; } 33 | 34 | [JsonPropertyName("template")] 35 | public bool Template { get; set; } 36 | 37 | [JsonPropertyName("trust_model")] 38 | public TrustModel TrustModel { get; set; } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /EventCreator/EventCreator/Clients/PgClient.cs: -------------------------------------------------------------------------------- 1 | using System.Data; 2 | 3 | using Altinn.Platform.Storage.Interface.Models; 4 | 5 | using Npgsql; 6 | using NpgsqlTypes; 7 | 8 | namespace EventCreator.Clients; 9 | 10 | public class PgClient 11 | { 12 | private readonly string _readSqlNoElements = "select * from storage.readinstancenoelements ($1)"; 13 | 14 | private readonly NpgsqlDataSource _dataSource; 15 | 16 | public PgClient(string _pgConnectionString) 17 | { 18 | var dataSourceBuilder = new NpgsqlDataSourceBuilder(_pgConnectionString); 19 | dataSourceBuilder.EnableDynamicJson(); 20 | _dataSource = dataSourceBuilder.Build(); 21 | } 22 | 23 | public async Task GetOne(Guid instanceGuid) 24 | { 25 | Instance? instance = null; 26 | 27 | await using NpgsqlCommand pgcom = _dataSource.CreateCommand(_readSqlNoElements); 28 | 29 | pgcom.Parameters.AddWithValue(NpgsqlDbType.Uuid, instanceGuid); 30 | 31 | await using (NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync()) 32 | { 33 | while (await reader.ReadAsync()) 34 | { 35 | instance = await reader.GetFieldValueAsync("instance"); 36 | } 37 | } 38 | 39 | return instance; 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /RepoCleanup/Functions/CreateRepoFunction.cs: -------------------------------------------------------------------------------- 1 | using RepoCleanup.Application.CommandHandlers; 2 | using RepoCleanup.Application.Commands; 3 | using RepoCleanup.Infrastructure.Clients.Gitea; 4 | using System; 5 | using System.Threading.Tasks; 6 | 7 | namespace RepoCleanup.Functions 8 | { 9 | public static class CreateRepoFunction 10 | { 11 | public async static Task Run() 12 | { 13 | SharedFunctionSnippets.WriteHeader("Create new repository for organisation(s)"); 14 | 15 | var orgs = await SharedFunctionSnippets.CollectExistingOrgsInfo(); 16 | var prefixRepoNameWithOrg = SharedFunctionSnippets.ShouldRepoNameBePrefixedWithOrg(); 17 | var repoName = SharedFunctionSnippets.CollectRepoName(); 18 | 19 | SharedFunctionSnippets.ConfirmWithExit($"You are about to create a new repository for {orgs.Count} organisation(s). Proceed?", "Aborting, no repositories created."); 20 | 21 | var command = new CreateRepoForOrgsCommand(orgs, repoName, prefixRepoNameWithOrg); 22 | var commandHander = new CreateRepoForOrgsCommandHandler(new GiteaService()); 23 | var result = await commandHander.Handle(command); 24 | 25 | Console.WriteLine($"Created {result} repositories."); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /RepoCleanup/Functions/DeleteDatamodelsRepoFunction.cs: -------------------------------------------------------------------------------- 1 | using RepoCleanup.Application.CommandHandlers; 2 | using RepoCleanup.Application.Commands; 3 | using RepoCleanup.Infrastructure.Clients.Gitea; 4 | using System; 5 | using System.Threading.Tasks; 6 | 7 | namespace RepoCleanup.Functions 8 | { 9 | public static class DeleteDatamodelsRepoFunction 10 | { 11 | public static async Task Run() 12 | { 13 | SharedFunctionSnippets.WriteHeader("Delete datamodels repo for all oranisations"); 14 | 15 | var orgs = await SharedFunctionSnippets.CollectExistingOrgsInfo(); 16 | var prefixRepoNameWithOrg = SharedFunctionSnippets.ShouldRepoNameBePrefixedWithOrg(); 17 | var repoName = SharedFunctionSnippets.CollectRepoName(); 18 | 19 | SharedFunctionSnippets.ConfirmWithExit($"You are about to delete repository {repoName} for {orgs.Count} organisation(s). Proceed?", "Aborting, no repositories deleted."); 20 | 21 | var command = new DeleteRepoForOrgsCommand(orgs, repoName, prefixRepoNameWithOrg); 22 | var commandHander = new DeleteRepoForOrgsCommandHandler(new GiteaService()); 23 | var result = await commandHander.Handle(command); 24 | 25 | Console.WriteLine($"Deleted {result} repositories."); 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/AppConfiguration.cs: -------------------------------------------------------------------------------- 1 | namespace Altinn.Apps.Monitoring.Application; 2 | 3 | internal sealed class AppConfiguration 4 | { 5 | public TimeSpan PollInterval { get; set; } = TimeSpan.FromMinutes(10); 6 | public int SearchFromDays { get; set; } = 90; 7 | 8 | public string SlackHost { get; set; } = "https://slack.com"; 9 | public string SlackAccessToken { get; set; } = null!; 10 | public string SlackChannel { get; set; } = null!; 11 | 12 | public string AltinnEnvironment { get; set; } = "at24"; 13 | 14 | public DbConfiguration Db { get; set; } = null!; 15 | public DbConfiguration DbAdmin { get; set; } = null!; 16 | 17 | public string KeyVaultUri { get; set; } = null!; 18 | 19 | public bool DisableOrchestrator { get; set; } 20 | public bool DisableSeeder { get; set; } 21 | public bool DisableAlerter { get; set; } 22 | public bool DisableSlackAlerts { get; set; } 23 | 24 | internal TaskCompletionSource? OrchestratorStartSignal { get; set; } 25 | } 26 | 27 | internal sealed class DbConfiguration 28 | { 29 | public string Host { get; set; } = null!; 30 | public string Username { get; set; } = null!; 31 | public string Password { get; set; } = null!; 32 | public string Database { get; set; } = null!; 33 | public int Port { get; set; } = 5432; 34 | } 35 | -------------------------------------------------------------------------------- /AppsMonitoring/docker-compose.yml: -------------------------------------------------------------------------------- 1 | services: 2 | altinn_apps_monitoring_postgres: 3 | image: 'postgres:16@sha256:d4c3314b2dd74ff23cd6cd07e4d7e737cb6ef791c22a7cbd744cfbfb4815df72' 4 | container_name: monitoring_postgres 5 | restart: unless-stopped 6 | environment: 7 | - POSTGRES_USER=platform_monitoring_admin 8 | - POSTGRES_PASSWORD=Password 9 | ports: 10 | - '5432:5432' 11 | volumes: 12 | - ./infra/postgres_init.sql:/docker-entrypoint-initdb.d/postgres_init.sql 13 | 14 | altinn_apps_monitoring_pgadmin: 15 | image: 'dpage/pgadmin4:9.10@sha256:8c128407f45f1c582eda69e71da1a393237388469052e3cc1e6ae4a475e12b70' 16 | container_name: monitoring_pgadmin 17 | restart: unless-stopped 18 | ports: 19 | - '8888:80' 20 | environment: 21 | PGADMIN_DEFAULT_EMAIL: platform_monitoring_admin@altinn.no 22 | PGADMIN_DEFAULT_PASSWORD: Password 23 | PGADMIN_CONFIG_SERVER_MODE: 'False' 24 | PGADMIN_CONFIG_MASTER_PASSWORD_REQUIRED: 'False' 25 | volumes: 26 | - ./infra/servers.json:/pgadmin4/servers.json 27 | 28 | altinn_apps_monitoring_lgtm: 29 | image: 'docker.io/grafana/otel-lgtm:0.11@sha256:15b963783b3dc24a6256ebc1bffd4fc6c133348810faf86ecaa939912fd311b6' 30 | container_name: monitoring_lgtm 31 | restart: unless-stopped 32 | ports: 33 | - '3000:3000' 34 | - '4317:4317' 35 | - '4318:4318' 36 | -------------------------------------------------------------------------------- /RepoCleanup/Application/CommandHandlers/AddTeamToRepoCommandHandler.cs: -------------------------------------------------------------------------------- 1 | using RepoCleanup.Application.Commands; 2 | using RepoCleanup.Infrastructure.Clients.Gitea; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace RepoCleanup.Application.CommandHandlers 7 | { 8 | public class AddTeamToRepoCommandHandler 9 | { 10 | private readonly GiteaService _giteaService; 11 | 12 | public AddTeamToRepoCommandHandler(GiteaService giteaService) 13 | { 14 | _giteaService = giteaService; 15 | } 16 | 17 | public async Task Handle(AddTeamToRepoCommand command) 18 | { 19 | var teamsAdded = 0; 20 | 21 | foreach(var org in command.Orgs) 22 | { 23 | var teams = await _giteaService.GetTeam(org); 24 | if (teams.FirstOrDefault(t => t.Name == command.TeamName) == null) 25 | { 26 | return teamsAdded; 27 | } 28 | 29 | var repoName = command.PrefixRepoNameWithOrg ? $"{org}-{command.RepoName}" : command.RepoName; 30 | var giteaResponse = await _giteaService.AddTeamToRepo(org, repoName, command.TeamName); 31 | if(giteaResponse.Success) 32 | { 33 | teamsAdded++; 34 | } 35 | } 36 | 37 | return teamsAdded; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /RepoCleanup/Utils/NotALogger.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using System.Text; 4 | 5 | namespace RepoCleanup.Utils 6 | { 7 | public class NotALogger 8 | { 9 | private StringBuilder _logBuilder = new StringBuilder(); 10 | 11 | private string _logFile; 12 | 13 | public NotALogger(string logFile) 14 | { 15 | _logFile = logFile; 16 | } 17 | 18 | public void AddNothing() 19 | { 20 | Console.WriteLine(); 21 | _logBuilder.AppendLine(); 22 | } 23 | 24 | public void AddInformation(string message) 25 | { 26 | message = $"{DateTime.Now} - INFO - {message}"; 27 | 28 | Console.WriteLine(message); 29 | _logBuilder.AppendLine(message); 30 | } 31 | 32 | public void AddError(Exception exception) 33 | { 34 | string message = $"{DateTime.Now} - ERRR - {exception.Message}\r\n"; 35 | message += $"{DateTime.Now} - ERRR - {exception.StackTrace}"; 36 | 37 | Console.WriteLine(message); 38 | _logBuilder.AppendLine(message); 39 | } 40 | 41 | public void WriteLog() 42 | { 43 | using (StreamWriter file = new StreamWriter(_logFile, true)) 44 | { 45 | file.WriteLine(_logBuilder.ToString()); 46 | } 47 | 48 | _logBuilder.Clear(); 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /RepoCleanup/Application/CommandHandlers/DeleteRepoForOrgsCommandHandler.cs: -------------------------------------------------------------------------------- 1 | using RepoCleanup.Application.Commands; 2 | using RepoCleanup.Infrastructure.Clients.Gitea; 3 | using RepoCleanup.Models; 4 | using System.Net; 5 | using System.Threading.Tasks; 6 | 7 | namespace RepoCleanup.Application.CommandHandlers 8 | { 9 | public class DeleteRepoForOrgsCommandHandler 10 | { 11 | private readonly GiteaService _giteaService; 12 | public DeleteRepoForOrgsCommandHandler(GiteaService giteaService) 13 | { 14 | _giteaService = giteaService; 15 | } 16 | 17 | public async Task Handle(DeleteRepoForOrgsCommand command) 18 | { 19 | var reposDeletedCounter = 0; 20 | 21 | foreach (var org in command.Orgs) 22 | { 23 | var repoName = command.PrefixRepoNameWithOrg ? $"{org}-{command.RepoName}" : command.RepoName; 24 | 25 | var giteaResponse = await _giteaService.GetRepo(org, repoName); 26 | if(giteaResponse.StatusCode == HttpStatusCode.OK) 27 | { 28 | giteaResponse = await _giteaService.DeleteRepository(org, repoName); 29 | 30 | if(giteaResponse.Success) 31 | { 32 | reposDeletedCounter++; 33 | } 34 | } 35 | } 36 | 37 | return reposDeletedCounter; 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /EventCreator/EventCreator/EventCreator.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net9.0 6 | enable 7 | enable 8 | d35fee57-7df1-4d7a-9b2d-233a12e3a09d 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | Always 28 | 29 | 30 | Always 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /RepoCleanup/Utils/StringExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | 4 | namespace RepoCleanup.Utils 5 | { 6 | /// 7 | /// Extensions to facilitate sanitization of string values 8 | /// 9 | public static class StringExtensions 10 | { 11 | /// 12 | /// Sanitize the input as a file name. 13 | /// 14 | /// The input variable to be sanitized. 15 | /// Throw exception instead of replacing invalid characters with '-'. 16 | /// A string that can be used ass a file path. 17 | public static string AsFileName(this string input, bool throwExceptionOnInvalidCharacters = true) 18 | { 19 | if (string.IsNullOrWhiteSpace(input)) 20 | { 21 | return input; 22 | } 23 | 24 | char[] illegalFileNameCharacters = System.IO.Path.GetInvalidFileNameChars(); 25 | if (throwExceptionOnInvalidCharacters) 26 | { 27 | if (illegalFileNameCharacters.Any(ic => input.Any(i => ic == i))) 28 | { 29 | throw new ArgumentOutOfRangeException(nameof(input)); 30 | } 31 | 32 | if (input == "..") 33 | { 34 | throw new ArgumentOutOfRangeException(nameof(input)); 35 | } 36 | 37 | return input; 38 | } 39 | 40 | if (input == "..") 41 | { 42 | return "-"; 43 | } 44 | 45 | return illegalFileNameCharacters.Aggregate(input, (current, c) => current.Replace(c, '-')); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Domain/ServiceOwner.cs: -------------------------------------------------------------------------------- 1 | using System.Diagnostics.CodeAnalysis; 2 | 3 | namespace Altinn.Apps.Monitoring.Domain; 4 | 5 | /// 6 | /// E.g "skd" "brg" 7 | /// 8 | internal readonly struct ServiceOwner : IEquatable 9 | { 10 | public readonly string Value { get; } 11 | public readonly string? ExtId { get; } 12 | 13 | private ServiceOwner(string value, string? extId) 14 | { 15 | Value = value; 16 | ExtId = extId; 17 | } 18 | 19 | public static ServiceOwner Parse(string serviceOwner, string? extId = null) 20 | { 21 | ArgumentException.ThrowIfNullOrWhiteSpace(serviceOwner, nameof(serviceOwner)); 22 | 23 | for (int i = 0; i < serviceOwner.Length; i++) 24 | { 25 | if (!char.IsLetter(serviceOwner[i]) || !char.IsLower(serviceOwner[i])) 26 | { 27 | throw new ArgumentException( 28 | $"Service owner must only contain lowercase letters. Got: '{serviceOwner}'", 29 | nameof(serviceOwner) 30 | ); 31 | } 32 | } 33 | 34 | return new ServiceOwner(serviceOwner, extId); 35 | } 36 | 37 | public bool Equals(ServiceOwner other) => Value.Equals(other.Value, StringComparison.Ordinal); 38 | 39 | public override bool Equals([NotNullWhen(true)] object? obj) => obj is ServiceOwner other && Equals(other); 40 | 41 | public override int GetHashCode() => Value.GetHashCode(StringComparison.Ordinal); 42 | 43 | public override string ToString() => Value; 44 | 45 | public static bool operator ==(ServiceOwner left, ServiceOwner right) => left.Equals(right); 46 | 47 | public static bool operator !=(ServiceOwner left, ServiceOwner right) => !left.Equals(right); 48 | } 49 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Db/IndexingTests.cs: -------------------------------------------------------------------------------- 1 | using Altinn.Apps.Monitoring.Application; 2 | using Altinn.Apps.Monitoring.Domain; 3 | 4 | namespace Altinn.Apps.Monitoring.Tests.Application.Db; 5 | 6 | public class IndexingTests 7 | { 8 | [Theory(Skip = "This test is not ready yet, index results not deterministic")] 9 | [InlineData(AppFixture.Slack.Cases.ServerError4TimesThenOk)] 10 | public async Task Db_Indexes_Are_Not_Missing(string @case) 11 | { 12 | ServiceOwner[] serviceOwners = [ServiceOwner.Parse("skd")]; 13 | await using var fixture = await AppFixture.Create(@case, serviceOwners); 14 | var orchestratorFixture = fixture.OrchestratorFixture; 15 | var (hostFixture, startSignal, adapterSemaphore, pollInterval, latency, queries, cancellationToken) = 16 | orchestratorFixture; 17 | 18 | var timeProvider = hostFixture.TimeProvider; 19 | var repository = hostFixture.Repository; 20 | var orchestratorResults = hostFixture.Orchestrator.Events; 21 | var alerterResults = hostFixture.Alerter.Events; 22 | 23 | List orchestratorEvents = new(); 24 | List alerterEvents = new(); 25 | // Let orchestrator start work 26 | _ = fixture.StartOrchestrator(); 27 | 28 | for (int i = 0; i < 1; i++) 29 | { 30 | await fixture.WaitForOrchestratorIteration(orchestratorEvents, cancellationToken); 31 | 32 | await fixture.WaitForAlerterIteration(alerterEvents, cancellationToken); 33 | } 34 | 35 | var indexRecommendations = await repository.ListIndexRecommendations(cancellationToken); 36 | 37 | await Verify(indexRecommendations).DontScrubDateTimes().DontIgnoreEmptyCollections().UseParameters(@case); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /.github/workflows/codeql.yml: -------------------------------------------------------------------------------- 1 | name: "CodeQL Advanced" 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | types: [opened, synchronize, reopened] 9 | schedule: 10 | - cron: '18 22 * * 3' 11 | 12 | jobs: 13 | analyze: 14 | name: Analyze 15 | runs-on: ubuntu-latest 16 | permissions: 17 | actions: read 18 | contents: read 19 | security-events: write 20 | 21 | strategy: 22 | fail-fast: false 23 | matrix: 24 | include: 25 | - language: actions 26 | build-mode: none 27 | - language: csharp 28 | build-mode: none 29 | 30 | steps: 31 | - name: Checkout repository 32 | uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 33 | - name: Setup .NET 9.0.* SDK 34 | uses: actions/setup-dotnet@2016bd2012dba4e32de620c46fe006a3ac9f0602 # v5.0.1 35 | with: 36 | dotnet-version: | 37 | 9.0.x 38 | - name: Initialize CodeQL 39 | uses: github/codeql-action/init@f47c8e6a9bd05ef3ee422fc8d8663be7fe4bdc61 # v3.31.8 40 | with: 41 | languages: ${{ matrix.language }} 42 | build-mode: ${{ matrix.build-mode }} 43 | # If you wish to specify custom queries, you can do so here or in a config file. 44 | # By default, queries listed here will override any specified in a config file. 45 | # Prefix the list here with "+" to use these queries and those in the config file. 46 | # queries: ./path/to/local/query, your-org/your-repo/queries@main 47 | - name: Autobuild 48 | uses: github/codeql-action/autobuild@f47c8e6a9bd05ef3ee422fc8d8663be7fe4bdc61 # v3.31.8 49 | 50 | - name: Perform CodeQL Analysis 51 | uses: github/codeql-action/analyze@f47c8e6a9bd05ef3ee422fc8d8663be7fe4bdc61 # v3.31.8 52 | with: 53 | category: "/language:${{matrix.language}}" 54 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Altinn.Apps.Monitoring.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net10.0 5 | enable 6 | enable 7 | false 8 | Exe 9 | CA1812 10 | 11 | 12 | 13 | 14 | Always 15 | 16 | 17 | 18 | 19 | 20 | runtime; build; native; contentfiles; analyzers; buildtransitive 21 | all 22 | 23 | 24 | 25 | 26 | runtime; build; native; contentfiles; analyzers; buildtransitive 27 | all 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /RepoCleanup/Application/CommandHandlers/CreateDefaultTeamsCommandHandler.cs: -------------------------------------------------------------------------------- 1 | using RepoCleanup.Application.Commands; 2 | using RepoCleanup.Infrastructure.Clients.Gitea; 3 | using RepoCleanup.Utils; 4 | using System.Collections.Generic; 5 | using System.Threading.Tasks; 6 | 7 | namespace RepoCleanup.Application.CommandHandlers 8 | { 9 | public class CreateDefaultTeamsCommandHandler 10 | { 11 | private readonly GiteaService _giteaService; 12 | 13 | public CreateDefaultTeamsCommandHandler(GiteaService giteaService) 14 | { 15 | _giteaService = giteaService; 16 | } 17 | 18 | public async Task Handle(CreateDefaultTeamsCommand command) 19 | { 20 | bool createdTeams = true; 21 | List teams = new List(); 22 | teams.Add(TeamOption.GetCreateTeamOption("Deploy-Production", "Members can deploy to production", false, Permission.read)); 23 | teams.Add(TeamOption.GetCreateTeamOption("Deploy-TT02", "Members can deploy to TT02", false, Permission.read)); 24 | teams.Add(TeamOption.GetCreateTeamOption("Admin-Production", "Members can administer published apps in production", false, Permission.read)); 25 | teams.Add(TeamOption.GetCreateTeamOption("Admin-TT02", "Members can administer published apps in TT02", false, Permission.read)); 26 | teams.Add(TeamOption.GetCreateTeamOption("Devs", "All application developers", true, Permission.write)); 27 | teams.Add(TeamOption.GetCreateTeamOption("Datamodels", "Team for those who can work on an organizations shared data models.", false, Permission.write)); 28 | 29 | foreach (CreateTeamOption team in teams) 30 | { 31 | GiteaResponse response = await _giteaService.CreateTeam(command.OrgShortName, team); 32 | 33 | if (!response.Success) 34 | { 35 | createdTeams = false; 36 | } 37 | } 38 | 39 | return createdTeams; 40 | } 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /RepoCleanup/Globals.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Configuration; 2 | using Microsoft.Extensions.Logging; 3 | using System.IO; 4 | using System.Net.Http; 5 | using System.Text.Json; 6 | using System.Text.Json.Serialization; 7 | 8 | namespace RepoCleanup 9 | { 10 | static class Globals 11 | { 12 | 13 | public static HttpClient Client { set; get; } 14 | 15 | public static bool IsDryRun { get; set; } = true; 16 | 17 | public static string GiteaToken { get; internal set; } 18 | 19 | public static string RepositoryBaseUrl { get; internal set; } 20 | 21 | public static readonly JsonSerializerOptions SerializerOptions = new JsonSerializerOptions 22 | { 23 | WriteIndented = true, 24 | Converters = 25 | { 26 | new JsonStringEnumConverter(JsonNamingPolicy.CamelCase) 27 | } 28 | }; 29 | 30 | public static IConfigurationRoot Configuration { get; } = 31 | ( 32 | ReadConfig() 33 | ); 34 | 35 | public static IConfigurationRoot ReadConfig() 36 | { 37 | var configBuilder = new ConfigurationBuilder(); 38 | 39 | configBuilder 40 | .SetBasePath(Directory.GetCurrentDirectory()) 41 | .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); 42 | 43 | return configBuilder.Build(); 44 | } 45 | 46 | public static ILogger CreateLogger() => LogFactory.CreateLogger(); 47 | 48 | public static ILoggerFactory LogFactory { get; } = LoggerFactory.Create(builder => 49 | { 50 | builder.ClearProviders(); 51 | builder 52 | .AddConfiguration(Configuration.GetSection("Logging")) 53 | .AddSimpleConsole(options => 54 | { 55 | options.IncludeScopes = true; 56 | options.TimestampFormat = "hh:mm:ss "; 57 | }); 58 | }); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Db/TelemetryEntityTests.cs: -------------------------------------------------------------------------------- 1 | using Altinn.Apps.Monitoring.Application.Db; 2 | 3 | namespace Altinn.Apps.Monitoring.Tests.Application.Db; 4 | 5 | public class TelemetryEntityTests 6 | { 7 | [Fact] 8 | public async Task TelemetryData_Trace_Serialization_Roundtrip_Succeeds() 9 | { 10 | var data = TestData.GenerateTelemetryTraceData(); 11 | 12 | var json = data.Serialize(); 13 | 14 | await VerifyJson(json); 15 | 16 | var deserializedData = TelemetryData.Deserialize(json); 17 | var deserializedTraceData = Assert.IsType(deserializedData); 18 | Assert.Equivalent(data, deserializedTraceData); 19 | } 20 | 21 | [Fact] 22 | public async Task TelemetryData_Logs_Serialization_Roundtrip_Succeeds() 23 | { 24 | var data = new LogsData 25 | { 26 | AltinnErrorId = 1, 27 | TraceId = "trace-id", 28 | SpanId = "span-id", 29 | Message = "some message", 30 | Attributes = new() { { "key1", "value1" }, { "key2", "value2" } }, 31 | }; 32 | 33 | var json = data.Serialize(); 34 | 35 | await VerifyJson(json); 36 | 37 | var deserializedData = TelemetryData.Deserialize(json); 38 | var deserializedLogsData = Assert.IsType(deserializedData); 39 | Assert.Equivalent(data, deserializedLogsData); 40 | } 41 | 42 | [Fact] 43 | public async Task TelemetryData_Metrics_Serialization_Roundtrip_Succeeds() 44 | { 45 | var data = new MetricData 46 | { 47 | AltinnErrorId = -1, 48 | Name = "metric-name", 49 | Value = 42, 50 | Attributes = new() { { "key1", "value1" }, { "key2", "value2" } }, 51 | }; 52 | 53 | var json = data.Serialize(); 54 | 55 | await VerifyJson(json); 56 | 57 | var deserializedData = TelemetryData.Deserialize(json); 58 | var deserializedLogsData = Assert.IsType(deserializedData); 59 | Assert.Equivalent(data, deserializedLogsData); 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Altinn.Apps.Monitoring.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net10.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | Always 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /RepoCleanup/Functions/AddTeamToRepoFunction.cs: -------------------------------------------------------------------------------- 1 | using RepoCleanup.Application.CommandHandlers; 2 | using RepoCleanup.Application.Commands; 3 | using RepoCleanup.Infrastructure.Clients.Gitea; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Threading.Tasks; 8 | 9 | namespace RepoCleanup.Functions 10 | { 11 | public static class AddTeamToRepoFunction 12 | { 13 | public async static Task Run() 14 | { 15 | SharedFunctionSnippets.WriteHeader("Add existing team to repository"); 16 | 17 | var orgs = await CollectOrgInfo(); 18 | var prefixReponame = SharedFunctionSnippets.ShouldRepoNameBePrefixedWithOrg(); 19 | var repoName = SharedFunctionSnippets.CollectRepoName(); 20 | var teamName = SharedFunctionSnippets.CollectTeamName(); 21 | 22 | SharedFunctionSnippets.ConfirmWithExit($"You are about to add team {teamName} in repo {repoName} for {orgs.Count} organisation(s). Proceed?", "Aborting, no teams added."); 23 | 24 | var command = new AddTeamToRepoCommand(orgs, repoName, prefixReponame, teamName); 25 | var commandHander = new AddTeamToRepoCommandHandler(new GiteaService()); 26 | var result = await commandHander.Handle(command); 27 | 28 | Console.WriteLine($"Added {result} teams."); 29 | } 30 | 31 | private static async Task> CollectOrgInfo() 32 | { 33 | List orgs = new List(); 34 | 35 | bool updateAllOrgs = SharedFunctionSnippets.ShouldThisApplyToAllOrgs(); 36 | 37 | if (updateAllOrgs) 38 | { 39 | List organisations = await GiteaService.GetOrganisations(); 40 | orgs.AddRange(organisations.Select(o => o.Username)); 41 | } 42 | else 43 | { 44 | Console.Write("\r\nProvide organisation name: "); 45 | 46 | string name = Console.ReadLine(); 47 | orgs.Add(name); 48 | } 49 | 50 | return orgs; 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /RepoCleanup/Application/CommandHandlers/CreateRepoForOrgsCommandHandler.cs: -------------------------------------------------------------------------------- 1 | using RepoCleanup.Application.Commands; 2 | using RepoCleanup.Infrastructure.Clients.Gitea; 3 | using System.Net; 4 | using System.Threading.Tasks; 5 | 6 | namespace RepoCleanup.Application.CommandHandlers 7 | { 8 | public class CreateRepoForOrgsCommandHandler 9 | { 10 | private readonly GiteaService _giteaService; 11 | public CreateRepoForOrgsCommandHandler(GiteaService giteaService) 12 | { 13 | _giteaService = giteaService; 14 | } 15 | 16 | public async Task Handle(CreateRepoForOrgsCommand command) 17 | { 18 | var reposCreatedCounter = 0; 19 | var authenticatedUser = await _giteaService.GetAuthenticatedUser(); 20 | 21 | foreach (var org in command.Orgs) 22 | { 23 | var repoName = command.PrefixRepoNameWithOrg ? $"{org}-{command.RepoName}" : command.RepoName; 24 | 25 | var giteaResponse = await _giteaService.GetRepo(org, repoName); 26 | if(giteaResponse.StatusCode == HttpStatusCode.NotFound) 27 | { 28 | var createRepoOption = GetCreateRepoOption(repoName); 29 | 30 | giteaResponse = await _giteaService.CreateRepo(createRepoOption); 31 | 32 | if(giteaResponse.Success) 33 | { 34 | await _giteaService.TransferRepoOwnership(authenticatedUser.Username, repoName, org); 35 | reposCreatedCounter++; 36 | } 37 | } 38 | } 39 | 40 | return reposCreatedCounter; 41 | } 42 | 43 | private CreateRepoOption GetCreateRepoOption(string repoName) 44 | { 45 | return new CreateRepoOption() 46 | { 47 | Name = repoName, 48 | AutoInit = true, 49 | DefaultBranch = "master", 50 | Private = false, 51 | TrustModel = TrustModel.@default 52 | }; 53 | 54 | } 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Db/AlertEntity.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json; 2 | using System.Text.Json.Serialization; 3 | using Altinn.Apps.Monitoring.Application.Slack; 4 | 5 | namespace Altinn.Apps.Monitoring.Application.Db; 6 | 7 | internal enum AlertState 8 | { 9 | Pending = 1, 10 | Alerted = 2, 11 | Mitigated = 3, 12 | } 13 | 14 | internal sealed record AlertEntity 15 | { 16 | public required long Id { get; init; } 17 | 18 | public required AlertState State { get; init; } 19 | 20 | public required long TelemetryId { get; init; } 21 | 22 | public required AlertData Data { get; init; } 23 | 24 | public required Instant CreatedAt { get; init; } 25 | 26 | public required Instant UpdatedAt { get; init; } 27 | } 28 | 29 | [JsonDerivedType(typeof(SlackAlerter.SlackAlertData), typeDiscriminator: Types.Slack)] 30 | internal abstract record AlertData 31 | { 32 | internal static class Types 33 | { 34 | public const string Slack = "slack"; 35 | 36 | public static readonly string[] All = typeof(Types) 37 | .GetFields() 38 | .Where(f => f.FieldType == typeof(string) && f.IsLiteral) 39 | .Select(f => f.GetRawConstantValue() as string ?? throw new Exception("Unexpected value")) 40 | .ToArray(); 41 | } 42 | 43 | public static bool TypeIsValid(string type) => Types.All.Contains(type); 44 | 45 | public bool IsType(string type) 46 | { 47 | return this switch 48 | { 49 | SlackAlerter.SlackAlertData => type == Types.Slack, 50 | _ => false, 51 | }; 52 | } 53 | 54 | public static AlertData Deserialize(string json) 55 | { 56 | return JsonSerializer.Deserialize(json, Config.JsonOptions) ?? throw new JsonException(); 57 | } 58 | 59 | public static AlertData Deserialize(byte[] json) 60 | { 61 | return JsonSerializer.Deserialize(json, Config.JsonOptions) ?? throw new JsonException(); 62 | } 63 | 64 | public string Serialize() 65 | { 66 | return JsonSerializer.Serialize(this, Config.JsonOptions); 67 | } 68 | 69 | public byte[] SerializeToUtf8Bytes() 70 | { 71 | return JsonSerializer.SerializeToUtf8Bytes(this, Config.JsonOptions); 72 | } 73 | } 74 | -------------------------------------------------------------------------------- /RepoCleanup/Functions/MigrateXsdSchemasFunction.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Threading.Tasks; 5 | 6 | using RepoCleanup.Application.Commands; 7 | using RepoCleanup.Application.CommandHandlers; 8 | using RepoCleanup.Utils; 9 | using RepoCleanup.Infrastructure.Clients.Gitea; 10 | 11 | namespace RepoCleanup.Functions 12 | { 13 | public static class MigrateXsdSchemasFunction 14 | { 15 | public static async Task Run() 16 | { 17 | NotALogger logger = new ("MigrateXsdSchemas - Log.txt"); 18 | logger.AddNothing(); 19 | 20 | SharedFunctionSnippets.WriteHeader("Migrating XSD Schemas from active services in Altinn II"); 21 | 22 | string basePath = CollectMigrationWorkFolder(); 23 | List organisations = await SharedFunctionSnippets.CollectExistingOrgsInfo(); 24 | 25 | logger.AddInformation($"Started!"); 26 | logger.AddInformation($"Using '{basePath}' as base path for all organisations"); 27 | 28 | try 29 | { 30 | MigrateAltinn2FormSchemasCommandHandler migrateAltinn2FormSchemasCommandHandler = new (new GiteaService(), logger); 31 | MigrateAltinn2FormSchemasCommand migrateAltinn2FormSchemasCommand = new (organisations, basePath); 32 | await migrateAltinn2FormSchemasCommandHandler.Handle(migrateAltinn2FormSchemasCommand); 33 | } 34 | catch (Exception exception) 35 | { 36 | logger.AddError(exception); 37 | throw; 38 | } 39 | finally 40 | { 41 | logger.WriteLog(); 42 | } 43 | } 44 | 45 | private static string CollectMigrationWorkFolder() 46 | { 47 | Console.WriteLine("This operation requires a folder to which it can clone the datamodels repositories."); 48 | string basePath = SharedFunctionSnippets.CollectInput("Provide folder name (should be empty): "); 49 | 50 | if (!Directory.Exists(basePath)) 51 | { 52 | Console.WriteLine("Can't find the specified folder!"); 53 | return CollectMigrationWorkFolder(); 54 | } 55 | 56 | return basePath; 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /.github/workflows/apps-monitoring-promote.yml: -------------------------------------------------------------------------------- 1 | name: Apps.Monitoring promote 2 | 3 | on: 4 | workflow_dispatch: 5 | inputs: 6 | from: 7 | description: "Environment to promote from" 8 | type: choice 9 | default: "at24" 10 | required: true 11 | options: 12 | - at24 13 | - tt02 14 | to: 15 | description: "Environment to promote to" 16 | type: choice 17 | default: "tt02" 18 | required: true 19 | options: 20 | - tt02 21 | - prod 22 | 23 | jobs: 24 | promote: 25 | 26 | runs-on: ubuntu-latest 27 | environment: prod 28 | permissions: 29 | id-token: write # Require write permission to Fetch an OIDC token. 30 | 31 | steps: 32 | - name: validate inputs 33 | uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0 34 | with: 35 | script: | 36 | const allowedPromotions = [ 37 | ['at24', 'tt02'], 38 | ['tt02', 'prod'] 39 | ]; 40 | const current = [ 41 | '${{ github.event.inputs.from }}', 42 | '${{ github.event.inputs.to }}' 43 | ]; 44 | const promotion = allowedPromotions.find(c => c[0] === current[0] && c[1] === current[1]); 45 | if (!promotion) { 46 | core.setFailed(`Cannot promote from: ${current[0]} to ${current[1]}`); 47 | } 48 | 49 | - name: flux install 50 | uses: fluxcd/flux2/action@8454b02a32e48d775b9f563cb51fdcb1787b5b93 # v2.7.5 51 | with: 52 | version: '2.5.1' 53 | 54 | - name: set vars 55 | id: vars 56 | run: | 57 | echo "registry=altinncr.azurecr.io" >> $GITHUB_OUTPUT 58 | 59 | - name: az login 60 | uses: azure/login@a457da9ea143d694b1b9c7c869ebb04ebe844ef5 # v2.3.0 61 | with: 62 | client-id: ${{ secrets.AZURE_CLIENT_ID }} 63 | tenant-id: ${{ secrets.AZURE_TENANT_ID }} 64 | subscription-id: ${{ secrets.ALTINNCR_SUBSCRIPTION_ID }} 65 | 66 | - name: az acr login 67 | run: az acr login --name altinncr 68 | 69 | - name: Tag config artifact 70 | run: | 71 | flux tag artifact oci://${{ steps.vars.outputs.registry }}/apps-monitor/configs:${{ github.event.inputs.from }} \ 72 | --provider=generic \ 73 | --tag ${{ github.event.inputs.to }} 74 | -------------------------------------------------------------------------------- /altinn-tools.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.0.31903.59 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "AppsMonitoring", "AppsMonitoring", "{908DEDBC-2BD0-45F9-9413-E22FABD4FB33}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{B853976B-7964-4FCF-961B-14BD8A1E0CF9}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Altinn.Apps.Monitoring", "AppsMonitoring\src\Altinn.Apps.Monitoring\Altinn.Apps.Monitoring.csproj", "{808CA16E-4CE1-416D-8CEC-4105AECC2225}" 11 | EndProject 12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{BCA45CBB-515E-4D16-B8FD-8F8313BA2765}" 13 | EndProject 14 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Altinn.Apps.Monitoring.Tests", "AppsMonitoring\test\Altinn.Apps.Monitoring.Tests\Altinn.Apps.Monitoring.Tests.csproj", "{D57DB076-5350-4148-847D-2F683A8D38BA}" 15 | EndProject 16 | Global 17 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 18 | Debug|Any CPU = Debug|Any CPU 19 | Release|Any CPU = Release|Any CPU 20 | EndGlobalSection 21 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 22 | {808CA16E-4CE1-416D-8CEC-4105AECC2225}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {808CA16E-4CE1-416D-8CEC-4105AECC2225}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {808CA16E-4CE1-416D-8CEC-4105AECC2225}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {808CA16E-4CE1-416D-8CEC-4105AECC2225}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {D57DB076-5350-4148-847D-2F683A8D38BA}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {D57DB076-5350-4148-847D-2F683A8D38BA}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {D57DB076-5350-4148-847D-2F683A8D38BA}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {D57DB076-5350-4148-847D-2F683A8D38BA}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(NestedProjects) = preSolution 35 | {B853976B-7964-4FCF-961B-14BD8A1E0CF9} = {908DEDBC-2BD0-45F9-9413-E22FABD4FB33} 36 | {808CA16E-4CE1-416D-8CEC-4105AECC2225} = {B853976B-7964-4FCF-961B-14BD8A1E0CF9} 37 | {BCA45CBB-515E-4D16-B8FD-8F8313BA2765} = {908DEDBC-2BD0-45F9-9413-E22FABD4FB33} 38 | {D57DB076-5350-4148-847D-2F683A8D38BA} = {BCA45CBB-515E-4D16-B8FD-8F8313BA2765} 39 | EndGlobalSection 40 | EndGlobal 41 | -------------------------------------------------------------------------------- /EventCreator/EventCreator/Program.cs: -------------------------------------------------------------------------------- 1 | using System.Reflection; 2 | 3 | using Altinn.Platform.Storage.Configuration; 4 | using Altinn.Platform.Storage.Interface.Models; 5 | 6 | using EventCreator.Clients; 7 | using EventCreator.Configuration; 8 | 9 | using Microsoft.Extensions.Configuration; 10 | 11 | var builder = new ConfigurationBuilder() 12 | .AddJsonFile($"appsettings.json", true, true) 13 | .AddUserSecrets(Assembly.GetExecutingAssembly()); 14 | var config = builder.Build(); 15 | 16 | QueueStorageSettings queueStorageSettings = new(); 17 | config.GetRequiredSection("QueueStorageSettings").Bind(queueStorageSettings); 18 | 19 | StorageDbSettings postgreSqlSettings = new(); 20 | config.GetRequiredSection("StorageDbSettings").Bind(postgreSqlSettings); 21 | 22 | GeneralSettings generalSettings = new(); 23 | config.GetRequiredSection("GeneralSettings").Bind(generalSettings); 24 | 25 | EventsQueueClient eventsQueueClient = new(queueStorageSettings, generalSettings.SourceBaseAddress); 26 | PgClient pgClient = new(postgreSqlSettings.ConnectionString); 27 | 28 | using FileStream logStream = File.OpenWrite("log.txt"); 29 | using StreamWriter logWriter = new(logStream); 30 | 31 | logWriter.WriteLine($"[{DateTime.Now}]: STARTING, reading instances.txt"); 32 | 33 | var lines = File.ReadAllLines("instances.txt"); 34 | for (var i = 0; i < lines.Length; i += 1) 35 | { 36 | var line = lines[i]; 37 | Console.WriteLine($"Processing instance: {line}"); 38 | 39 | logWriter.WriteLine($"[{DateTime.Now}]:[{line}]: Started processing, reading from Storage"); 40 | 41 | Instance? instance = await pgClient.GetOne(Guid.Parse(line)); 42 | 43 | if (instance is null) 44 | { 45 | logWriter.WriteLine($"[{DateTime.Now}]:[{line}]: Instance NOT FOUND, skipping"); 46 | continue; 47 | } 48 | 49 | logWriter.WriteLine($"[{DateTime.Now}]:[{line}]: Instance FOUND, generating and sending event"); 50 | 51 | //// await eventsQueueClient.AddEvent("app.instance.process.movedTo.Task_2", instance); 52 | //// await eventsQueueClient.AddEvent("app.instance.process.movedTo.Task_2Revisor", instance); 53 | //// await eventsQueueClient.AddEvent("app.instance.process.movedTo.Task_3", instance); 54 | //// await eventsQueueClient.AddEvent("app.instance.substatus.changed", instance); 55 | await eventsQueueClient.AddEvent("app.instance.process.completed", instance); 56 | 57 | logWriter.WriteLine($"[{DateTime.Now}]:[{line}]: Finished processing"); 58 | } 59 | 60 | logWriter.WriteLine($"[{DateTime.Now}]: Finished processing"); 61 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/_snapshots/OrchestratorTests.Orchestration_Progresses_Successfully_serviceOwner=one_generator=Empty.verified.txt: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | Desc: Iteration 1 - generator=Empty, 4 | Start: 2025-01-01T12:00:00Z, 5 | End: 2025-01-01T12:00:05Z, 6 | StateBefore: { 7 | Telemetry: [], 8 | Queries: [] 9 | }, 10 | Input: { 11 | ReportedEvents: [ 12 | { 13 | ServiceOwner: { 14 | Value: one 15 | }, 16 | Query: { 17 | Name: query, 18 | Type: Traces, 19 | QueryTemplate: template-{searchFrom}-{searchTo}, 20 | Hash: HpynynzeF9Pk52Vahp477Q== 21 | }, 22 | SearchFrom: 2024-10-03T11:59:59Z, 23 | SearchTo: 2025-01-01T11:50:00Z, 24 | Telemetry: [], 25 | Result: { 26 | Ids: [], 27 | DupeExtIds: [] 28 | } 29 | } 30 | ] 31 | }, 32 | StateAfter: { 33 | Telemetry: [], 34 | Queries: [ 35 | { 36 | Id: 1, 37 | ServiceOwner: one, 38 | Name: query, 39 | Hash: HpynynzeF9Pk52Vahp477Q==, 40 | QueriedUntil: 2025-01-01T11:50:00Z 41 | } 42 | ] 43 | } 44 | }, 45 | { 46 | Desc: Iteration 2 - generator=Empty, 47 | Start: 2025-01-01T12:10:00Z, 48 | End: 2025-01-01T12:10:05Z, 49 | StateBefore: { 50 | Telemetry: [], 51 | Queries: [ 52 | { 53 | Id: 1, 54 | ServiceOwner: one, 55 | Name: query, 56 | Hash: HpynynzeF9Pk52Vahp477Q==, 57 | QueriedUntil: 2025-01-01T11:50:00Z 58 | } 59 | ] 60 | }, 61 | Input: { 62 | ReportedEvents: [ 63 | { 64 | ServiceOwner: { 65 | Value: one 66 | }, 67 | Query: { 68 | Name: query, 69 | Type: Traces, 70 | QueryTemplate: template-{searchFrom}-{searchTo}, 71 | Hash: HpynynzeF9Pk52Vahp477Q== 72 | }, 73 | SearchFrom: 2025-01-01T11:50:00Z, 74 | SearchTo: 2025-01-01T12:00:00Z, 75 | Telemetry: [], 76 | Result: { 77 | Ids: [], 78 | DupeExtIds: [] 79 | } 80 | } 81 | ] 82 | }, 83 | StateAfter: { 84 | Telemetry: [], 85 | Queries: [ 86 | { 87 | Id: 1, 88 | ServiceOwner: one, 89 | Name: query, 90 | Hash: HpynynzeF9Pk52Vahp477Q==, 91 | QueriedUntil: 2025-01-01T12:00:00Z 92 | } 93 | ] 94 | } 95 | } 96 | ] -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Db/TelemetryEntity.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json; 2 | using System.Text.Json.Serialization; 3 | 4 | namespace Altinn.Apps.Monitoring.Application.Db; 5 | 6 | internal sealed record TelemetryEntity 7 | { 8 | public required long Id { get; init; } 9 | public required string ExtId { get; init; } 10 | public required string ServiceOwner { get; init; } 11 | public required string AppName { get; init; } 12 | public required string AppVersion { get; init; } 13 | public required Instant TimeGenerated { get; init; } 14 | public required Instant TimeIngested { get; init; } 15 | public required long DupeCount { get; init; } 16 | public required bool Seeded { get; init; } 17 | public required TelemetryData Data { get; init; } 18 | } 19 | 20 | [JsonDerivedType(typeof(TraceData), typeDiscriminator: "trace")] 21 | [JsonDerivedType(typeof(LogsData), typeDiscriminator: "logs")] 22 | [JsonDerivedType(typeof(MetricData), typeDiscriminator: "metric")] 23 | internal abstract class TelemetryData 24 | { 25 | public required int AltinnErrorId { get; init; } 26 | 27 | public static TelemetryData Deserialize(string json) 28 | { 29 | return JsonSerializer.Deserialize(json, Config.JsonOptions) ?? throw new JsonException(); 30 | } 31 | 32 | public static TelemetryData Deserialize(byte[] json) 33 | { 34 | return JsonSerializer.Deserialize(json, Config.JsonOptions) ?? throw new JsonException(); 35 | } 36 | 37 | public string Serialize() 38 | { 39 | return JsonSerializer.Serialize(this, Config.JsonOptions); 40 | } 41 | 42 | public byte[] SerializeToUtf8Bytes() 43 | { 44 | return JsonSerializer.SerializeToUtf8Bytes(this, Config.JsonOptions); 45 | } 46 | } 47 | 48 | internal sealed class TraceData : TelemetryData 49 | { 50 | public required int? InstanceOwnerPartyId { get; init; } 51 | public required Guid? InstanceId { get; init; } 52 | public required string TraceId { get; init; } 53 | public required string SpanId { get; init; } 54 | public required string? ParentSpanId { get; init; } 55 | public required string TraceName { get; init; } 56 | public required string SpanName { get; init; } 57 | public required bool? Success { get; init; } 58 | public required string? Result { get; init; } 59 | public required Duration Duration { get; init; } 60 | public required Dictionary? Attributes { get; init; } 61 | } 62 | 63 | internal sealed class LogsData : TelemetryData 64 | { 65 | public required string? TraceId { get; init; } 66 | public required string? SpanId { get; init; } 67 | public required string Message { get; init; } 68 | public required Dictionary? Attributes { get; init; } 69 | } 70 | 71 | internal sealed class MetricData : TelemetryData 72 | { 73 | public required string Name { get; init; } 74 | public required double Value { get; init; } 75 | public required Dictionary? Attributes { get; init; } 76 | } 77 | -------------------------------------------------------------------------------- /RepoCleanup/Functions/SetupNewServiceOwnerFunction.cs: -------------------------------------------------------------------------------- 1 | using RepoCleanup.Application.CommandHandlers; 2 | using RepoCleanup.Application.Commands; 3 | using RepoCleanup.Infrastructure.Clients.Gitea; 4 | using System; 5 | using System.Threading.Tasks; 6 | 7 | namespace RepoCleanup.Functions 8 | { 9 | public static class SetupNewServiceOwnerFunction 10 | { 11 | public static async Task Run() 12 | { 13 | SharedFunctionSnippets.WriteHeader("Setup a new service owner in Gitea with all teams and default repositories."); 14 | 15 | var giteaService = new GiteaService(); 16 | 17 | // Create new org 18 | Organisation org = SharedFunctionSnippets.CollectNewOrgInfo(); 19 | var createOrgCommandHandler = new CreateOrgCommandHandler(giteaService); 20 | bool orgCreated = await createOrgCommandHandler.Handle(new CreateOrgCommand(org.Username, org.Fullname, org.Website)); 21 | 22 | if (!orgCreated) 23 | { 24 | Console.WriteLine($"Could not create org {org.Fullname}"); 25 | return; 26 | } 27 | else 28 | { 29 | Console.WriteLine($"Created org {org.Fullname}"); 30 | } 31 | 32 | // Create default teams 33 | var createDefaultTeamsCommandHandler = new CreateDefaultTeamsCommandHandler(giteaService); 34 | var teamsCreated = await createDefaultTeamsCommandHandler.Handle(new CreateDefaultTeamsCommand(org.Username)); 35 | if (!teamsCreated) 36 | { 37 | Console.WriteLine($"Could not create all default teams for {org.Fullname}"); 38 | return; 39 | } 40 | else 41 | { 42 | Console.WriteLine($"Created all default teams for {org.Fullname}"); 43 | } 44 | 45 | // Create default repositories 46 | var isDatamodelRepoCreated = await CreateRepoWithPrefix(giteaService, org, "datamodels"); 47 | var isContentRepoCreated = await CreateRepoWithPrefix(giteaService, org, "content"); 48 | 49 | if (isDatamodelRepoCreated && isContentRepoCreated) 50 | { 51 | Console.WriteLine("Done setting up new service owner in Gitea!"); 52 | } 53 | } 54 | 55 | private static async Task CreateRepoWithPrefix(GiteaService giteaService, Organisation org, string repoName) 56 | { 57 | var createRepoForOrgsCommandHandler = new CreateRepoForOrgsCommandHandler(giteaService); 58 | var numberOfReposCreated = await createRepoForOrgsCommandHandler.Handle(new CreateRepoForOrgsCommand([org.Username], repoName, true)); 59 | if (numberOfReposCreated != 1) 60 | { 61 | Console.WriteLine($"Could not create default {repoName} repository for {org.Fullname}"); 62 | return false; 63 | } 64 | else 65 | { 66 | Console.WriteLine($"Created default {repoName} repository for {org.Fullname}"); 67 | return true; 68 | } 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /EventCreator/EventCreator/Clients/EventsQueueClient.cs: -------------------------------------------------------------------------------- 1 | using System.Text; 2 | 3 | using Altinn.Platform.Storage.Interface.Models; 4 | 5 | using Azure.Storage.Queues; 6 | 7 | using CloudNative.CloudEvents; 8 | using EventCreator.Configuration; 9 | 10 | namespace EventCreator.Clients; 11 | 12 | public class EventsQueueClient(QueueStorageSettings settings, string resourceBaseAddress) 13 | { 14 | public const string AppResourceTemplate = "urn:altinn:resource:app_{0}"; 15 | 16 | private readonly QueueStorageSettings _settings = settings; 17 | private readonly string _resourceBaseAddress = resourceBaseAddress; 18 | 19 | private QueueClient? _registrationQueueClient; 20 | 21 | public async Task AddEvent(string eventType, Instance instance) 22 | { 23 | string? alternativeSubject = null; 24 | if (!string.IsNullOrWhiteSpace(instance.InstanceOwner.OrganisationNumber)) 25 | { 26 | alternativeSubject = $"/org/{instance.InstanceOwner.OrganisationNumber}"; 27 | } 28 | 29 | if (!string.IsNullOrWhiteSpace(instance.InstanceOwner.PersonNumber)) 30 | { 31 | alternativeSubject = $"/person/{instance.InstanceOwner.PersonNumber}"; 32 | } 33 | 34 | var baseUrl = FormattedExternalAppBaseUrl(instance.Org, instance.AppId); 35 | 36 | CloudEvent cloudEvent = new(CloudEventsSpecVersion.V1_0) 37 | { 38 | Id = Guid.NewGuid().ToString(), 39 | Subject = $"/party/{instance.InstanceOwner.PartyId}", 40 | Type = eventType, 41 | Time = DateTime.UtcNow, 42 | Source = new Uri($"{baseUrl}/instances/{instance.InstanceOwner.PartyId}/{instance.Id}"), 43 | }; 44 | 45 | cloudEvent.SetAttributeFromString("resource", string.Format(AppResourceTemplate, instance.AppId.Replace('/', '_'))); 46 | cloudEvent.SetAttributeFromString("resourceinstance", $"{instance.InstanceOwner.PartyId}/{instance.Id}"); 47 | 48 | if (!string.IsNullOrEmpty(alternativeSubject)) 49 | { 50 | cloudEvent.SetAttributeFromString("alternativesubject", alternativeSubject); 51 | } 52 | 53 | string serializedCloudEvent = cloudEvent.Serialize(); 54 | 55 | await EnqueueRegistration(serializedCloudEvent); 56 | } 57 | 58 | private async Task EnqueueRegistration(string content) 59 | { 60 | QueueClient client = await GetRegistrationQueueClient(); 61 | await client.SendMessageAsync(Convert.ToBase64String(Encoding.UTF8.GetBytes(content))); 62 | } 63 | 64 | private async Task GetRegistrationQueueClient() 65 | { 66 | if (_registrationQueueClient == null) 67 | { 68 | _registrationQueueClient = new QueueClient(_settings.ConnectionString, _settings.RegistrationQueueName); 69 | await _registrationQueueClient.CreateIfNotExistsAsync(); 70 | } 71 | 72 | return _registrationQueueClient; 73 | } 74 | 75 | private string FormattedExternalAppBaseUrl(string org, string appId) 76 | { 77 | string appHostUrl = string.Format(_resourceBaseAddress, org); 78 | string sourceUrl = $"{appHostUrl}/{appId}"; 79 | return sourceUrl; 80 | } 81 | } 82 | -------------------------------------------------------------------------------- /.github/workflows/apps-monitoring-deploy.yml: -------------------------------------------------------------------------------- 1 | name: Apps.Monitoring deploy 2 | 3 | on: 4 | workflow_dispatch: 5 | 6 | jobs: 7 | deploy: 8 | 9 | runs-on: ubuntu-latest 10 | environment: prod 11 | permissions: 12 | id-token: write # Require write permission to Fetch an OIDC token. 13 | defaults: 14 | run: 15 | working-directory: ./AppsMonitoring 16 | 17 | steps: 18 | - uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1 19 | with: 20 | fetch-depth: 0 21 | 22 | - name: flux install 23 | uses: fluxcd/flux2/action@8454b02a32e48d775b9f563cb51fdcb1787b5b93 # v2.7.5 24 | with: 25 | version: '2.5.1' 26 | 27 | - name: set vars 28 | id: vars 29 | run: | 30 | echo "registry=altinncr.azurecr.io" >> $GITHUB_OUTPUT 31 | echo "image=altinncr.azurecr.io/apps-monitor/image:$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT 32 | echo "image_tag=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT 33 | 34 | - name: az login 35 | uses: azure/login@a457da9ea143d694b1b9c7c869ebb04ebe844ef5 # v2.3.0 36 | with: 37 | client-id: ${{ secrets.AZURE_CLIENT_ID }} 38 | tenant-id: ${{ secrets.AZURE_TENANT_ID }} 39 | subscription-id: ${{ secrets.ALTINNCR_SUBSCRIPTION_ID }} 40 | 41 | - name: az acr login 42 | run: az acr login --name altinncr 43 | 44 | - name: docker build 45 | run: docker build -t ${{ steps.vars.outputs.image }} -f src/Altinn.Apps.Monitoring/Dockerfile . 46 | 47 | - name: scan image 48 | uses: aquasecurity/trivy-action@b6643a29fecd7f34b3597bc6acb0a98b03d33ff8 # 0.33.1 49 | with: 50 | image-ref: '${{ steps.vars.outputs.image }}' 51 | format: 'table' 52 | exit-code: '1' 53 | ignore-unfixed: true 54 | vuln-type: 'os,library' 55 | severity: 'CRITICAL,HIGH' 56 | 57 | - name: push image 58 | run: docker push ${{ steps.vars.outputs.image }} 59 | 60 | - name: yq install 61 | uses: mikefarah/yq@065b200af9851db0d5132f50bc10b1406ea5c0a8 # v4.50.1 62 | 63 | - name: patch base with image tag 64 | run: | 65 | export APP_IMAGE="${{ steps.vars.outputs.image }}" 66 | export APP_IMAGE_TAG="${{ steps.vars.outputs.image_tag }}" 67 | yq -i '.metadata.annotations["altinn.no/image"] = env(APP_IMAGE)' infra/deployment/base/deployment.yaml 68 | yq -i '.metadata.annotations["altinn.no/image-tag"] = env(APP_IMAGE_TAG)' infra/deployment/base/deployment.yaml 69 | 70 | - name: push deployment 71 | run: | 72 | cd infra/deployment 73 | flux push artifact oci://${{ steps.vars.outputs.registry }}/apps-monitor/configs:${{ steps.vars.outputs.image_tag }} \ 74 | --provider=generic \ 75 | --reproducible \ 76 | --path="." \ 77 | --source="$(git config --get remote.origin.url)" \ 78 | --revision="$(git branch --show-current)/${{ steps.vars.outputs.image_tag }}" 79 | flux tag artifact oci://${{ steps.vars.outputs.registry }}/apps-monitor/configs:${{ steps.vars.outputs.image_tag }} \ 80 | --provider=generic \ 81 | --tag at24 -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Querying/StaticQueryLoader.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.Options; 2 | 3 | namespace Altinn.Apps.Monitoring.Application; 4 | 5 | internal sealed class StaticQueryLoader(ILogger logger, IOptionsMonitor config) 6 | : IQueryLoader 7 | { 8 | private readonly ILogger _logger = logger; 9 | private readonly IOptionsMonitor _config = config; 10 | 11 | public ValueTask> Load(CancellationToken cancellationToken) 12 | { 13 | var config = _config.CurrentValue; 14 | var target = config.AltinnEnvironment switch 15 | { 16 | "prod" => "platform.altinn.no", 17 | _ => $"platform.{config.AltinnEnvironment}.altinn.no", 18 | }; 19 | _logger.LogInformation("Loading queries for target: {Target}", target); 20 | 21 | Query[] queries = 22 | [ 23 | new( 24 | "Failed Storage instance events", 25 | QueryType.Traces, 26 | // * 'OperationName' is present for classic Azure App Insights SDK (the same name as the root span name) 27 | // but for OpenTelemetry the root span name is not present on children spans, so we need to use 'OperationName1' (from the join) 28 | // * 'Target' sometimes has garbage at the end, so we use 'startswith' 29 | // * This error condition should have failed the root process/next span, so we check 'Success1' 30 | $$""" 31 | AppDependencies 32 | | where TimeGenerated > todatetime('{0}') and TimeGenerated <= todatetime('{1}') 33 | | where Success == false 34 | | where Target startswith "{{target}}" 35 | | where Name startswith "POST /storage/api/v1/instances/" and Name endswith "/events" 36 | | join kind=inner AppRequests on OperationId 37 | | where OperationName1 startswith "PUT Process/NextElement" or OperationName1 endswith "/process/next" 38 | | where Success1 == false; 39 | """ 40 | ), 41 | new( 42 | "Failed Altinn events", 43 | QueryType.Traces, 44 | // * 'OperationName' is present for classic Azure App Insights SDK (the same name as the root span name) 45 | // but for OpenTelemetry the root span name is not present on children spans, so we need to use 'OperationName1' (from the join) 46 | // * 'Target' sometimes has garbage at the end, so we use 'startswith' 47 | // * Errors in app.process.completed event does not fail the root process/next span, so we don't check 'Success1' here 48 | $$""" 49 | AppDependencies 50 | | where TimeGenerated > todatetime('{0}') and TimeGenerated <= todatetime('{1}') 51 | | where Success == false 52 | | where Target startswith "{{target}}" 53 | | where Name == "POST /events/api/v1/app" 54 | | join kind=inner AppRequests on OperationId 55 | | where OperationName1 startswith "PUT Process/NextElement" or OperationName1 endswith "/process/next"; 56 | """ 57 | ), 58 | ]; 59 | 60 | return new(queries); 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Db/_snapshots/SeederTests.Seeds_Db_Successfully.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | Telemetry: [ 3 | { 4 | Id: 1, 5 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 6 | ServiceOwner: skd, 7 | AppName: formueinntekt-skattemelding-v2, 8 | AppVersion: 8.0.8, 9 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 10 | TimeIngested: 2025-02-19T07:34:32.420042Z, 11 | Seeded: true, 12 | Data: { 13 | InstanceOwnerPartyId: 123, 14 | InstanceId: Guid_1, 15 | TraceId: Guid_2, 16 | SpanId: 90c159bde9b1a6c1, 17 | ParentSpanId: 7e7143a41c29e532, 18 | TraceName: PUT Process/NextElement [app/instanceGuid/instanceOwnerPartyId/org], 19 | SpanName: POST /storage/api/v1/instances/123/1d449be1-7114-405c-aeee-1f09799f7b74/events, 20 | Success: false, 21 | Result: Faulted, 22 | Duration: 0:00:00:27.478494, 23 | Attributes: { 24 | Data: https://platform.altinn.no/storage/api/v1/instances/123/1d449be1-7114-405c-aeee-1f09799f7b74/events, 25 | DependencyType: HTTP, 26 | PerformanceBucket: 15sec-30sec, 27 | Properties: {"AspNetCoreEnvironment":"Production","_MS.ProcessedByMetricExtractors":"(Name:'Dependencies', Ver:'1.1')"}, 28 | Target: platform.altinn.no 29 | }, 30 | AltinnErrorId: 1 31 | } 32 | }, 33 | { 34 | Id: 2, 35 | ExtId: 4ba19e3f5a545728934b1f921e06d92b-f86a2aed5c5d9f63, 36 | ServiceOwner: tad, 37 | AppName: bku, 38 | AppVersion: 6.0.35, 39 | TimeGenerated: 2025-02-19T08:29:04.386517Z, 40 | TimeIngested: 2025-02-19T08:34:32.406732Z, 41 | Seeded: true, 42 | Data: { 43 | InstanceOwnerPartyId: 123, 44 | InstanceId: Guid_1, 45 | TraceId: Guid_3, 46 | SpanId: f86a2aed5c5d9f63, 47 | ParentSpanId: e962c6c0f02d5dde, 48 | TraceName: PUT Process/NextElement [app/instanceGuid/instanceOwnerPartyId/org], 49 | SpanName: POST /events/api/v1/app, 50 | Success: false, 51 | Result: Faulted, 52 | Duration: 0:00:00:27.804053, 53 | Attributes: { 54 | Data: https://platform.altinn.no/events/api/v1/app, 55 | DependencyType: HTTP, 56 | PerformanceBucket: 15sec-30sec, 57 | Properties: {"AspNetCoreEnvironment":"Production","_MS.ProcessedByMetricExtractors":"(Name:'Dependencies', Ver:'1.1')"}, 58 | Target: platform.altinn.no 59 | }, 60 | AltinnErrorId: 2 61 | } 62 | }, 63 | { 64 | Id: 3, 65 | ExtId: f2b6eea456788828f6195fbce59f740f-e0135dfc441dc49a, 66 | ServiceOwner: ssb, 67 | AppName: ra1000-01, 68 | AppVersion: 6.0.26, 69 | TimeGenerated: 2025-02-19T16:07:44.720544Z, 70 | TimeIngested: 2025-02-19T16:00:13.785543Z, 71 | Seeded: true, 72 | Data: { 73 | InstanceOwnerPartyId: 123, 74 | InstanceId: Guid_1, 75 | TraceId: Guid_4, 76 | SpanId: e0135dfc441dc49a, 77 | ParentSpanId: e37e123015737cb8, 78 | TraceName: PUT Process/NextElement [app/instanceGuid/instanceOwnerPartyId/org], 79 | SpanName: POST /events/api/v1/app, 80 | Success: false, 81 | Result: Faulted, 82 | Duration: 0:00:00:26.8139518, 83 | Attributes: { 84 | Data: https://platform.altinn.no/events/api/v1/app, 85 | DependencyType: HTTP, 86 | PerformanceBucket: 15sec-30sec, 87 | Properties: {"AspNetCoreEnvironment":"Production","_MS.ProcessedByMetricExtractors":"(Name:'Dependencies', Ver:'1.1')"}, 88 | Target: platform.altinn.no 89 | }, 90 | AltinnErrorId: 2 91 | } 92 | } 93 | ], 94 | QueryStates: [] 95 | } -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Azure/AzureServiceOwnerDiscovery.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Concurrent; 2 | using Altinn.Apps.Monitoring.Domain; 3 | using Azure.ResourceManager; 4 | using Microsoft.Extensions.Options; 5 | 6 | namespace Altinn.Apps.Monitoring.Application.Azure; 7 | 8 | internal sealed class AzureServiceOwnerDiscovery( 9 | ILogger logger, 10 | IOptionsMonitor config, 11 | AzureClients clients, 12 | AzureServiceOwnerResources serviceOwnerResources, 13 | Telemetry telemetry 14 | ) : IServiceOwnerDiscovery 15 | { 16 | private readonly ILogger _logger = logger; 17 | private readonly IOptionsMonitor _config = config; 18 | private readonly ArmClient _armClient = clients.ArmClient; 19 | private readonly AzureServiceOwnerResources _serviceOwnerResources = serviceOwnerResources; 20 | private readonly Telemetry _telemetry = telemetry; 21 | private long _iteration = -1; 22 | 23 | public async ValueTask> Discover(CancellationToken cancellationToken) 24 | { 25 | using var activity = _telemetry.Activities.StartActivity("AzureServiceOwnerDiscovery.Discover"); 26 | var env = _config.CurrentValue.AltinnEnvironment; 27 | var envToMatch = env switch 28 | { 29 | "prod" => "prod", 30 | "at24" => "test", 31 | "tt02" => "test", 32 | _ => throw new Exception("Unexpected environment: " + env), 33 | }; 34 | var iteration = Interlocked.Increment(ref _iteration); 35 | var serviceOwners = new ConcurrentBag(); 36 | await Parallel.ForEachAsync( 37 | _armClient.GetSubscriptions().GetAllAsync(cancellationToken), 38 | new ParallelOptions 39 | { 40 | MaxDegreeOfParallelism = Math.Max(4, Environment.ProcessorCount), 41 | CancellationToken = cancellationToken, 42 | }, 43 | async (subscription, cancellationToken) => 44 | { 45 | if (iteration == 0) 46 | { 47 | _logger.LogInformation( 48 | "Found Subscription {SubscriptionId}: {DisplayName}", 49 | subscription.Id.SubscriptionId, 50 | subscription.Data.DisplayName 51 | ); 52 | } 53 | 54 | if (!subscription.Data.DisplayName.StartsWith("altinn", StringComparison.OrdinalIgnoreCase)) 55 | return; 56 | if (!subscription.Data.DisplayName.EndsWith(envToMatch, StringComparison.OrdinalIgnoreCase)) 57 | return; 58 | 59 | var split = subscription.Data.DisplayName.Split('-'); 60 | if (split.Length != 3) 61 | return; 62 | 63 | var serviceOwnerValue = split[1]; 64 | if (serviceOwnerValue.Any(c => char.IsLower(c) || !char.IsLetter(c))) 65 | return; 66 | 67 | #pragma warning disable CA1308 // Normalize strings to uppercase 68 | var serviceOwner = ServiceOwner.Parse( 69 | serviceOwnerValue.ToLowerInvariant(), 70 | subscription.Id.SubscriptionId 71 | ); 72 | #pragma warning restore CA1308 // Normalize strings to uppercase 73 | var resources = await _serviceOwnerResources.GetResources(serviceOwner, cancellationToken); 74 | if (resources is null) 75 | return; 76 | 77 | serviceOwners.Add(serviceOwner); 78 | } 79 | ); 80 | 81 | var result = serviceOwners.ToArray(); 82 | activity?.SetTag("serviceowners.count", result.Length); 83 | _logger.LogInformation("Discovered {Count} service owners", result.Length); 84 | return result; 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Slack/_snapshots/SlackAlerterTests.Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry_case=200-error_waitForRetryEvents=0.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | OrchestratorEvents: [ 3 | { 4 | ServiceOwner: { 5 | Value: skd 6 | }, 7 | Query: { 8 | Name: query, 9 | Type: Traces, 10 | QueryTemplate: template-{searchFrom}-{searchTo}, 11 | Hash: HpynynzeF9Pk52Vahp477Q== 12 | }, 13 | SearchFrom: 2024-11-22T11:59:59Z, 14 | SearchTo: 2025-02-20T11:50:00Z, 15 | Telemetry: [ 16 | { 17 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 18 | ServiceOwner: skd, 19 | AppName: formueinntekt-skattemelding-v2, 20 | AppVersion: 8.0.8, 21 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 22 | TimeIngested: 2025-02-20T12:00:05Z, 23 | Seeded: false, 24 | Data: {Scrubbed} 25 | }, 26 | { 27 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 28 | ServiceOwner: skd, 29 | AppName: formueinntekt-skattemelding-v2, 30 | AppVersion: 8.0.8, 31 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 32 | TimeIngested: 2025-02-20T12:00:05Z, 33 | Seeded: false, 34 | Data: {Scrubbed} 35 | } 36 | ], 37 | Result: { 38 | Written: 1, 39 | Ids: [ 40 | 1, 41 | 5 42 | ], 43 | DupeExtIds: [ 44 | 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1 45 | ] 46 | } 47 | } 48 | ], 49 | AlerterEvents: [ 50 | { 51 | Item: { 52 | Id: 5, 53 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 54 | ServiceOwner: skd, 55 | AppName: formueinntekt-skattemelding-v2, 56 | AppVersion: 8.0.8, 57 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 58 | TimeIngested: 2025-02-20T12:00:05Z, 59 | Seeded: false, 60 | Data: {Scrubbed} 61 | }, 62 | AlertBefore: { 63 | State: Pending, 64 | TelemetryId: 5, 65 | Data: {}, 66 | CreatedAt: {Scrubbed}, 67 | UpdatedAt: {Scrubbed} 68 | } 69 | } 70 | ], 71 | State: { 72 | Telemetry: [ 73 | { 74 | Id: 2, 75 | ExtId: 4ba19e3f5a545728934b1f921e06d92b-f86a2aed5c5d9f63, 76 | ServiceOwner: tad, 77 | AppName: bku, 78 | AppVersion: 6.0.35, 79 | TimeGenerated: 2025-02-19T08:29:04.386517Z, 80 | TimeIngested: 2025-02-19T08:34:32.406732Z, 81 | Seeded: true, 82 | Data: {Scrubbed} 83 | }, 84 | { 85 | Id: 3, 86 | ExtId: f2b6eea456788828f6195fbce59f740f-e0135dfc441dc49a, 87 | ServiceOwner: ssb, 88 | AppName: ra1000-01, 89 | AppVersion: 6.0.26, 90 | TimeGenerated: 2025-02-19T16:07:44.720544Z, 91 | TimeIngested: 2025-02-19T16:00:13.785543Z, 92 | Seeded: true, 93 | Data: {Scrubbed} 94 | }, 95 | { 96 | Id: 1, 97 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 98 | ServiceOwner: skd, 99 | AppName: formueinntekt-skattemelding-v2, 100 | AppVersion: 8.0.8, 101 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 102 | TimeIngested: 2025-02-19T07:34:32.420042Z, 103 | DupeCount: 1, 104 | Seeded: true, 105 | Data: {Scrubbed} 106 | }, 107 | { 108 | Id: 5, 109 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 110 | ServiceOwner: skd, 111 | AppName: formueinntekt-skattemelding-v2, 112 | AppVersion: 8.0.8, 113 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 114 | TimeIngested: 2025-02-20T12:00:05Z, 115 | Seeded: false, 116 | Data: {Scrubbed} 117 | } 118 | ], 119 | Alerts: [] 120 | } 121 | } -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Slack/_snapshots/SlackAlerterTests.Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry_case=500-error_waitForRetryEvents=0.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | OrchestratorEvents: [ 3 | { 4 | ServiceOwner: { 5 | Value: skd 6 | }, 7 | Query: { 8 | Name: query, 9 | Type: Traces, 10 | QueryTemplate: template-{searchFrom}-{searchTo}, 11 | Hash: HpynynzeF9Pk52Vahp477Q== 12 | }, 13 | SearchFrom: 2024-11-22T11:59:59Z, 14 | SearchTo: 2025-02-20T11:50:00Z, 15 | Telemetry: [ 16 | { 17 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 18 | ServiceOwner: skd, 19 | AppName: formueinntekt-skattemelding-v2, 20 | AppVersion: 8.0.8, 21 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 22 | TimeIngested: 2025-02-20T12:00:05Z, 23 | Seeded: false, 24 | Data: {Scrubbed} 25 | }, 26 | { 27 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 28 | ServiceOwner: skd, 29 | AppName: formueinntekt-skattemelding-v2, 30 | AppVersion: 8.0.8, 31 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 32 | TimeIngested: 2025-02-20T12:00:05Z, 33 | Seeded: false, 34 | Data: {Scrubbed} 35 | } 36 | ], 37 | Result: { 38 | Written: 1, 39 | Ids: [ 40 | 1, 41 | 5 42 | ], 43 | DupeExtIds: [ 44 | 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1 45 | ] 46 | } 47 | } 48 | ], 49 | AlerterEvents: [ 50 | { 51 | Item: { 52 | Id: 5, 53 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 54 | ServiceOwner: skd, 55 | AppName: formueinntekt-skattemelding-v2, 56 | AppVersion: 8.0.8, 57 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 58 | TimeIngested: 2025-02-20T12:00:05Z, 59 | Seeded: false, 60 | Data: {Scrubbed} 61 | }, 62 | AlertBefore: { 63 | State: Pending, 64 | TelemetryId: 5, 65 | Data: {}, 66 | CreatedAt: {Scrubbed}, 67 | UpdatedAt: {Scrubbed} 68 | } 69 | } 70 | ], 71 | State: { 72 | Telemetry: [ 73 | { 74 | Id: 2, 75 | ExtId: 4ba19e3f5a545728934b1f921e06d92b-f86a2aed5c5d9f63, 76 | ServiceOwner: tad, 77 | AppName: bku, 78 | AppVersion: 6.0.35, 79 | TimeGenerated: 2025-02-19T08:29:04.386517Z, 80 | TimeIngested: 2025-02-19T08:34:32.406732Z, 81 | Seeded: true, 82 | Data: {Scrubbed} 83 | }, 84 | { 85 | Id: 3, 86 | ExtId: f2b6eea456788828f6195fbce59f740f-e0135dfc441dc49a, 87 | ServiceOwner: ssb, 88 | AppName: ra1000-01, 89 | AppVersion: 6.0.26, 90 | TimeGenerated: 2025-02-19T16:07:44.720544Z, 91 | TimeIngested: 2025-02-19T16:00:13.785543Z, 92 | Seeded: true, 93 | Data: {Scrubbed} 94 | }, 95 | { 96 | Id: 1, 97 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 98 | ServiceOwner: skd, 99 | AppName: formueinntekt-skattemelding-v2, 100 | AppVersion: 8.0.8, 101 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 102 | TimeIngested: 2025-02-19T07:34:32.420042Z, 103 | DupeCount: 1, 104 | Seeded: true, 105 | Data: {Scrubbed} 106 | }, 107 | { 108 | Id: 5, 109 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 110 | ServiceOwner: skd, 111 | AppName: formueinntekt-skattemelding-v2, 112 | AppVersion: 8.0.8, 113 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 114 | TimeIngested: 2025-02-20T12:00:05Z, 115 | Seeded: false, 116 | Data: {Scrubbed} 117 | } 118 | ], 119 | Alerts: [] 120 | } 121 | } -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Slack/_snapshots/SlackAlerterTests.Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry_case=429-ratelimited_waitForRetryEvents=0.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | OrchestratorEvents: [ 3 | { 4 | ServiceOwner: { 5 | Value: skd 6 | }, 7 | Query: { 8 | Name: query, 9 | Type: Traces, 10 | QueryTemplate: template-{searchFrom}-{searchTo}, 11 | Hash: HpynynzeF9Pk52Vahp477Q== 12 | }, 13 | SearchFrom: 2024-11-22T11:59:59Z, 14 | SearchTo: 2025-02-20T11:50:00Z, 15 | Telemetry: [ 16 | { 17 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 18 | ServiceOwner: skd, 19 | AppName: formueinntekt-skattemelding-v2, 20 | AppVersion: 8.0.8, 21 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 22 | TimeIngested: 2025-02-20T12:00:05Z, 23 | Seeded: false, 24 | Data: {Scrubbed} 25 | }, 26 | { 27 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 28 | ServiceOwner: skd, 29 | AppName: formueinntekt-skattemelding-v2, 30 | AppVersion: 8.0.8, 31 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 32 | TimeIngested: 2025-02-20T12:00:05Z, 33 | Seeded: false, 34 | Data: {Scrubbed} 35 | } 36 | ], 37 | Result: { 38 | Written: 1, 39 | Ids: [ 40 | 1, 41 | 5 42 | ], 43 | DupeExtIds: [ 44 | 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1 45 | ] 46 | } 47 | } 48 | ], 49 | AlerterEvents: [ 50 | { 51 | Item: { 52 | Id: 5, 53 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 54 | ServiceOwner: skd, 55 | AppName: formueinntekt-skattemelding-v2, 56 | AppVersion: 8.0.8, 57 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 58 | TimeIngested: 2025-02-20T12:00:05Z, 59 | Seeded: false, 60 | Data: {Scrubbed} 61 | }, 62 | AlertBefore: { 63 | State: Pending, 64 | TelemetryId: 5, 65 | Data: {}, 66 | CreatedAt: {Scrubbed}, 67 | UpdatedAt: {Scrubbed} 68 | } 69 | } 70 | ], 71 | State: { 72 | Telemetry: [ 73 | { 74 | Id: 2, 75 | ExtId: 4ba19e3f5a545728934b1f921e06d92b-f86a2aed5c5d9f63, 76 | ServiceOwner: tad, 77 | AppName: bku, 78 | AppVersion: 6.0.35, 79 | TimeGenerated: 2025-02-19T08:29:04.386517Z, 80 | TimeIngested: 2025-02-19T08:34:32.406732Z, 81 | Seeded: true, 82 | Data: {Scrubbed} 83 | }, 84 | { 85 | Id: 3, 86 | ExtId: f2b6eea456788828f6195fbce59f740f-e0135dfc441dc49a, 87 | ServiceOwner: ssb, 88 | AppName: ra1000-01, 89 | AppVersion: 6.0.26, 90 | TimeGenerated: 2025-02-19T16:07:44.720544Z, 91 | TimeIngested: 2025-02-19T16:00:13.785543Z, 92 | Seeded: true, 93 | Data: {Scrubbed} 94 | }, 95 | { 96 | Id: 1, 97 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 98 | ServiceOwner: skd, 99 | AppName: formueinntekt-skattemelding-v2, 100 | AppVersion: 8.0.8, 101 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 102 | TimeIngested: 2025-02-19T07:34:32.420042Z, 103 | DupeCount: 1, 104 | Seeded: true, 105 | Data: {Scrubbed} 106 | }, 107 | { 108 | Id: 5, 109 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 110 | ServiceOwner: skd, 111 | AppName: formueinntekt-skattemelding-v2, 112 | AppVersion: 8.0.8, 113 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 114 | TimeIngested: 2025-02-20T12:00:05Z, 115 | Seeded: false, 116 | Data: {Scrubbed} 117 | } 118 | ], 119 | Alerts: [] 120 | } 121 | } -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Slack/SlackAlerterTests.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json; 2 | using Altinn.Apps.Monitoring.Application; 3 | using Altinn.Apps.Monitoring.Application.Db; 4 | using Altinn.Apps.Monitoring.Application.Slack; 5 | using Altinn.Apps.Monitoring.Domain; 6 | 7 | namespace Altinn.Apps.Monitoring.Tests.Application.Slack; 8 | 9 | public class SlackAlerterTests 10 | { 11 | [Theory] 12 | [InlineData(AppFixture.Slack.Cases.Ok)] 13 | [InlineData(AppFixture.Slack.Cases.Error)] 14 | [InlineData(AppFixture.Slack.Cases.ServerError)] 15 | [InlineData(AppFixture.Slack.Cases.RateLimited)] 16 | [InlineData(AppFixture.Slack.Cases.ServerErrorThenOk)] 17 | [InlineData(AppFixture.Slack.Cases.RateLimitedThenOk)] 18 | [InlineData(AppFixture.Slack.Cases.ServerError4TimesThenOk)] 19 | public async Task Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry(string @case) 20 | { 21 | ServiceOwner[] serviceOwners = [ServiceOwner.Parse("skd")]; 22 | await using var fixture = await AppFixture.Create(@case, serviceOwners); 23 | var orchestratorFixture = fixture.OrchestratorFixture; 24 | var (hostFixture, startSignal, adapterSemaphore, pollInterval, latency, queries, cancellationToken) = 25 | orchestratorFixture; 26 | 27 | var timeProvider = hostFixture.TimeProvider; 28 | var repository = hostFixture.Repository; 29 | 30 | List orchestratorEvents = new(); 31 | List alerterEvents = new(); 32 | 33 | // Let orchestrator start work 34 | _ = fixture.StartOrchestrator(); 35 | 36 | await fixture.WaitForOrchestratorIteration(orchestratorEvents, cancellationToken); 37 | await fixture.WaitForAlerterIteration(alerterEvents, cancellationToken); 38 | 39 | var expectedRetries = AppFixture.Slack.ExpectedRetries[@case]; 40 | 41 | await Verify(NewSnapshot(orchestratorEvents, alerterEvents, repository, cancellationToken)) 42 | .ScrubMember(e => e.Data) 43 | .DontScrubDateTimes() 44 | .ScrubMember(e => e.CreatedAt) 45 | .ScrubMember(e => e.UpdatedAt) 46 | .DontIgnoreEmptyCollections() 47 | .UseTextForParameters($"case={@case}_waitForRetryEvents={expectedRetries}"); 48 | } 49 | 50 | private sealed record State(IReadOnlyList Telemetry, IReadOnlyList Alerts); 51 | 52 | private static async ValueTask NewSnapshot( 53 | IReadOnlyList orchestratorEvents, 54 | IReadOnlyList alerterEvents, 55 | Repository repository, 56 | CancellationToken cancellationToken 57 | ) 58 | { 59 | var (telemetry, alerts) = await GetState(repository, cancellationToken); 60 | return new 61 | { 62 | OrchestratorEvents = orchestratorEvents, 63 | AlerterEvents = alerterEvents, 64 | State = new State(telemetry, alerts), 65 | }; 66 | } 67 | 68 | private static async Task<(IReadOnlyList Telemetry, IReadOnlyList Alerts)> GetState( 69 | Repository repository, 70 | CancellationToken cancellationToken 71 | ) 72 | { 73 | var telemetry = await repository.ListTelemetry(cancellationToken: cancellationToken); 74 | var alerts = await repository.ListAlerts(cancellationToken: cancellationToken); 75 | return (telemetry, alerts); 76 | } 77 | 78 | [Fact] 79 | public async Task Deserialization_Of_Slack_Ok_Response_Succeeds() 80 | { 81 | var json = AppFixture.Slack.OkPayload; 82 | 83 | var response = JsonSerializer.Deserialize(json); 84 | await Verify(response); 85 | } 86 | 87 | [Fact] 88 | public async Task Deserialization_Of_Slack_Error_Response_Succeeds() 89 | { 90 | var json = AppFixture.Slack.ErrorPayload; 91 | 92 | var response = JsonSerializer.Deserialize(json); 93 | await Verify(response); 94 | } 95 | } 96 | -------------------------------------------------------------------------------- /RepoCleanup/Infrastructure/Clients/Altinn2/AltinnServiceRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Net.Http; 3 | using System.Text.Json; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | using System.Xml.Linq; 7 | 8 | namespace RepoCleanup.Infrastructure.Clients.Altinn2 9 | { 10 | /// 11 | /// A simple tool for downloading metadata informasion about services in Altinn 2. 12 | /// 13 | public class AltinnServiceRepository 14 | { 15 | private const string metadataApi = "https://www.altinn.no/api/metadata"; 16 | private static HttpClient client = new HttpClient(); 17 | 18 | /// 19 | /// Gets all active reporting services from Altinn 2 20 | /// 21 | /// A list with all active reporting services from Altinn 2 22 | public static async Task> GetReportingServices() 23 | { 24 | List reportingServices = null; 25 | 26 | string path = $"{metadataApi}?$filter=ServiceType%20eq%20%27FormTask%27"; 27 | 28 | HttpResponseMessage response = await client.GetAsync(path); 29 | 30 | if (response.IsSuccessStatusCode) 31 | { 32 | string content = await response.Content.ReadAsStringAsync(); 33 | reportingServices = JsonSerializer.Deserialize>(content); 34 | } 35 | 36 | return reportingServices ?? new List(); 37 | } 38 | 39 | /// 40 | /// Gets the metadata for a specific reporting service from Altinn 2 41 | /// 42 | /// The Altinn 2 service description. 43 | /// The reporting service metadata description. 44 | public static async Task GetReportingService(Altinn2Service altinn2Service) 45 | { 46 | string path = ReportingServiceMetadataUrl(altinn2Service); 47 | Altinn2ReportingService reportingService = null; 48 | 49 | HttpResponseMessage response = await client.GetAsync(path); 50 | if (response.IsSuccessStatusCode) 51 | { 52 | string content = await response.Content.ReadAsStringAsync(); 53 | reportingService = JsonSerializer.Deserialize(content); 54 | } 55 | 56 | return reportingService; 57 | } 58 | 59 | /// 60 | /// Get the XSD for a specific form. 61 | /// 62 | /// The service metadata description. 63 | /// The form metadata description. 64 | /// The XSD loaded into an 65 | public static async Task GetFormXsd(Altinn2Service altinn2Service, Altinn2Form formMetaData) 66 | { 67 | string path = FormXsdUrl(altinn2Service, formMetaData); 68 | 69 | using HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Get, path); 70 | requestMessage.Headers.Add("Accept", "application/xml"); 71 | 72 | HttpResponseMessage response = await client.SendAsync(requestMessage); 73 | 74 | if (response.IsSuccessStatusCode) 75 | { 76 | XDocument doc = await XDocument.LoadAsync( 77 | await response.Content.ReadAsStreamAsync(), 78 | LoadOptions.None, 79 | CancellationToken.None); 80 | 81 | return doc; 82 | } 83 | 84 | return null; 85 | } 86 | 87 | private static string ReportingServiceMetadataUrl(Altinn2Service altinnResource) 88 | { 89 | return $"{metadataApi}/formtask/{altinnResource.ServiceCode}/{altinnResource.ServiceEditionCode}"; 90 | } 91 | 92 | private static string FormXsdUrl(Altinn2Service altinnResource, Altinn2Form formMetaData) 93 | { 94 | return ReportingServiceMetadataUrl(altinnResource) 95 | + $"/forms/{formMetaData.DataFormatID}/{formMetaData.DataFormatVersion}/xsd"; 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /AppsMonitoring/infra/deployment/base/deployment.yaml: -------------------------------------------------------------------------------- 1 | apiVersion: apps/v1 2 | kind: Deployment 3 | metadata: 4 | name: apps-monitor 5 | annotations: 6 | altinn.no/image: altinncr.azurecr.io/apps-monitor/image:latest 7 | altinn.no/image-tag: latest 8 | spec: 9 | minReadySeconds: 3 10 | revisionHistoryLimit: 5 11 | progressDeadlineSeconds: 60 12 | # We use a single replica and Recreate strategy since this is a stateful application 13 | replicas: 1 14 | strategy: 15 | # Recreate means all existing pods (1) are killed before new ones are created 16 | type: Recreate 17 | selector: 18 | matchLabels: 19 | app: apps-monitor 20 | template: 21 | metadata: 22 | labels: 23 | app: apps-monitor 24 | # We use workload identity, but service account is created through Terraform 25 | azure.workload.identity/use: "true" 26 | annotations: 27 | linkerd.io/inject: enabled 28 | spec: 29 | # SA is generated in terraform due to the Workload Identity client ID being generated in Terraform 30 | serviceAccountName: apps-monitor-sa 31 | # .NET docker images run with '1654', can verify with 32 | # docker image inspect mcr.microsoft.com/dotnet/aspnet:9.0-noble-chiseled-extra 33 | # Which is what we use in the Dockerfile 34 | securityContext: 35 | runAsUser: 1654 36 | runAsGroup: 1654 37 | fsGroup: 1654 38 | runAsNonRoot: true 39 | containers: 40 | - name: apps-monitor 41 | image: "" 42 | imagePullPolicy: Always 43 | securityContext: 44 | allowPrivilegeEscalation: false 45 | readOnlyRootFilesystem: true 46 | privileged: false 47 | capabilities: 48 | drop: 49 | - ALL 50 | ports: 51 | - name: http 52 | containerPort: 5156 53 | protocol: TCP 54 | env: 55 | - name: ASPNETCORE_URLS 56 | value: http://*:5156 57 | - name: TMPDIR 58 | value: /tmp 59 | - name: AppConfiguration__KeyVaultUri 60 | valueFrom: 61 | secretKeyRef: 62 | name: apps-monitor-kvconfig 63 | key: keyvault_uri 64 | - name: OTEL_EXPORTER_OTLP_ENDPOINT 65 | value: http://otel-collector.monitoring.svc.cluster.local:4317 66 | - name: OTEL_EXPORTER_OTLP_PROTOCOL 67 | value: grpc 68 | - name: OTEL_DOTNET_EXPERIMENTAL_OTLP_RETRY 69 | value: disk 70 | - name: OTEL_DOTNET_EXPERIMENTAL_OTLP_DISK_RETRY_DIRECTORY_PATH 71 | value: /telemetry 72 | - name: K8S_NODE_NAME 73 | valueFrom: 74 | fieldRef: 75 | apiVersion: v1 76 | fieldPath: spec.nodeName 77 | - name: K8S_NAMESPACE_NAME 78 | valueFrom: 79 | fieldRef: 80 | fieldPath: metadata.namespace 81 | - name: K8S_POD_NAME 82 | valueFrom: 83 | fieldRef: 84 | fieldPath: metadata.name 85 | - name: K8S_POD_UID 86 | valueFrom: 87 | fieldRef: 88 | fieldPath: metadata.uid 89 | - name: K8S_CONTAINER_IMAGE_TAG 90 | value: "" 91 | - name: OTEL_RESOURCE_ATTRIBUTES 92 | value: "k8s.node.name=$(K8S_NODE_NAME),\ 93 | k8s.namespace.name=$(K8S_NAMESPACE_NAME),\ 94 | k8s.pod.name=$(K8S_POD_NAME),\ 95 | k8s.pod.uid=$(K8S_POD_UID),\ 96 | service.namespace=altinn,\ 97 | service.name=apps-monitor,\ 98 | service.instance.id=$(K8S_POD_NAME),\ 99 | service.version=$(K8S_CONTAINER_IMAGE_TAG)" 100 | readinessProbe: 101 | httpGet: 102 | path: /health/ready 103 | port: 5156 104 | initialDelaySeconds: 5 105 | periodSeconds: 3 106 | livenessProbe: 107 | httpGet: 108 | path: /health/live 109 | port: 5156 110 | initialDelaySeconds: 5 111 | periodSeconds: 3 112 | resources: 113 | limits: 114 | cpu: 1000m 115 | memory: 512Mi 116 | requests: 117 | cpu: 100m 118 | memory: 128Mi 119 | volumeMounts: 120 | - name: telemetry 121 | mountPath: /telemetry 122 | - name: tmp 123 | mountPath: /tmp 124 | volumes: 125 | - name: telemetry 126 | emptyDir: 127 | sizeLimit: 128Mi 128 | - name: tmp 129 | emptyDir: 130 | sizeLimit: 64Mi 131 | -------------------------------------------------------------------------------- /RepoCleanup/Data/orgs.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "username": "dpa", 4 | "full_name": "Datatilsynet", 5 | "website": "https://www.datatilsynet.no/", 6 | "visibility": "public", 7 | "repo_admin_change_team_access": false 8 | }, 9 | { 10 | "username": "dsb", 11 | "full_name": "Direktoratet for samfunnssikkerhet og beredskap", 12 | "website": "https://www.dsb.no/", 13 | "visibility": "public", 14 | "repo_admin_change_team_access": false 15 | }, 16 | { 17 | "username": "fk", 18 | "full_name": "Fellesordningen for avtalefestet pensjon", 19 | "website": "https://www.afp.no/", 20 | "visibility": "public", 21 | "repo_admin_change_team_access": false 22 | }, 23 | { 24 | "username": "krt", 25 | "full_name": "Finanstilsynet", 26 | "website": "https://www.finanstilsynet.no/", 27 | "visibility": "public", 28 | "repo_admin_change_team_access": false 29 | }, 30 | { 31 | "username": "kyv", 32 | "full_name": "Kystverket", 33 | "website": "https://www.kystverket.no/", 34 | "visibility": "public", 35 | "repo_admin_change_team_access": false 36 | }, 37 | { 38 | "username": "lt", 39 | "full_name": "Luftfartstilsynet", 40 | "website": "https://luftfartstilsynet.no/", 41 | "visibility": "public", 42 | "repo_admin_change_team_access": false 43 | }, 44 | { 45 | "username": "lts", 46 | "full_name": "Lotteri- og stiftelsestilsynet", 47 | "website": "https://lottstift.no/", 48 | "visibility": "public", 49 | "repo_admin_change_team_access": false 50 | }, 51 | { 52 | "username": "mat", 53 | "full_name": "Mattilsynet", 54 | "website": "https://mattilsynet.no/", 55 | "visibility": "public", 56 | "repo_admin_change_team_access": false 57 | }, 58 | { 59 | "username": "npe", 60 | "full_name": "Norsk pasientskadeerstatning", 61 | "website": "https://www.npe.no/", 62 | "visibility": "public", 63 | "repo_admin_change_team_access": false 64 | }, 65 | { 66 | "username": "ok", 67 | "full_name": "Oslo kommune", 68 | "website": "https://www.oslo.kommune.no/", 69 | "visibility": "public", 70 | "repo_admin_change_team_access": false 71 | }, 72 | { 73 | "username": "oko", 74 | "full_name": "Økokrim", 75 | "website": "https://www.okokrim.no/", 76 | "visibility": "public", 77 | "repo_admin_change_team_access": false 78 | }, 79 | { 80 | "username": "pat", 81 | "full_name": "Patenstyret", 82 | "website": "https://www.patentstyret.no/", 83 | "visibility": "public", 84 | "repo_admin_change_team_access": false 85 | }, 86 | { 87 | "username": "pod", 88 | "full_name": "Politiet", 89 | "website": "https://www.politiet.no/", 90 | "visibility": "public", 91 | "repo_admin_change_team_access": false 92 | }, 93 | { 94 | "username": "sfd", 95 | "full_name": "Sjøfartsdirektoratet", 96 | "website": "https://www.sdir.no/", 97 | "visibility": "public", 98 | "repo_admin_change_team_access": false 99 | }, 100 | { 101 | "username": "sht", 102 | "full_name": "Statens havarikommisjon", 103 | "website": "https://havarikommisjonen.no/", 104 | "visibility": "public", 105 | "repo_admin_change_team_access": false 106 | }, 107 | { 108 | "username": "slf", 109 | "full_name": "Landbruksdirektoratet", 110 | "website": "https://www.landbruksdirektoratet.no/", 111 | "visibility": "public", 112 | "repo_admin_change_team_access": false 113 | }, 114 | { 115 | "username": "slv", 116 | "full_name": "Statens legemiddelverk", 117 | "website": "https://legemiddelverket.no/", 118 | "visibility": "public", 119 | "repo_admin_change_team_access": false 120 | }, 121 | { 122 | "username": "spk", 123 | "full_name": "Statens pensjonskasse", 124 | "website": "https://www.spk.no/", 125 | "visibility": "public", 126 | "repo_admin_change_team_access": false 127 | }, 128 | { 129 | "username": "tad", 130 | "full_name": "Tolldirektoratet", 131 | "website": "https://www.toll.no/", 132 | "visibility": "public", 133 | "repo_admin_change_team_access": false 134 | }, 135 | { 136 | "username": "tra", 137 | "full_name": "Tilsynsrådet for advokatvirksomhet", 138 | "website": "https://tilsynet.no/", 139 | "visibility": "public", 140 | "repo_admin_change_team_access": false 141 | } 142 | ] -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/Azure/AzureServiceOwnerResources.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel; 2 | using Altinn.Apps.Monitoring.Domain; 3 | using Azure; 4 | using Azure.Core; 5 | using Azure.Monitor.Query; 6 | using Azure.Monitor.Query.Models; 7 | using Azure.ResourceManager.OperationalInsights; 8 | using Microsoft.Extensions.Caching.Hybrid; 9 | using Microsoft.Extensions.Options; 10 | 11 | namespace Altinn.Apps.Monitoring.Application.Azure; 12 | 13 | internal sealed class AzureServiceOwnerResources( 14 | ILogger logger, 15 | IOptionsMonitor config, 16 | AzureClients clients, 17 | HybridCache cache, 18 | Telemetry telemetry 19 | ) 20 | { 21 | private readonly ILogger _logger = logger; 22 | private readonly IOptionsMonitor _config = config; 23 | private readonly AzureClients _clients = clients; 24 | private readonly HybridCache _cache = cache; 25 | private readonly Telemetry _telemetry = telemetry; 26 | 27 | private readonly HybridCacheEntryOptions _cacheEntryOptions = new() 28 | { 29 | Expiration = TimeSpan.FromMinutes(30), 30 | LocalCacheExpiration = TimeSpan.FromMinutes(30), 31 | }; 32 | 33 | public ValueTask GetResources( 34 | ServiceOwner serviceOwner, 35 | CancellationToken cancellationToken 36 | ) 37 | { 38 | using var activity = _telemetry.Activities.StartActivity("AzureServiceOwnerResources.GetResources"); 39 | activity?.SetTag("serviceowner", serviceOwner.Value); 40 | return _cache.GetOrCreateAsync( 41 | $"{nameof(AzureServiceOwnerResources)}-{serviceOwner.Value}", 42 | (this, serviceOwner), 43 | static async ValueTask (state, cancellationToken) => 44 | { 45 | var (self, serviceOwner) = state; 46 | var config = self._config.CurrentValue; 47 | 48 | if (string.IsNullOrWhiteSpace(serviceOwner.ExtId)) 49 | return null; 50 | 51 | var env = config.AltinnEnvironment; 52 | 53 | ResourceIdentifier workspaceId; 54 | try 55 | { 56 | workspaceId = OperationalInsightsWorkspaceResource.CreateResourceIdentifier( 57 | serviceOwner.ExtId, 58 | $"monitor-{serviceOwner.Value}-{env}-rg", 59 | $"application-{serviceOwner.Value}-{env}-law" 60 | ); 61 | } 62 | catch (Exception ex) 63 | { 64 | self._logger.LogWarning( 65 | ex, 66 | "Failed to create workspace ID for service owner {ServiceOwner}. Subscription ID: '{SubscriptionId}'", 67 | serviceOwner.Value, 68 | serviceOwner.ExtId 69 | ); 70 | return null; 71 | } 72 | 73 | try 74 | { 75 | Response results = await self._clients.LogsQueryClient.QueryResourceAsync( 76 | workspaceId, 77 | "AppDependencies | project TimeGenerated", 78 | new QueryTimeRange(TimeSpan.FromMinutes(5)), 79 | cancellationToken: cancellationToken 80 | ); 81 | if (results.Value.Status != LogsQueryResultStatus.Success) 82 | { 83 | self._logger.LogWarning( 84 | "Failed to probe workspace '{WorkspaceId}' for service owner {ServiceOwner}: {Status}/{Error}", 85 | workspaceId, 86 | serviceOwner.Value, 87 | results.Value.Status, 88 | results.Value.Error 89 | ); 90 | return null; 91 | } 92 | } 93 | catch (Exception ex) 94 | { 95 | self._logger.LogWarning( 96 | ex, 97 | "Failed to probe workspace '{WorkspaceId}' for service owner {ServiceOwner}", 98 | workspaceId, 99 | serviceOwner.Value 100 | ); 101 | return null; 102 | } 103 | 104 | return new(workspaceId); 105 | }, 106 | options: _cacheEntryOptions, 107 | cancellationToken: cancellationToken 108 | ); 109 | } 110 | } 111 | 112 | [ImmutableObject(true)] 113 | internal sealed record AzureServiceOwnerResourcesRecord(ResourceIdentifier WorkspaceId); 114 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Db/TestData.cs: -------------------------------------------------------------------------------- 1 | using Altinn.Apps.Monitoring.Application.Db; 2 | using Altinn.Apps.Monitoring.Domain; 3 | using NodaTime.Text; 4 | 5 | namespace Altinn.Apps.Monitoring.Tests.Application.Db; 6 | 7 | internal static class TestData 8 | { 9 | public static TraceData GenerateTelemetryTraceData( 10 | int? altinnErrorId = null, 11 | int? instanceOwnerPartyId = null, 12 | Guid? instanceId = null, 13 | string? traceId = null, 14 | string? spanId = null, 15 | string? parentSpanId = null, 16 | string? traceName = null, 17 | string? spanName = null, 18 | bool? success = null, 19 | string? result = null, 20 | Duration? duration = null, 21 | Dictionary? attributes = null 22 | ) 23 | { 24 | return new TraceData 25 | { 26 | AltinnErrorId = altinnErrorId ?? 1, 27 | InstanceOwnerPartyId = instanceOwnerPartyId ?? 2, 28 | InstanceId = instanceId ?? Guid.Parse("12525089-e367-4c64-bc8a-eb1b901553c9"), 29 | TraceId = traceId ?? "trace-id", 30 | SpanId = spanId ?? "span-id", 31 | ParentSpanId = parentSpanId ?? "parent-span-id", 32 | TraceName = traceName ?? "trace-name", 33 | SpanName = spanName ?? "span-name", 34 | Success = success ?? false, 35 | Result = result ?? "result", 36 | Duration = duration ?? Duration.FromMilliseconds(100), 37 | Attributes = attributes ?? new() { ["key1"] = "value1", ["key2"] = "value2" }, 38 | }; 39 | } 40 | 41 | public static TelemetryEntity GenerateTelemetryEntity( 42 | string? extId = null, 43 | string? serviceOwner = null, 44 | string? appName = null, 45 | string? appVersion = null, 46 | Instant? timeGenerated = null, 47 | Instant? timeIngested = null, 48 | int? dupeCount = null, 49 | bool? seeded = null, 50 | Func? dataGenerator = null, 51 | TimeProvider? timeProvider = null 52 | ) 53 | { 54 | timeProvider ??= TimeProvider.System; 55 | 56 | return new TelemetryEntity 57 | { 58 | Id = 0, 59 | ExtId = extId ?? "ext-id", 60 | ServiceOwner = serviceOwner ?? "so", 61 | AppName = appName ?? "app-name", 62 | AppVersion = appVersion ?? "8.0.0", 63 | TimeGenerated = timeGenerated ?? timeProvider.GetCurrentInstant().Minus(Duration.FromMinutes(15)), 64 | TimeIngested = timeIngested ?? timeProvider.GetCurrentInstant(), 65 | DupeCount = dupeCount ?? 0, 66 | Seeded = seeded ?? false, 67 | Data = dataGenerator?.Invoke() ?? GenerateTelemetryTraceData(), 68 | }; 69 | } 70 | 71 | public static TelemetryEntity GenerateMiniDbTrace( 72 | ServiceOwner serviceOwner, 73 | ref long id, 74 | Instant timeGenerated, 75 | TimeProvider timeProvider 76 | ) 77 | { 78 | var spanId = $"90c159bde9b1a6c{id++}"; 79 | return TestData.GenerateTelemetryEntity( 80 | extId: $"75563ff0b3251e04c70362c5a3495174-{spanId}", // Matches Azure adapter 81 | serviceOwner: serviceOwner.Value, 82 | appName: "formueinntekt-skattemelding-v2", 83 | appVersion: "8.0.8", 84 | timeGenerated: timeGenerated, 85 | timeIngested: Instant.MinValue, 86 | dupeCount: 0, 87 | seeded: false, 88 | dataGenerator: () => 89 | TestData.GenerateTelemetryTraceData( 90 | altinnErrorId: 1, 91 | instanceOwnerPartyId: 123, 92 | instanceId: Guid.Parse("1d449be1-7114-405c-aeee-1f09799f7b74"), 93 | traceId: "75563ff0b3251e04c70362c5a3495174", 94 | spanId: spanId, 95 | parentSpanId: "7e7143a41c29e532", 96 | traceName: "PUT Process/NextElement [app/instanceGuid/instanceOwnerPartyId/org]", 97 | spanName: "POST /storage/api/v1/instances/123/1d449be1-7114-405c-aeee-1f09799f7b74/events", 98 | success: false, 99 | result: "Faulted", 100 | duration: DurationPattern.Roundtrip.Parse("0:00:00:27.478494").Value, 101 | attributes: new() 102 | { 103 | ["Data"] = 104 | "https://platform.altinn.no/storage/api/v1/instances/123/1d449be1-7114-405c-aeee-1f09799f7b74/events", 105 | ["DependencyType"] = "HTTP", 106 | ["PerformanceBucket"] = "15sec-30sec", 107 | ["Properties"] = 108 | """{"AspNetCoreEnvironment":"Production","_MS.ProcessedByMetricExtractors":"(Name:'Dependencies', Ver:'1.1')"}""", 109 | ["Target"] = "platform.altinn.no", 110 | } 111 | ), 112 | timeProvider: timeProvider 113 | ); 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Slack/_snapshots/SlackAlerterTests.Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry_case=200-ok_waitForRetryEvents=0.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | OrchestratorEvents: [ 3 | { 4 | ServiceOwner: { 5 | Value: skd 6 | }, 7 | Query: { 8 | Name: query, 9 | Type: Traces, 10 | QueryTemplate: template-{searchFrom}-{searchTo}, 11 | Hash: HpynynzeF9Pk52Vahp477Q== 12 | }, 13 | SearchFrom: 2024-11-22T11:59:59Z, 14 | SearchTo: 2025-02-20T11:50:00Z, 15 | Telemetry: [ 16 | { 17 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 18 | ServiceOwner: skd, 19 | AppName: formueinntekt-skattemelding-v2, 20 | AppVersion: 8.0.8, 21 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 22 | TimeIngested: 2025-02-20T12:00:05Z, 23 | Seeded: false, 24 | Data: {Scrubbed} 25 | }, 26 | { 27 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 28 | ServiceOwner: skd, 29 | AppName: formueinntekt-skattemelding-v2, 30 | AppVersion: 8.0.8, 31 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 32 | TimeIngested: 2025-02-20T12:00:05Z, 33 | Seeded: false, 34 | Data: {Scrubbed} 35 | } 36 | ], 37 | Result: { 38 | Written: 1, 39 | Ids: [ 40 | 1, 41 | 5 42 | ], 43 | DupeExtIds: [ 44 | 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1 45 | ] 46 | } 47 | } 48 | ], 49 | AlerterEvents: [ 50 | { 51 | Item: { 52 | Id: 5, 53 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 54 | ServiceOwner: skd, 55 | AppName: formueinntekt-skattemelding-v2, 56 | AppVersion: 8.0.8, 57 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 58 | TimeIngested: 2025-02-20T12:00:05Z, 59 | Seeded: false, 60 | Data: {Scrubbed} 61 | }, 62 | AlertBefore: { 63 | State: Pending, 64 | TelemetryId: 5, 65 | Data: {}, 66 | CreatedAt: {Scrubbed}, 67 | UpdatedAt: {Scrubbed} 68 | }, 69 | AlertAfter: { 70 | State: Alerted, 71 | TelemetryId: 5, 72 | Data: { 73 | Channel: C01UJ9G, 74 | Message: 75 | *ALERT* `2025-02-15T14:56:04Z`: 76 | - App: *skd*/*formueinntekt-skattemelding-v2*/*8.0.8* 77 | - Feil: *POST /storage/api/v1/instances/123/1d449be1-7114-405c-aeee-1f09799f7b74/events* (status *Faulted*, *27478.49ms*) 78 | - Instansen: *123*/*1d449be1-7114-405c-aeee-1f09799f7b74* 79 | - Operation ID: *75563ff0b3251e04c70362c5a3495174*, 80 | ThreadTs: 1634160000.000100 81 | }, 82 | CreatedAt: {Scrubbed}, 83 | UpdatedAt: {Scrubbed} 84 | } 85 | } 86 | ], 87 | State: { 88 | Telemetry: [ 89 | { 90 | Id: 2, 91 | ExtId: 4ba19e3f5a545728934b1f921e06d92b-f86a2aed5c5d9f63, 92 | ServiceOwner: tad, 93 | AppName: bku, 94 | AppVersion: 6.0.35, 95 | TimeGenerated: 2025-02-19T08:29:04.386517Z, 96 | TimeIngested: 2025-02-19T08:34:32.406732Z, 97 | Seeded: true, 98 | Data: {Scrubbed} 99 | }, 100 | { 101 | Id: 3, 102 | ExtId: f2b6eea456788828f6195fbce59f740f-e0135dfc441dc49a, 103 | ServiceOwner: ssb, 104 | AppName: ra1000-01, 105 | AppVersion: 6.0.26, 106 | TimeGenerated: 2025-02-19T16:07:44.720544Z, 107 | TimeIngested: 2025-02-19T16:00:13.785543Z, 108 | Seeded: true, 109 | Data: {Scrubbed} 110 | }, 111 | { 112 | Id: 1, 113 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 114 | ServiceOwner: skd, 115 | AppName: formueinntekt-skattemelding-v2, 116 | AppVersion: 8.0.8, 117 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 118 | TimeIngested: 2025-02-19T07:34:32.420042Z, 119 | DupeCount: 1, 120 | Seeded: true, 121 | Data: {Scrubbed} 122 | }, 123 | { 124 | Id: 5, 125 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 126 | ServiceOwner: skd, 127 | AppName: formueinntekt-skattemelding-v2, 128 | AppVersion: 8.0.8, 129 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 130 | TimeIngested: 2025-02-20T12:00:05Z, 131 | Seeded: false, 132 | Data: {Scrubbed} 133 | } 134 | ], 135 | Alerts: [ 136 | { 137 | Id: 1, 138 | State: Alerted, 139 | TelemetryId: 5, 140 | Data: { 141 | Channel: C01UJ9G, 142 | Message: 143 | *ALERT* `2025-02-15T14:56:04Z`: 144 | - App: *skd*/*formueinntekt-skattemelding-v2*/*8.0.8* 145 | - Feil: *POST /storage/api/v1/instances/123/1d449be1-7114-405c-aeee-1f09799f7b74/events* (status *Faulted*, *27478.49ms*) 146 | - Instansen: *123*/*1d449be1-7114-405c-aeee-1f09799f7b74* 147 | - Operation ID: *75563ff0b3251e04c70362c5a3495174*, 148 | ThreadTs: 1634160000.000100 149 | }, 150 | CreatedAt: {Scrubbed}, 151 | UpdatedAt: {Scrubbed} 152 | } 153 | ] 154 | } 155 | } -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Slack/_snapshots/SlackAlerterTests.Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry_case=500-error-then-ok_waitForRetryEvents=0.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | OrchestratorEvents: [ 3 | { 4 | ServiceOwner: { 5 | Value: skd 6 | }, 7 | Query: { 8 | Name: query, 9 | Type: Traces, 10 | QueryTemplate: template-{searchFrom}-{searchTo}, 11 | Hash: HpynynzeF9Pk52Vahp477Q== 12 | }, 13 | SearchFrom: 2024-11-22T11:59:59Z, 14 | SearchTo: 2025-02-20T11:50:00Z, 15 | Telemetry: [ 16 | { 17 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 18 | ServiceOwner: skd, 19 | AppName: formueinntekt-skattemelding-v2, 20 | AppVersion: 8.0.8, 21 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 22 | TimeIngested: 2025-02-20T12:00:05Z, 23 | Seeded: false, 24 | Data: {Scrubbed} 25 | }, 26 | { 27 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 28 | ServiceOwner: skd, 29 | AppName: formueinntekt-skattemelding-v2, 30 | AppVersion: 8.0.8, 31 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 32 | TimeIngested: 2025-02-20T12:00:05Z, 33 | Seeded: false, 34 | Data: {Scrubbed} 35 | } 36 | ], 37 | Result: { 38 | Written: 1, 39 | Ids: [ 40 | 1, 41 | 5 42 | ], 43 | DupeExtIds: [ 44 | 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1 45 | ] 46 | } 47 | } 48 | ], 49 | AlerterEvents: [ 50 | { 51 | Item: { 52 | Id: 5, 53 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 54 | ServiceOwner: skd, 55 | AppName: formueinntekt-skattemelding-v2, 56 | AppVersion: 8.0.8, 57 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 58 | TimeIngested: 2025-02-20T12:00:05Z, 59 | Seeded: false, 60 | Data: {Scrubbed} 61 | }, 62 | AlertBefore: { 63 | State: Pending, 64 | TelemetryId: 5, 65 | Data: {}, 66 | CreatedAt: {Scrubbed}, 67 | UpdatedAt: {Scrubbed} 68 | }, 69 | AlertAfter: { 70 | State: Alerted, 71 | TelemetryId: 5, 72 | Data: { 73 | Channel: C01UJ9G, 74 | Message: 75 | *ALERT* `2025-02-15T14:56:04Z`: 76 | - App: *skd*/*formueinntekt-skattemelding-v2*/*8.0.8* 77 | - Feil: *POST /storage/api/v1/instances/123/1d449be1-7114-405c-aeee-1f09799f7b74/events* (status *Faulted*, *27478.49ms*) 78 | - Instansen: *123*/*1d449be1-7114-405c-aeee-1f09799f7b74* 79 | - Operation ID: *75563ff0b3251e04c70362c5a3495174*, 80 | ThreadTs: 1634160000.000100 81 | }, 82 | CreatedAt: {Scrubbed}, 83 | UpdatedAt: {Scrubbed} 84 | } 85 | } 86 | ], 87 | State: { 88 | Telemetry: [ 89 | { 90 | Id: 2, 91 | ExtId: 4ba19e3f5a545728934b1f921e06d92b-f86a2aed5c5d9f63, 92 | ServiceOwner: tad, 93 | AppName: bku, 94 | AppVersion: 6.0.35, 95 | TimeGenerated: 2025-02-19T08:29:04.386517Z, 96 | TimeIngested: 2025-02-19T08:34:32.406732Z, 97 | Seeded: true, 98 | Data: {Scrubbed} 99 | }, 100 | { 101 | Id: 3, 102 | ExtId: f2b6eea456788828f6195fbce59f740f-e0135dfc441dc49a, 103 | ServiceOwner: ssb, 104 | AppName: ra1000-01, 105 | AppVersion: 6.0.26, 106 | TimeGenerated: 2025-02-19T16:07:44.720544Z, 107 | TimeIngested: 2025-02-19T16:00:13.785543Z, 108 | Seeded: true, 109 | Data: {Scrubbed} 110 | }, 111 | { 112 | Id: 1, 113 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 114 | ServiceOwner: skd, 115 | AppName: formueinntekt-skattemelding-v2, 116 | AppVersion: 8.0.8, 117 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 118 | TimeIngested: 2025-02-19T07:34:32.420042Z, 119 | DupeCount: 1, 120 | Seeded: true, 121 | Data: {Scrubbed} 122 | }, 123 | { 124 | Id: 5, 125 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 126 | ServiceOwner: skd, 127 | AppName: formueinntekt-skattemelding-v2, 128 | AppVersion: 8.0.8, 129 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 130 | TimeIngested: 2025-02-20T12:00:05Z, 131 | Seeded: false, 132 | Data: {Scrubbed} 133 | } 134 | ], 135 | Alerts: [ 136 | { 137 | Id: 1, 138 | State: Alerted, 139 | TelemetryId: 5, 140 | Data: { 141 | Channel: C01UJ9G, 142 | Message: 143 | *ALERT* `2025-02-15T14:56:04Z`: 144 | - App: *skd*/*formueinntekt-skattemelding-v2*/*8.0.8* 145 | - Feil: *POST /storage/api/v1/instances/123/1d449be1-7114-405c-aeee-1f09799f7b74/events* (status *Faulted*, *27478.49ms*) 146 | - Instansen: *123*/*1d449be1-7114-405c-aeee-1f09799f7b74* 147 | - Operation ID: *75563ff0b3251e04c70362c5a3495174*, 148 | ThreadTs: 1634160000.000100 149 | }, 150 | CreatedAt: {Scrubbed}, 151 | UpdatedAt: {Scrubbed} 152 | } 153 | ] 154 | } 155 | } -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Slack/_snapshots/SlackAlerterTests.Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry_case=429-ratelimited-then-ok_waitForRetryEvents=0.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | OrchestratorEvents: [ 3 | { 4 | ServiceOwner: { 5 | Value: skd 6 | }, 7 | Query: { 8 | Name: query, 9 | Type: Traces, 10 | QueryTemplate: template-{searchFrom}-{searchTo}, 11 | Hash: HpynynzeF9Pk52Vahp477Q== 12 | }, 13 | SearchFrom: 2024-11-22T11:59:59Z, 14 | SearchTo: 2025-02-20T11:50:00Z, 15 | Telemetry: [ 16 | { 17 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 18 | ServiceOwner: skd, 19 | AppName: formueinntekt-skattemelding-v2, 20 | AppVersion: 8.0.8, 21 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 22 | TimeIngested: 2025-02-20T12:00:05Z, 23 | Seeded: false, 24 | Data: {Scrubbed} 25 | }, 26 | { 27 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 28 | ServiceOwner: skd, 29 | AppName: formueinntekt-skattemelding-v2, 30 | AppVersion: 8.0.8, 31 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 32 | TimeIngested: 2025-02-20T12:00:05Z, 33 | Seeded: false, 34 | Data: {Scrubbed} 35 | } 36 | ], 37 | Result: { 38 | Written: 1, 39 | Ids: [ 40 | 1, 41 | 5 42 | ], 43 | DupeExtIds: [ 44 | 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1 45 | ] 46 | } 47 | } 48 | ], 49 | AlerterEvents: [ 50 | { 51 | Item: { 52 | Id: 5, 53 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 54 | ServiceOwner: skd, 55 | AppName: formueinntekt-skattemelding-v2, 56 | AppVersion: 8.0.8, 57 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 58 | TimeIngested: 2025-02-20T12:00:05Z, 59 | Seeded: false, 60 | Data: {Scrubbed} 61 | }, 62 | AlertBefore: { 63 | State: Pending, 64 | TelemetryId: 5, 65 | Data: {}, 66 | CreatedAt: {Scrubbed}, 67 | UpdatedAt: {Scrubbed} 68 | }, 69 | AlertAfter: { 70 | State: Alerted, 71 | TelemetryId: 5, 72 | Data: { 73 | Channel: C01UJ9G, 74 | Message: 75 | *ALERT* `2025-02-15T14:56:04Z`: 76 | - App: *skd*/*formueinntekt-skattemelding-v2*/*8.0.8* 77 | - Feil: *POST /storage/api/v1/instances/123/1d449be1-7114-405c-aeee-1f09799f7b74/events* (status *Faulted*, *27478.49ms*) 78 | - Instansen: *123*/*1d449be1-7114-405c-aeee-1f09799f7b74* 79 | - Operation ID: *75563ff0b3251e04c70362c5a3495174*, 80 | ThreadTs: 1634160000.000100 81 | }, 82 | CreatedAt: {Scrubbed}, 83 | UpdatedAt: {Scrubbed} 84 | } 85 | } 86 | ], 87 | State: { 88 | Telemetry: [ 89 | { 90 | Id: 2, 91 | ExtId: 4ba19e3f5a545728934b1f921e06d92b-f86a2aed5c5d9f63, 92 | ServiceOwner: tad, 93 | AppName: bku, 94 | AppVersion: 6.0.35, 95 | TimeGenerated: 2025-02-19T08:29:04.386517Z, 96 | TimeIngested: 2025-02-19T08:34:32.406732Z, 97 | Seeded: true, 98 | Data: {Scrubbed} 99 | }, 100 | { 101 | Id: 3, 102 | ExtId: f2b6eea456788828f6195fbce59f740f-e0135dfc441dc49a, 103 | ServiceOwner: ssb, 104 | AppName: ra1000-01, 105 | AppVersion: 6.0.26, 106 | TimeGenerated: 2025-02-19T16:07:44.720544Z, 107 | TimeIngested: 2025-02-19T16:00:13.785543Z, 108 | Seeded: true, 109 | Data: {Scrubbed} 110 | }, 111 | { 112 | Id: 1, 113 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 114 | ServiceOwner: skd, 115 | AppName: formueinntekt-skattemelding-v2, 116 | AppVersion: 8.0.8, 117 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 118 | TimeIngested: 2025-02-19T07:34:32.420042Z, 119 | DupeCount: 1, 120 | Seeded: true, 121 | Data: {Scrubbed} 122 | }, 123 | { 124 | Id: 5, 125 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 126 | ServiceOwner: skd, 127 | AppName: formueinntekt-skattemelding-v2, 128 | AppVersion: 8.0.8, 129 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 130 | TimeIngested: 2025-02-20T12:00:05Z, 131 | Seeded: false, 132 | Data: {Scrubbed} 133 | } 134 | ], 135 | Alerts: [ 136 | { 137 | Id: 1, 138 | State: Alerted, 139 | TelemetryId: 5, 140 | Data: { 141 | Channel: C01UJ9G, 142 | Message: 143 | *ALERT* `2025-02-15T14:56:04Z`: 144 | - App: *skd*/*formueinntekt-skattemelding-v2*/*8.0.8* 145 | - Feil: *POST /storage/api/v1/instances/123/1d449be1-7114-405c-aeee-1f09799f7b74/events* (status *Faulted*, *27478.49ms*) 146 | - Instansen: *123*/*1d449be1-7114-405c-aeee-1f09799f7b74* 147 | - Operation ID: *75563ff0b3251e04c70362c5a3495174*, 148 | ThreadTs: 1634160000.000100 149 | }, 150 | CreatedAt: {Scrubbed}, 151 | UpdatedAt: {Scrubbed} 152 | } 153 | ] 154 | } 155 | } -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/_snapshots/OrchestratorTests.Orchestration_Progresses_Successfully_serviceOwner=skd_generator=WithSeeder.verified.txt: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | Desc: Iteration 1 - generator=WithSeeder, 4 | Start: 2025-02-20T12:00:00Z, 5 | End: 2025-02-20T12:00:05Z, 6 | StateBefore: { 7 | Telemetry: [ 8 | { 9 | Id: 1, 10 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 11 | ServiceOwner: skd, 12 | AppName: formueinntekt-skattemelding-v2, 13 | AppVersion: 8.0.8, 14 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 15 | TimeIngested: 2025-02-19T07:34:32.420042Z, 16 | Seeded: true, 17 | Data: {Scrubbed} 18 | }, 19 | { 20 | Id: 2, 21 | ExtId: 4ba19e3f5a545728934b1f921e06d92b-f86a2aed5c5d9f63, 22 | ServiceOwner: tad, 23 | AppName: bku, 24 | AppVersion: 6.0.35, 25 | TimeGenerated: 2025-02-19T08:29:04.386517Z, 26 | TimeIngested: 2025-02-19T08:34:32.406732Z, 27 | Seeded: true, 28 | Data: {Scrubbed} 29 | }, 30 | { 31 | Id: 3, 32 | ExtId: f2b6eea456788828f6195fbce59f740f-e0135dfc441dc49a, 33 | ServiceOwner: ssb, 34 | AppName: ra1000-01, 35 | AppVersion: 6.0.26, 36 | TimeGenerated: 2025-02-19T16:07:44.720544Z, 37 | TimeIngested: 2025-02-19T16:00:13.785543Z, 38 | Seeded: true, 39 | Data: {Scrubbed} 40 | } 41 | ], 42 | Queries: [] 43 | }, 44 | Input: { 45 | ReportedEvents: [ 46 | { 47 | ServiceOwner: { 48 | Value: skd 49 | }, 50 | Query: { 51 | Name: query, 52 | Type: Traces, 53 | QueryTemplate: template-{searchFrom}-{searchTo}, 54 | Hash: HpynynzeF9Pk52Vahp477Q== 55 | }, 56 | SearchFrom: 2024-11-22T11:59:59Z, 57 | SearchTo: 2025-02-20T11:50:00Z, 58 | Telemetry: [ 59 | { 60 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 61 | ServiceOwner: skd, 62 | AppName: formueinntekt-skattemelding-v2, 63 | AppVersion: 8.0.8, 64 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 65 | TimeIngested: 2025-02-20T12:00:05Z, 66 | Seeded: false, 67 | Data: {Scrubbed} 68 | }, 69 | { 70 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 71 | ServiceOwner: skd, 72 | AppName: formueinntekt-skattemelding-v2, 73 | AppVersion: 8.0.8, 74 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 75 | TimeIngested: 2025-02-20T12:00:05Z, 76 | Seeded: false, 77 | Data: {Scrubbed} 78 | } 79 | ], 80 | Result: { 81 | Written: 1, 82 | Ids: [ 83 | 1, 84 | 5 85 | ], 86 | DupeExtIds: [ 87 | 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1 88 | ] 89 | } 90 | } 91 | ] 92 | }, 93 | StateAfter: { 94 | Telemetry: [ 95 | { 96 | Id: 2, 97 | ExtId: 4ba19e3f5a545728934b1f921e06d92b-f86a2aed5c5d9f63, 98 | ServiceOwner: tad, 99 | AppName: bku, 100 | AppVersion: 6.0.35, 101 | TimeGenerated: 2025-02-19T08:29:04.386517Z, 102 | TimeIngested: 2025-02-19T08:34:32.406732Z, 103 | Seeded: true, 104 | Data: {Scrubbed} 105 | }, 106 | { 107 | Id: 3, 108 | ExtId: f2b6eea456788828f6195fbce59f740f-e0135dfc441dc49a, 109 | ServiceOwner: ssb, 110 | AppName: ra1000-01, 111 | AppVersion: 6.0.26, 112 | TimeGenerated: 2025-02-19T16:07:44.720544Z, 113 | TimeIngested: 2025-02-19T16:00:13.785543Z, 114 | Seeded: true, 115 | Data: {Scrubbed} 116 | }, 117 | { 118 | Id: 1, 119 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 120 | ServiceOwner: skd, 121 | AppName: formueinntekt-skattemelding-v2, 122 | AppVersion: 8.0.8, 123 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 124 | TimeIngested: 2025-02-19T07:34:32.420042Z, 125 | DupeCount: 1, 126 | Seeded: true, 127 | Data: {Scrubbed} 128 | }, 129 | { 130 | Id: 5, 131 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 132 | ServiceOwner: skd, 133 | AppName: formueinntekt-skattemelding-v2, 134 | AppVersion: 8.0.8, 135 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 136 | TimeIngested: 2025-02-20T12:00:05Z, 137 | Seeded: false, 138 | Data: {Scrubbed} 139 | } 140 | ], 141 | Queries: [ 142 | { 143 | Id: 1, 144 | ServiceOwner: skd, 145 | Name: query, 146 | Hash: HpynynzeF9Pk52Vahp477Q==, 147 | QueriedUntil: 2025-02-15T14:56:04.906736Z 148 | } 149 | ] 150 | } 151 | } 152 | ] -------------------------------------------------------------------------------- /AppsMonitoring/src/Altinn.Apps.Monitoring/Application/DbUp/Migrator.cs: -------------------------------------------------------------------------------- 1 | using System.Data; 2 | using System.Reflection; 3 | using Altinn.Apps.Monitoring.Application.Db; 4 | using DbUp; 5 | using DbUp.Engine; 6 | using Microsoft.Extensions.Options; 7 | 8 | namespace Altinn.Apps.Monitoring.Application.DbUp; 9 | 10 | internal sealed class Migrator( 11 | ILogger logger, 12 | DistributedLocking locking, 13 | [FromKeyedServices(Config.AdminMode)] ConnectionString connectionStringAdmin, 14 | IOptions appConfiguration, 15 | Telemetry telemetry 16 | ) : IHostedService 17 | { 18 | private readonly ILogger _logger = logger; 19 | private readonly DistributedLocking _locking = locking; 20 | private readonly ConnectionString _connectionStringAdmin = connectionStringAdmin; 21 | private readonly AppConfiguration _appConfiguration = appConfiguration.Value; 22 | private readonly Telemetry _telemetry = telemetry; 23 | 24 | internal static AsyncLocal<(DbConfiguration Admin, DbConfiguration User)> Configurations { get; } = new(); 25 | 26 | public async Task StartAsync(CancellationToken cancellationToken) 27 | { 28 | using var activity = _telemetry.Activities.StartActivity("Migrator.Run"); 29 | try 30 | { 31 | await using var _ = await _locking.AcquireLock(DistributedLockName.DbMigrator, cancellationToken); 32 | 33 | Configurations.Value = (_appConfiguration.DbAdmin, _appConfiguration.Db); 34 | 35 | var upgrader = DeployChanges 36 | .To.PostgresqlDatabase(_connectionStringAdmin.Value) 37 | .JournalToPostgresqlTable(Repository.Schema, "schema_version") 38 | .WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly()) 39 | .LogTo(_logger) 40 | .WithTransaction() 41 | .Build(); 42 | 43 | var result = upgrader.PerformUpgrade(); 44 | 45 | if (!result.Successful) 46 | { 47 | _logger.LogError( 48 | result.Error, 49 | "Failed to upgrade database, using script: {ErrorScript}", 50 | result.ErrorScript.Name 51 | ); 52 | throw new Exception("Failed to upgrade database", result.Error); 53 | } 54 | 55 | foreach (var script in result.Scripts) 56 | _logger.LogInformation("Executed script: {Script}", script.Name); 57 | 58 | _logger.LogInformation("Database upgrade successful"); 59 | } 60 | catch (Exception ex) 61 | { 62 | _logger.LogError(ex, "Failed seeding database"); 63 | throw; 64 | } 65 | } 66 | 67 | public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; 68 | } 69 | 70 | internal sealed class Script0001Initial : IScript 71 | { 72 | public string ProvideScript(Func dbCommandFactory) 73 | { 74 | var (admin, user) = Migrator.Configurations.Value; 75 | 76 | return $""" 77 | ALTER DEFAULT PRIVILEGES FOR USER {admin.Username} IN SCHEMA {Repository.Schema} 78 | GRANT SELECT,INSERT,UPDATE,REFERENCES,DELETE,TRUNCATE,REFERENCES,TRIGGER ON TABLES TO {user.Username}; 79 | 80 | ALTER DEFAULT PRIVILEGES FOR USER {admin.Username} IN SCHEMA {Repository.Schema} 81 | GRANT ALL ON SEQUENCES TO {user.Username}; 82 | 83 | CREATE TABLE {Repository.Tables.Telemetry} ( 84 | id BIGSERIAL PRIMARY KEY, 85 | ext_id TEXT NOT NULL, 86 | service_owner TEXT NOT NULL, 87 | app_name TEXT NOT NULL, 88 | app_version TEXT NOT NULL, 89 | time_generated TIMESTAMPTZ NOT NULL, 90 | time_ingested TIMESTAMPTZ NOT NULL, 91 | dupe_count BIGINT NOT NULL, 92 | seeded BOOLEAN NOT NULL, 93 | data JSONB NOT NULL, 94 | UNIQUE (service_owner, ext_id) 95 | ); 96 | 97 | CREATE INDEX idx_telemetry_time_generated ON {Repository.Tables.Telemetry} (time_generated); 98 | CREATE INDEX idx_telemetry_seeded ON {Repository.Tables.Telemetry} (seeded); 99 | 100 | CREATE TABLE {Repository.Tables.Queries} ( 101 | id BIGSERIAL PRIMARY KEY, 102 | service_owner TEXT NOT NULL, 103 | name TEXT NOT NULL, 104 | hash TEXT NOT NULL, 105 | queried_until TIMESTAMPTZ NOT NULL, 106 | UNIQUE (service_owner, hash) 107 | ); 108 | 109 | CREATE TABLE {Repository.Tables.Alerts} ( 110 | id BIGSERIAL PRIMARY KEY, 111 | state INTEGER NOT NULL, 112 | telemetry_id BIGSERIAL NOT NULL REFERENCES {Repository.Tables.Telemetry} (id), 113 | data JSONB NOT NULL, 114 | created_at TIMESTAMPTZ NOT NULL, 115 | updated_at TIMESTAMPTZ NOT NULL, 116 | UNIQUE (telemetry_id) 117 | ); 118 | 119 | CREATE INDEX idx_alerts_from_telemetry ON {Repository.Tables.Alerts} (telemetry_id, state, (data->>'$type')); 120 | """; 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Slack/_snapshots/SlackAlerterTests.Alerts_Eventually_Successfully_For_Non_Seeded_Telemetry_case=500-error-4-times-then-ok_waitForRetryEvents=1.verified.txt: -------------------------------------------------------------------------------- 1 | { 2 | OrchestratorEvents: [ 3 | { 4 | ServiceOwner: { 5 | Value: skd 6 | }, 7 | Query: { 8 | Name: query, 9 | Type: Traces, 10 | QueryTemplate: template-{searchFrom}-{searchTo}, 11 | Hash: HpynynzeF9Pk52Vahp477Q== 12 | }, 13 | SearchFrom: 2024-11-22T11:59:59Z, 14 | SearchTo: 2025-02-20T11:50:00Z, 15 | Telemetry: [ 16 | { 17 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 18 | ServiceOwner: skd, 19 | AppName: formueinntekt-skattemelding-v2, 20 | AppVersion: 8.0.8, 21 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 22 | TimeIngested: 2025-02-20T12:00:05Z, 23 | Seeded: false, 24 | Data: {Scrubbed} 25 | }, 26 | { 27 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 28 | ServiceOwner: skd, 29 | AppName: formueinntekt-skattemelding-v2, 30 | AppVersion: 8.0.8, 31 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 32 | TimeIngested: 2025-02-20T12:00:05Z, 33 | Seeded: false, 34 | Data: {Scrubbed} 35 | } 36 | ], 37 | Result: { 38 | Written: 1, 39 | Ids: [ 40 | 1, 41 | 5 42 | ], 43 | DupeExtIds: [ 44 | 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1 45 | ] 46 | } 47 | } 48 | ], 49 | AlerterEvents: [ 50 | { 51 | Item: { 52 | Id: 5, 53 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 54 | ServiceOwner: skd, 55 | AppName: formueinntekt-skattemelding-v2, 56 | AppVersion: 8.0.8, 57 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 58 | TimeIngested: 2025-02-20T12:00:05Z, 59 | Seeded: false, 60 | Data: {Scrubbed} 61 | }, 62 | AlertBefore: { 63 | State: Pending, 64 | TelemetryId: 5, 65 | Data: {}, 66 | CreatedAt: {Scrubbed}, 67 | UpdatedAt: {Scrubbed} 68 | } 69 | }, 70 | { 71 | Item: { 72 | Id: 5, 73 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 74 | ServiceOwner: skd, 75 | AppName: formueinntekt-skattemelding-v2, 76 | AppVersion: 8.0.8, 77 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 78 | TimeIngested: 2025-02-20T12:00:05Z, 79 | Seeded: false, 80 | Data: {Scrubbed} 81 | }, 82 | AlertBefore: { 83 | State: Pending, 84 | TelemetryId: 5, 85 | Data: {}, 86 | CreatedAt: {Scrubbed}, 87 | UpdatedAt: {Scrubbed} 88 | }, 89 | AlertAfter: { 90 | State: Alerted, 91 | TelemetryId: 5, 92 | Data: { 93 | Channel: C01UJ9G, 94 | Message: 95 | *ALERT* `2025-02-15T14:56:04Z`: 96 | - App: *skd*/*formueinntekt-skattemelding-v2*/*8.0.8* 97 | - Feil: *POST /storage/api/v1/instances/123/1d449be1-7114-405c-aeee-1f09799f7b74/events* (status *Faulted*, *27478.49ms*) 98 | - Instansen: *123*/*1d449be1-7114-405c-aeee-1f09799f7b74* 99 | - Operation ID: *75563ff0b3251e04c70362c5a3495174*, 100 | ThreadTs: 1634160000.000100 101 | }, 102 | CreatedAt: {Scrubbed}, 103 | UpdatedAt: {Scrubbed} 104 | } 105 | } 106 | ], 107 | State: { 108 | Telemetry: [ 109 | { 110 | Id: 2, 111 | ExtId: 4ba19e3f5a545728934b1f921e06d92b-f86a2aed5c5d9f63, 112 | ServiceOwner: tad, 113 | AppName: bku, 114 | AppVersion: 6.0.35, 115 | TimeGenerated: 2025-02-19T08:29:04.386517Z, 116 | TimeIngested: 2025-02-19T08:34:32.406732Z, 117 | Seeded: true, 118 | Data: {Scrubbed} 119 | }, 120 | { 121 | Id: 3, 122 | ExtId: f2b6eea456788828f6195fbce59f740f-e0135dfc441dc49a, 123 | ServiceOwner: ssb, 124 | AppName: ra1000-01, 125 | AppVersion: 6.0.26, 126 | TimeGenerated: 2025-02-19T16:07:44.720544Z, 127 | TimeIngested: 2025-02-19T16:00:13.785543Z, 128 | Seeded: true, 129 | Data: {Scrubbed} 130 | }, 131 | { 132 | Id: 1, 133 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c1, 134 | ServiceOwner: skd, 135 | AppName: formueinntekt-skattemelding-v2, 136 | AppVersion: 8.0.8, 137 | TimeGenerated: 2025-02-15T14:51:04.906736Z, 138 | TimeIngested: 2025-02-19T07:34:32.420042Z, 139 | DupeCount: 1, 140 | Seeded: true, 141 | Data: {Scrubbed} 142 | }, 143 | { 144 | Id: 5, 145 | ExtId: 75563ff0b3251e04c70362c5a3495174-90c159bde9b1a6c2, 146 | ServiceOwner: skd, 147 | AppName: formueinntekt-skattemelding-v2, 148 | AppVersion: 8.0.8, 149 | TimeGenerated: 2025-02-15T14:56:04.906736Z, 150 | TimeIngested: 2025-02-20T12:00:05Z, 151 | Seeded: false, 152 | Data: {Scrubbed} 153 | } 154 | ], 155 | Alerts: [ 156 | { 157 | Id: 1, 158 | State: Alerted, 159 | TelemetryId: 5, 160 | Data: { 161 | Channel: C01UJ9G, 162 | Message: 163 | *ALERT* `2025-02-15T14:56:04Z`: 164 | - App: *skd*/*formueinntekt-skattemelding-v2*/*8.0.8* 165 | - Feil: *POST /storage/api/v1/instances/123/1d449be1-7114-405c-aeee-1f09799f7b74/events* (status *Faulted*, *27478.49ms*) 166 | - Instansen: *123*/*1d449be1-7114-405c-aeee-1f09799f7b74* 167 | - Operation ID: *75563ff0b3251e04c70362c5a3495174*, 168 | ThreadTs: 1634160000.000100 169 | }, 170 | CreatedAt: {Scrubbed}, 171 | UpdatedAt: {Scrubbed} 172 | } 173 | ] 174 | } 175 | } -------------------------------------------------------------------------------- /AppsMonitoring/test/Altinn.Apps.Monitoring.Tests/Application/Db/_snapshots/RepositoryTests.Insert_Telemetry_Is_Idempotent.verified.txt: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | Desc: First write from clean DB, 4 | Start: 2025-01-01T12:00:00Z, 5 | End: 2025-01-01T12:10:00Z, 6 | StateBefore: { 7 | Telemetry: [], 8 | Queries: [] 9 | }, 10 | Input: { 11 | ServiceOwner: so, 12 | Telemetry: [ 13 | { 14 | ExtId: ext-id, 15 | ServiceOwner: so, 16 | AppName: app-name, 17 | AppVersion: 8.0.0, 18 | TimeGenerated: 2025-01-01T11:45:00Z, 19 | TimeIngested: 2025-01-01T12:00:00Z, 20 | Seeded: false, 21 | Data: {Scrubbed} 22 | } 23 | ] 24 | }, 25 | StateAfter: { 26 | Telemetry: [ 27 | { 28 | Id: 1, 29 | ExtId: ext-id, 30 | ServiceOwner: so, 31 | AppName: app-name, 32 | AppVersion: 8.0.0, 33 | TimeGenerated: 2025-01-01T11:45:00Z, 34 | TimeIngested: 2025-01-01T12:00:00Z, 35 | Seeded: false, 36 | Data: {Scrubbed} 37 | } 38 | ], 39 | Queries: [ 40 | { 41 | Id: 1, 42 | ServiceOwner: so, 43 | Name: query-name, 44 | Hash: lhcdD2d5F62whymwYsw5Sw==, 45 | QueriedUntil: 2025-01-01T11:45:00Z 46 | } 47 | ] 48 | }, 49 | PersistedEntities: 1 50 | }, 51 | { 52 | Desc: Second write with existing data (test for idempotency), 53 | Start: 2025-01-01T12:10:00Z, 54 | End: 2025-01-01T12:20:00Z, 55 | StateBefore: { 56 | Telemetry: [ 57 | { 58 | Id: 1, 59 | ExtId: ext-id, 60 | ServiceOwner: so, 61 | AppName: app-name, 62 | AppVersion: 8.0.0, 63 | TimeGenerated: 2025-01-01T11:45:00Z, 64 | TimeIngested: 2025-01-01T12:00:00Z, 65 | Seeded: false, 66 | Data: {Scrubbed} 67 | } 68 | ], 69 | Queries: [ 70 | { 71 | Id: 1, 72 | ServiceOwner: so, 73 | Name: query-name, 74 | Hash: lhcdD2d5F62whymwYsw5Sw==, 75 | QueriedUntil: 2025-01-01T11:45:00Z 76 | } 77 | ] 78 | }, 79 | Input: { 80 | ServiceOwner: so, 81 | Telemetry: [ 82 | { 83 | ExtId: ext-id, 84 | ServiceOwner: so, 85 | AppName: app-name, 86 | AppVersion: 8.0.0, 87 | TimeGenerated: 2025-01-01T11:45:00Z, 88 | TimeIngested: 2025-01-01T12:10:00Z, 89 | Seeded: false, 90 | Data: {Scrubbed} 91 | } 92 | ] 93 | }, 94 | StateAfter: { 95 | Telemetry: [ 96 | { 97 | Id: 1, 98 | ExtId: ext-id, 99 | ServiceOwner: so, 100 | AppName: app-name, 101 | AppVersion: 8.0.0, 102 | TimeGenerated: 2025-01-01T11:45:00Z, 103 | TimeIngested: 2025-01-01T12:00:00Z, 104 | DupeCount: 1, 105 | Seeded: false, 106 | Data: {Scrubbed} 107 | } 108 | ], 109 | Queries: [ 110 | { 111 | Id: 1, 112 | ServiceOwner: so, 113 | Name: query-name, 114 | Hash: lhcdD2d5F62whymwYsw5Sw==, 115 | QueriedUntil: 2025-01-01T12:00:00Z 116 | } 117 | ] 118 | } 119 | }, 120 | { 121 | Desc: Same data, different service owner, expecting write, 122 | Start: 2025-01-01T12:20:00Z, 123 | End: 2025-01-01T12:30:00Z, 124 | StateBefore: { 125 | Telemetry: [ 126 | { 127 | Id: 1, 128 | ExtId: ext-id, 129 | ServiceOwner: so, 130 | AppName: app-name, 131 | AppVersion: 8.0.0, 132 | TimeGenerated: 2025-01-01T11:45:00Z, 133 | TimeIngested: 2025-01-01T12:00:00Z, 134 | DupeCount: 1, 135 | Seeded: false, 136 | Data: {Scrubbed} 137 | } 138 | ], 139 | Queries: [ 140 | { 141 | Id: 1, 142 | ServiceOwner: so, 143 | Name: query-name, 144 | Hash: lhcdD2d5F62whymwYsw5Sw==, 145 | QueriedUntil: 2025-01-01T12:00:00Z 146 | } 147 | ] 148 | }, 149 | Input: { 150 | ServiceOwner: sot, 151 | Telemetry: [ 152 | { 153 | ExtId: ext-id, 154 | ServiceOwner: sot, 155 | AppName: app-name, 156 | AppVersion: 8.0.0, 157 | TimeGenerated: 2025-01-01T11:45:00Z, 158 | TimeIngested: 2025-01-01T12:20:00Z, 159 | DupeCount: 1, 160 | Seeded: false, 161 | Data: {Scrubbed} 162 | } 163 | ] 164 | }, 165 | StateAfter: { 166 | Telemetry: [ 167 | { 168 | Id: 1, 169 | ExtId: ext-id, 170 | ServiceOwner: so, 171 | AppName: app-name, 172 | AppVersion: 8.0.0, 173 | TimeGenerated: 2025-01-01T11:45:00Z, 174 | TimeIngested: 2025-01-01T12:00:00Z, 175 | DupeCount: 1, 176 | Seeded: false, 177 | Data: {Scrubbed} 178 | }, 179 | { 180 | Id: 3, 181 | ExtId: ext-id, 182 | ServiceOwner: sot, 183 | AppName: app-name, 184 | AppVersion: 8.0.0, 185 | TimeGenerated: 2025-01-01T11:45:00Z, 186 | TimeIngested: 2025-01-01T12:20:00Z, 187 | Seeded: false, 188 | Data: {Scrubbed} 189 | } 190 | ], 191 | Queries: [ 192 | { 193 | Id: 1, 194 | ServiceOwner: so, 195 | Name: query-name, 196 | Hash: lhcdD2d5F62whymwYsw5Sw==, 197 | QueriedUntil: 2025-01-01T12:00:00Z 198 | }, 199 | { 200 | Id: 3, 201 | ServiceOwner: sot, 202 | Name: query-name, 203 | Hash: lhcdD2d5F62whymwYsw5Sw==, 204 | QueriedUntil: 2025-01-01T11:45:00Z 205 | } 206 | ] 207 | }, 208 | PersistedEntities: 1 209 | } 210 | ] -------------------------------------------------------------------------------- /RepoCleanup/Functions/SharedFunctionSnippets.cs: -------------------------------------------------------------------------------- 1 | using RepoCleanup.Infrastructure.Clients.Gitea; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text.RegularExpressions; 6 | using System.Threading.Tasks; 7 | 8 | namespace RepoCleanup.Functions 9 | { 10 | public static class SharedFunctionSnippets 11 | { 12 | private const int HEADER_WIDTH = 80; 13 | 14 | public static void WriteHeader(string header) 15 | { 16 | Console.Clear(); 17 | Console.WriteLine(); 18 | Console.WriteLine("-------------------------------------------------------------------------------"); 19 | Console.WriteLine(CenterText(header, HEADER_WIDTH, '-')); 20 | Console.WriteLine("-------------------------------------------------------------------------------"); 21 | Console.WriteLine(); 22 | } 23 | 24 | public static bool ShouldRepoNameBePrefixedWithOrg() 25 | { 26 | return YesNo("Should repository name be prefixed with {org}-?"); 27 | } 28 | 29 | public static string CollectRepoName() 30 | { 31 | return CollectInput("Provide repository name: "); 32 | } 33 | 34 | public static string CollectTeamName() 35 | { 36 | return CollectInput("Provide team name (must exist): "); 37 | } 38 | 39 | public static string CollectInput(string inputLabel) 40 | { 41 | Console.Write(inputLabel); 42 | var inputValue = Console.ReadLine(); 43 | 44 | return inputValue; 45 | } 46 | 47 | public static async Task> CollectExistingOrgsInfo() 48 | { 49 | List orgs = new List(); 50 | 51 | bool updateAllOrgs = ShouldThisApplyToAllOrgs(); 52 | 53 | if (updateAllOrgs) 54 | { 55 | List organisations = await GiteaService.GetOrganisations(); 56 | orgs.AddRange(organisations.Select(o => o.Username)); 57 | } 58 | else 59 | { 60 | Console.Write("\r\nProvide organisation name: "); 61 | 62 | string name = Console.ReadLine(); 63 | orgs.Add(name); 64 | } 65 | 66 | return orgs; 67 | } 68 | 69 | public static bool ShouldThisApplyToAllOrgs() 70 | { 71 | return YesNo("Should this apply to all organisations?"); 72 | } 73 | 74 | public static Organisation CollectNewOrgInfo() 75 | { 76 | var shortName = CollectShortNameForOrg(); 77 | var fullname = CollectFullNameForOrg(); 78 | var website = CollectWebSiteForOrg(); 79 | 80 | var org = new Organisation 81 | { 82 | Username = shortName, 83 | Fullname = fullname, 84 | Website = website, 85 | Visibility = "public", 86 | RepoAdminChangeTeamAccess = false 87 | }; 88 | 89 | return org; 90 | } 91 | 92 | private static string CollectWebSiteForOrg() 93 | { 94 | var isValid = false; 95 | var website = string.Empty; 96 | 97 | while (!isValid) 98 | { 99 | Console.Write("\r\nSet website for org: "); 100 | website = Console.ReadLine(); 101 | 102 | if (string.IsNullOrEmpty(website)) 103 | { 104 | isValid = true; 105 | } 106 | else 107 | { 108 | isValid = Regex.IsMatch(website, "^[a-zA-Z0-9\\-._/:]*$"); 109 | } 110 | if (!isValid) 111 | { 112 | Console.WriteLine("Invalid website adress. Letters a-z and characters:'-', '_', '.', '/', ':' are permitted."); 113 | } 114 | } 115 | 116 | return website; 117 | } 118 | 119 | private static string CollectFullNameForOrg() 120 | { 121 | return CollectInput("\r\nSet fullname for org: "); 122 | } 123 | 124 | private static string CollectShortNameForOrg() 125 | { 126 | var isValid = false; 127 | var shortName = string.Empty; 128 | while (!isValid) 129 | { 130 | Console.Write("\r\nSet shortname for org: "); 131 | shortName = Console.ReadLine().ToLower(); 132 | 133 | isValid = IsValidOrgShortName(shortName); 134 | if (!isValid) 135 | { 136 | Console.WriteLine("Invalid name. Letters a-z and character '-' are permitted. Username must start with a letter and end with a letter or number."); 137 | } 138 | } 139 | 140 | return shortName; 141 | } 142 | 143 | private static bool IsValidOrgShortName(string shortName) 144 | { 145 | return Regex.IsMatch(shortName, "^[a-z]+[a-z0-9]$"); 146 | } 147 | 148 | public static void ConfirmWithExit(string confirmMessage, string exitMessage) 149 | { 150 | var proceed = YesNo(confirmMessage); 151 | 152 | if (!proceed) 153 | { 154 | Console.WriteLine(exitMessage); 155 | Environment.Exit(0); 156 | } 157 | } 158 | 159 | private static bool YesNo(string question) 160 | { 161 | Console.Write($"{question} (Y)es / (N)o : "); 162 | string yesNo = Console.ReadLine().ToUpper(); 163 | 164 | return yesNo == "Y"; 165 | } 166 | 167 | private static string CenterText(string text, int length, char padChar) 168 | { 169 | int pad = (length - text.Length) / 2; 170 | 171 | int leftPad = pad - 1; 172 | int rightPad = (pad % 2 == 0) ? pad - 1 : pad; 173 | 174 | var left = "".PadLeft(leftPad, padChar); 175 | var right = "".PadRight(rightPad, padChar); 176 | 177 | return $"{left} {text} {right}"; 178 | } 179 | } 180 | } 181 | --------------------------------------------------------------------------------