Need help in plugin development for go agent and log printing #13113
Replies: 2 comments 2 replies
-
same issue facing while adding couchbase plugin |
Beta Was this translation helpful? Give feedback.
-
@mrproliu Can you help me to to fix the error i try many permutaion combination that did not work for couchbase plugin it have the complex type like
Error : github.com/apache/skywalking-go/agent/reporter/var/folders/7z/gvj6gfw141l1q92bchmtn1gh0000gn/T/go-build1838507083/b046/grpc.go:50:38: undefined: operator.LogOperator github.com/couchbase/gocb/v2panic: parsing go failure: 23:28: expected ')', found '/' (and 3 more errors)
} goroutine 1 [running]: import (
) //go:embed * //skywalking:nocopy func NewInstrument() *Instrument { func (i *Instrument) Name() string { func (i *Instrument) BasePackage() string { func (i *Instrument) VersionChecker(version string) bool { func (i *Instrument) Points() []*instrument.Point { // func (i *Instrument) Points() []*instrument.Point { func (i *Instrument) FS() *embed.FS { |
Beta Was this translation helpful? Give feedback.
-
I wrote the plugin for couchbase datadase for qb statement and query interception just like elastic search plugin it is registering by point method i print the log but in intercepter after and before method not executed while running sample application.
I tried all the steps to print the log for agent it is not printing after auto-instrumentation without my plugin as well i set all the env variables and config file tried please help me in printing log and debug the agent
Also i am getting intermittent issue "
go: finding module for package github.com/bmizerany/assert
go: finding module for package github.com/apache/skywalking-go/plugin/trace
go: found github.com/bmizerany/assert in github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869
go: finding module for package github.com/apache/skywalking-go/plugin/trace
go: github.com/apache/skywalking-go/tools/go-agent/instrument/plugins imports
github.com/apache/skywalking-go/plugin/trace: module github.com/apache/skywalking-go@latest found (v0.5.0), but does not contain package github.com/apache/skywalking-go/plugin/trace"
I tird the steps to use gork sync , replace directory but still this issue is coming
Here is my Instrumentation.go
// 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 couchbase
import (
"embed"
//"fmt"
"log"
"github.com/apache/skywalking-go/plugins/core/instrument"
)
var fs embed.FS
//skywalking:nocopy
type Instrument struct {
}
func NewInstrument() *Instrument {
return &Instrument{}
}
func (i *Instrument) Name() string {
return "couchbase"
}
func (i *Instrument) BasePackage() string {
return "github.com/couchbase/gocb/v2"
}
func (i *Instrument) VersionChecker(version string) bool {
// Return true for all versions of the Couchbase Go SDK
return true
}
func (i *Instrument) Points() []*instrument.Point {
log.Println("[DEBUG] Couchbase plugin Points() method is being executed")
return []*instrument.Point{
{
PackagePath: "github.com/couchbase/gocb/v2", // Correct package path for the Cluster struct
At: instrument.NewMethodEnhance("Cluster", "Query", // Use NewMethodEnhance for instance methods
instrument.WithArgsCount(2), // Query takes 2 arguments: query string and options
instrument.WithArgType(0, "string"), // First argument is the query string
instrument.WithArgType(1, "*QueryOptions"), // Second argument is a pointer to QueryOptions
instrument.WithResultCount(2), // Returns 2 results: QueryResult and error
instrument.WithResultType(0, "*QueryResult"), // First result is QueryResult
instrument.WithResultType(1, "error")), // Second result is error
Interceptor: "QueryInterceptor",
},
}
}
func (i *Instrument) FS() *embed.FS {
return &fs
}
Intercepter.go
package couchbase
import (
"fmt"
"log"
"github.com/apache/skywalking-go/plugins/core/operator"
"github.com/apache/skywalking-go/plugins/core/tracing"
)
type QueryInterceptor struct{}
func (q *QueryInterceptor) BeforeInvoke(invocation operator.Invocation) error {
log.Println("[DEBUG] Couchbase QueryInterceptor BeforeInvoke: Interceptor is running")
// Ensure Args()[0] is a string
query, ok := invocation.Args()[0].(string)
if !ok {
return fmt.Errorf("invalid argument: expected string, got %T", invocation.Args()[0])
}
log.Printf("[DEBUG] Couchbase QueryInterceptor BeforeInvoke: query=%s\n", query)
// Replace "couchbase-host" with the actual Couchbase host
peer := "couchbase-host" // Replace with dynamic value if possible
span, err := tracing.CreateExitSpan("Couchbase/Query", peer, func(headerKey, headerValue string) error {
return nil
}, tracing.WithComponent(43),
tracing.WithLayer(tracing.SpanLayerDatabase),
tracing.WithTag(tracing.TagDBType, "Couchbase"),
tracing.WithTag(tracing.TagDBStatement, query))
if err != nil {
fmt.Printf("[ERROR] Failed to create exit span: %v\n", err)
return err
}
invocation.SetContext(map[string]interface{}{
"span": span,
})
return nil
}
func (q *QueryInterceptor) AfterInvoke(invocation operator.Invocation, result ...interface{}) error {
log.Println("[DEBUG] Couchbase QueryInterceptor AfterInvoke: Interceptor is running")
// Retrieve the span from the context
contextData, ok := invocation.GetContext().(map[string]interface{})
if !ok {
return fmt.Errorf("invalid context: expected map[string]interface{}, got %T", invocation.GetContext())
}
span, ok := contextData["span"].(tracing.Span)
if !ok {
return fmt.Errorf("invalid context: span not found")
}
// Check for errors in the result
if len(result) > 1 && result[1] != nil {
if err, ok := result[1].(error); ok {
fmt.Printf("[DEBUG] Couchbase QueryInterceptor AfterInvoke: error=%v\n", err)
span.Error(err.Error())
}
}
fmt.Println("[DEBUG] Couchbase QueryInterceptor AfterInvoke: ending span")
span.End()
return nil
}
Beta Was this translation helpful? Give feedback.
All reactions