{
  "repo": "tidbcloud/cost-provider",
  "binary": "cost-provider",
  "binary_path": "cmd",
  "dependencies": {
    "Runtime CostProviderLifecycle.LoadFlags": {
      "infrastructure": {},
      "services": []
    },
    "Runtime CostProviderLifecycle.ConfigureLogging": {
      "infrastructure": {},
      "services": []
    },
    "Runtime CostProviderLifecycle.LoadConfig": {
      "infrastructure": {
        "filesystem": {
          "conditions": [
            "Always"
          ],
          "operations": [
            {
              "operation": "READ FILE",
              "trigger": "Load the INI configuration before any provider client is initialized",
              "conditions": [
                "configPath comes from --config-file or the default /etc/cost-collector.conf"
              ],
              "implementation": "config.InitViper(configPath) -> viper.ReadInConfig()"
            }
          ]
        }
      },
      "services": []
    },
    "Runtime CostProviderLifecycle.InitializeManager": {
      "infrastructure": {},
      "services": [
        {
          "name": "google-cloud-bigquery",
          "target_interface": "BigQuery client initialization",
          "operation_type": "client_init",
          "target_repo": "googleapis/google-cloud-go",
          "conditions": [
            "Always during startup",
            "gcp.provider_project_id != \"\"",
            "gcp.location != \"\"",
            "gcp.bigquery_table_name != \"\"",
            "gcp.bill_project_id != \"\""
          ],
          "operations": [
            {
              "operation": "CREATE CLIENT",
              "trigger": "Build the shared BigQuery client used by later request fan-out",
              "conditions": [
                "All required gcp.* fields are present"
              ],
              "implementation": "gcp.NewBillingClient(providerProjectID, location, sharedParam) -> bigquery.NewClient(ctx, providerProjectID, opts...)"
            }
          ]
        },
        {
          "name": "gcp-workload-identity",
          "target_interface": "pingcap/dbaas GCP auth helper",
          "operation_type": "auth_bootstrap",
          "target_repo": "tidbcloud/dbaas",
          "conditions": [
            "Only when GOOGLE_APPLICATION_CREDENTIALS is empty"
          ],
          "operations": [
            {
              "operation": "LOAD CREDENTIAL OPTIONS",
              "trigger": "Fall back to Kubernetes or web-identity based GCP auth instead of a local JSON credential file",
              "conditions": [
                "os.Getenv(\"GOOGLE_APPLICATION_CREDENTIALS\") == \"\""
              ],
              "implementation": "gcp.NewBillingClient -> gcpauth.InitWebIdentityEnv(); gcpauth.DefaultClientOptions()"
            }
          ]
        },
        {
          "name": "aws-athena",
          "target_interface": "Athena client initialization",
          "operation_type": "client_init",
          "target_repo": "aws/aws-sdk-go",
          "conditions": [
            "Always during startup",
            "aws.output_bucket != \"\"",
            "aws.database != \"\"",
            "aws.table != \"\""
          ],
          "operations": [
            {
              "operation": "CREATE CLIENT",
              "trigger": "Build the shared Athena client used by later request fan-out",
              "conditions": [
                "All required aws.* fields are present"
              ],
              "implementation": "aws.NewBillingClient(outputBucket, waitTime, sharedParam) -> session.NewSession(&aws.Config{}); athena.New(sess)"
            }
          ]
        },
        {
          "name": "cost-provider-manager",
          "target_interface": "CostProviderManager.RegisterProvider",
          "operation_type": "in_process_call",
          "target_repo": "tidbcloud/cost-provider",
          "conditions": [
            "Always during startup after each provider client is created successfully"
          ],
          "operations": [
            {
              "operation": "CALL",
              "trigger": "Register the GCP client in the in-process plugin registry",
              "conditions": [
                "gcp.NewBillingClient(...) succeeded"
              ],
              "implementation": "initManager -> manager.Register(\"gcp\", gcpClient)"
            },
            {
              "operation": "CALL",
              "trigger": "Register the AWS client in the in-process plugin registry",
              "conditions": [
                "aws.NewBillingClient(...) succeeded"
              ],
              "implementation": "initManager -> manager.Register(\"aws\", awsClient)"
            }
          ]
        }
      ]
    },
    "Runtime CostProviderLifecycle.RegisterHTTPRoutes": {
      "infrastructure": {},
      "services": []
    },
    "Runtime CostProviderLifecycle.StartHTTPServer": {
      "infrastructure": {},
      "services": []
    },
    "Runtime CostProviderManager.QueryBill": {
      "infrastructure": {},
      "services": [
        {
          "name": "google-cloud-bigquery",
          "target_interface": "BigQuery SQL query job",
          "operation_type": "sql_query",
          "target_repo": "googleapis/google-cloud-go",
          "conditions": [
            "Always when startup succeeded, because initManager always registers the GCP provider",
            "The request already passed JSON binding and start/end time validation"
          ],
          "operations": [
            {
              "operation": "RUN QUERY JOB",
              "trigger": "Query GCP billing rows for the requested tenant and time window",
              "conditions": [
                "gcp provider is present in manager.plugins"
              ],
              "implementation": "Manager.QueryBill -> GCPBillingClient.GetProviderCost -> ExecProviderCostQuery -> CreateBigquerySQL -> client.Query(sql).Run(ctx)"
            },
            {
              "operation": "WAIT JOB",
              "trigger": "Wait for the BigQuery job to finish before reading result rows",
              "conditions": [
                "The BigQuery job was created successfully"
              ],
              "implementation": "GCPBillingClient.query -> job.Wait(ctx)"
            },
            {
              "operation": "READ ROWS",
              "trigger": "Read all BigQuery result rows into []global.Item",
              "conditions": [
                "The BigQuery job finished without status.Err()"
              ],
              "implementation": "GCPBillingClient.query -> job.Read(ctx); iterator.Next(&row)"
            }
          ]
        },
        {
          "name": "aws-athena",
          "target_interface": "Athena SQL query execution",
          "operation_type": "sql_query",
          "target_repo": "aws/aws-sdk-go",
          "conditions": [
            "Always when startup succeeded, because initManager always registers the AWS provider",
            "The request already passed JSON binding and start/end time validation"
          ],
          "operations": [
            {
              "operation": "START QUERY",
              "trigger": "Submit the Athena SQL job for the requested tenant and time window",
              "conditions": [
                "aws provider is present in manager.plugins"
              ],
              "implementation": "Manager.QueryBill -> AWSBillingClient.GetProviderCost -> ExecProviderCostQuery -> CreateAthenaSQL -> StartQueryExecutionRequest(params).Send()"
            },
            {
              "operation": "POLL RESULTS",
              "trigger": "Poll Athena until the query leaves QUEUED or RUNNING state",
              "conditions": [
                "StartQueryExecution succeeded"
              ],
              "implementation": "AWSBillingClient.query -> GetQueryResultsRequest(params2).Send() with waitTime sleep and InvalidRequestException state retry"
            },
            {
              "operation": "READ PAGINATED RESULTS",
              "trigger": "Read all Athena result pages into []global.Item",
              "conditions": [
                "Athena returns one or more pages"
              ],
              "implementation": "AWSBillingClient.query -> append(output2.ResultSet.Rows...); SetNextToken(nextToken)"
            }
          ]
        },
        {
          "name": "provider-cost-sql-templates",
          "target_interface": "Provider-specific SQL template selection",
          "operation_type": "local_branch",
          "target_repo": "tidbcloud/cost-provider",
          "conditions": [
            "Always, as part of both provider query paths"
          ],
          "operations": [
            {
              "operation": "SELECT TEMPLATE",
              "trigger": "Choose the GCP and AWS SQL template version before each provider query",
              "conditions": [
                "request.DataVersion == \"v2\" uses SQLTemplateV2",
                "otherwise SQLTemplate is used"
              ],
              "implementation": "CreateBigquerySQL(args) / CreateAthenaSQL(args) -> if args.Request.IsDataVersionV2()"
            },
            {
              "operation": "ADD PROJECT FILTER",
              "trigger": "Add a project-level WHERE clause only for project-scoped requests",
              "conditions": [
                "request.ProjectID != \"\""
              ],
              "implementation": "text/template SQL blocks -> {{ if .ProjectID }} ... {{ end }}"
            }
          ]
        }
      ]
    },
    "Runtime CostProviderManager.RegisterProvider": {
      "infrastructure": {},
      "services": []
    },
    "HTTP POST /api/v1/provider/cost": {
      "infrastructure": {},
      "services": [
        {
          "name": "cost-provider-manager",
          "target_interface": "CostProviderManager.QueryBill",
          "operation_type": "in_process_call",
          "target_repo": "tidbcloud/cost-provider",
          "conditions": [
            "c.BindJSON(&request) succeeds",
            "time.Parse(global.TIME_TEMPLATE, request.StartTime) succeeds",
            "time.Parse(global.TIME_TEMPLATE, request.EndTime) succeeds"
          ],
          "operations": [
            {
              "operation": "CALL",
              "trigger": "Fan out the validated request to all registered provider clients and aggregate their results",
              "conditions": [
                "The request body is valid JSON",
                "Both start_time and end_time match global.TIME_TEMPLATE"
              ],
              "implementation": "ProviderCostAPI.GetProviderCost -> t.manager.QueryBill(context.Background(), request)"
            }
          ]
        }
      ]
    },
    "HTTP GET /ping": {
      "infrastructure": {},
      "services": []
    },
    "HTTP GET /version": {
      "infrastructure": {},
      "services": []
    }
  },
  "notes": [
    "CostProviderManager.QueryBill launches one goroutine per registered provider and waits for all of them; a single provider error aborts the whole request with a provider-prefixed error string.",
    "The actual downstream auth and SQL branching is provider-specific: GCP may bootstrap workload identity when GOOGLE_APPLICATION_CREDENTIALS is absent, while AWS relies on the ambient SDK credential chain created by session.NewSession.",
    "config.GetEnvConfig() mentions AWS_REGION, but the startup path in cmd/main.go never calls it, so AWS_REGION is deployment evidence rather than a code-enforced startup requirement in this binary."
  ]
}
