Copilot commented on code in PR #240: URL: https://github.com/apache/skywalking-go/pull/240#discussion_r3040128500
########## plugins/sql/pgxstdlib/interceptor.go: ########## @@ -0,0 +1,127 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package pgxstdlib + +import ( + "strconv" + "strings" + + "github.com/jackc/pgx/v5" + + "github.com/apache/skywalking-go/plugins/core/operator" + "github.com/apache/skywalking-go/plugins/core/tracing" +) + +const postgreSQLComponentID int32 = 22 + +type OpenConnectorInterceptor struct { +} + +func (i *OpenConnectorInterceptor) BeforeInvoke(invocation operator.Invocation) error { + return nil +} + +func (i *OpenConnectorInterceptor) AfterInvoke(invocation operator.Invocation, results ...interface{}) error { + if tracing.GetRuntimeContextValue("needInfo") != true { + return nil + } + info := i.buildDBInfo(invocation) + if info != nil { + tracing.SetRuntimeContextValue("info", info) + } Review Comment: This interceptor hard-codes the runtime-context keys ("needInfo" / "info"). The SQL entry interceptor already defines these keys and clears them after `sql.Open`; using shared constants (or at least package-level consts) would reduce the risk of silent breakage if the key names are ever refactored. ########## plugins/gorm/postgres/interceptor.go: ########## @@ -0,0 +1,162 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package postgres + +import ( + "strconv" + "strings" + + "github.com/jackc/pgx/v5" + "gorm.io/driver/postgres" + + "github.com/apache/skywalking-go/plugins/core/operator" +) + +const postgreSQLComponentID int32 = 22 +const postgreSQLDBType = "PostgreSQL" + +type sqlDatabaseInfo interface { + DBType() string + ComponentID() int32 + Peer() string +} + +func buildDBInfoFromDialector(dial *postgres.Dialector) *DatabaseInfo { + if dial == nil { + return nil + } + return buildDBInfoFromConfig(dial.Config) +} + +func buildDBInfoFromDialectorValue(dial postgres.Dialector) *DatabaseInfo { + return buildDBInfoFromConfig(dial.Config) +} + +func buildDBInfoFromConfig(config *postgres.Config) *DatabaseInfo { + if config == nil { + return nil + } + if config.DSN != "" { + cfg, err := pgx.ParseConfig(config.DSN) + if err == nil { + peer := buildPeerAddress(cfg) + if peer != "" { + return &DatabaseInfo{PeerAddress: peer} + } + } + } + return buildDBInfoFromConn(config.Conn) +} + +type DatabaseInfo struct { + PeerAddress string +} + +func (d *DatabaseInfo) Type() string { + return postgreSQLDBType +} + +func (d *DatabaseInfo) DBType() string { + return postgreSQLDBType +} + +func (d *DatabaseInfo) ComponentID() int32 { + return postgreSQLComponentID +} + +func (d *DatabaseInfo) Peer() string { + return d.PeerAddress +} + +func buildPeerAddress(cfg *pgx.ConnConfig) string { + if cfg == nil { + return "" + } + addresses := make([]string, 0, len(cfg.Fallbacks)+1) + addresses = appendPeerAddress(addresses, cfg.Host, cfg.Port) + for _, fallback := range cfg.Fallbacks { + if fallback == nil { + continue + } + addresses = appendPeerAddress(addresses, fallback.Host, fallback.Port) + } + return strings.Join(addresses, ",") +} + +func appendPeerAddress(addresses []string, host string, port uint16) []string { + if host == "" { + return addresses + } + address := host + ":" + strconv.Itoa(int(port)) + if strings.HasPrefix(host, "/") { + if strings.HasSuffix(host, "/") { + address = host + ".s.PGSQL." + strconv.Itoa(int(port)) + } else { + address = host + "/.s.PGSQL." + strconv.Itoa(int(port)) + } + } else if strings.Count(host, ":") > 1 && !strings.HasPrefix(host, "[") { + address = "[" + host + "]:" + strconv.Itoa(int(port)) + } + for _, existed := range addresses { + if existed == address { + return addresses + } + } + return append(addresses, address) +} Review Comment: `buildPeerAddress`/`appendPeerAddress` logic is duplicated here and in `plugins/sql/pgxstdlib/interceptor.go`. Duplicated peer formatting logic risks divergence (e.g., IPv6 / unix-socket / fallbacks handling) and makes future fixes easy to miss. Consider extracting a shared helper (or a small internal package) so both SQL and GORM PostgreSQL instrumentation use the exact same peer-building implementation. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
