|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package tel |
| 16 | + |
| 17 | +import "context" |
| 18 | + |
| 19 | +const ( |
| 20 | + meterName = "alloydb.googleapis.com/client/connector" |
| 21 | + monitoredResource = "cloudsql.googleapis.com/InstanceClient" |
| 22 | + connectLatency = "connect_latencies" |
| 23 | + closedConnectionCount = "closed_connection_count" |
| 24 | + openConnections = "open_connections" |
| 25 | + |
| 26 | + // The identifier of the GCP project associated with this CSQL resource |
| 27 | + ResourceContainer = "resource_container" |
| 28 | + // The Cloud SQL instance identifier in the format of [project_name:instance_name] |
| 29 | + ResourceID = "resource_id" |
| 30 | + // A unique identifier generated for each Dialer instance |
| 31 | + ClientUID = "client_uid" |
| 32 | + // The application name provided by the user or defaulted by the connector |
| 33 | + ApplicationName = "application_name" |
| 34 | + // Cloud SQL Instance's location e.g. us-central1 |
| 35 | + Region = "region" |
| 36 | + // ClientRegion is the region from which the client is connecting, unknown if not on GCP |
| 37 | + ClientRegion = "client_region" |
| 38 | + // ComputePlatform is the platform on which the client is running, e.g. GCE, GKE, etc. |
| 39 | + ComputePlatform = "compute_platform" |
| 40 | + // Cloud SQL Connector type. "go" in this case. |
| 41 | + ConnectorType = "connector_type" |
| 42 | + // Cloud SQL Connector version |
| 43 | + ConnectorVersion = "connector_version" |
| 44 | + // Database engine type [MySQL, PostgreSQL, SQL Server]. |
| 45 | + DatabaseEngineType = "database_engine_type" |
| 46 | + // authType is one of iam or built-in |
| 47 | + authType = "auth_type" |
| 48 | + // IP address type of the connection, one of [public, psa, psc] |
| 49 | + ipType = "ip_type" |
| 50 | + // status indicates whether the dial attempt succeeded or not. |
| 51 | + status = "status" |
| 52 | +) |
| 53 | + |
| 54 | +// Config holds all the necessary information to configure a MetricRecorder. |
| 55 | +type Config struct { |
| 56 | + // Enabled specifies whether the metrics should be enabled. |
| 57 | + Enabled bool |
| 58 | + // Project id |
| 59 | + ResourceContainer string |
| 60 | + // The Cloud SQL instance identifier in the format of [project_name:instance_name] |
| 61 | + ResourceID string |
| 62 | + // A unique identifier generated for each Dialer instance |
| 63 | + ClientUID string |
| 64 | + // The application name provided by the user or defaulted by the connector |
| 65 | + ApplicationName string |
| 66 | + // Cloud SQL Instance's location e.g. us-central1 |
| 67 | + Region string |
| 68 | + // ClientRegion is the region from which the client is connecting, unknown if not on GCP |
| 69 | + ClientRegion string |
| 70 | + // ComputePlatform is the platform on which the client is running, e.g. GCE, GKE, etc. |
| 71 | + ComputePlatform string |
| 72 | + // Cloud SQL Connector type. "go" in this case. |
| 73 | + ConnectorType string |
| 74 | + // Cloud SQL Connector version |
| 75 | + ConnectorVersion string |
| 76 | + // Database engine type [MySQL, PostgreSQL, SQL Server]. |
| 77 | + DatabaseEngineType string |
| 78 | +} |
| 79 | + |
| 80 | +// Attributes holds all the various pieces of metadata to attach to a metric. |
| 81 | +type Attributes struct { |
| 82 | + // IAMAuthN specifies whether IAM authentication is enabled. |
| 83 | + IAMAuthN bool |
| 84 | + // UserAgent is the full user-agent of the alloydbconn.Dialer. |
| 85 | + UserAgent string |
| 86 | + // CacheHit specifies whether connection info was present in the cache. |
| 87 | + CacheHit bool |
| 88 | + // DialStatus specifies the result of the dial attempt. |
| 89 | + DialStatus string |
| 90 | +} |
| 91 | + |
| 92 | +// MetricRecorder defines the interface for recording metrics related to the |
| 93 | +// internal operations of alloydbconn.Dialer. |
| 94 | +type MetricRecorder interface { |
| 95 | + RecordOpenConnection(context.Context, Attributes) |
| 96 | + RecordClosedConnectionCount(context.Context, Attributes) |
| 97 | + RecordConnectLatencies(context.Context, Attributes) |
| 98 | +} |
0 commit comments