-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
149 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# SQLCommenter Core [In development] | ||
|
||
SQLcommenter is a plugin/middleware/wrapper to augment application related information/tags with SQL Statements that can be used later to correlate user code with SQL statements. | ||
|
||
This package contains configuration options, framework interface and support functions for all the sqlcommenter go modules | ||
|
||
## Installation | ||
|
||
This is a support package and will be installed indirectly by other go sqlcommenter packages | ||
|
||
## Usages | ||
|
||
### Configuration | ||
|
||
Users are given control over what tags they want to append by using `core.CommenterOptions` struct. | ||
|
||
```go | ||
type CommenterOptions struct { | ||
EnableDBDriver bool | ||
EnableTraceparent bool // OpenTelemetry trace information | ||
EnableRoute bool // applicable for web frameworks | ||
EnableFramework bool // applicable for web frameworks | ||
EnableController bool // applicable for web frameworks | ||
EnableAction bool // applicable for web frameworks | ||
} | ||
``` | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// Licensed 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 core | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/url" | ||
"reflect" | ||
"runtime" | ||
"sort" | ||
"strings" | ||
|
||
"go.opentelemetry.io/otel/propagation" | ||
) | ||
|
||
const ( | ||
Route string = "route" | ||
Controller string = "controller" | ||
Action string = "action" | ||
Framework string = "framework" | ||
Driver string = "driver" | ||
Traceparent string = "traceparent" | ||
) | ||
|
||
type CommenterOptions struct { | ||
EnableDBDriver bool | ||
EnableRoute bool | ||
EnableFramework bool | ||
EnableController bool | ||
EnableAction bool | ||
EnableTraceparent bool | ||
} | ||
|
||
func encodeURL(k string) string { | ||
return url.QueryEscape(string(k)) | ||
} | ||
|
||
func GetFunctionName(i interface{}) string { | ||
if i == nil { | ||
return "" | ||
} | ||
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name() | ||
} | ||
|
||
func ConvertMapToComment(tags map[string]string) string { | ||
var sb strings.Builder | ||
i, sz := 0, len(tags) | ||
|
||
//sort by keys | ||
sortedKeys := make([]string, 0, len(tags)) | ||
for k := range tags { | ||
sortedKeys = append(sortedKeys, k) | ||
} | ||
sort.Strings(sortedKeys) | ||
|
||
for _, key := range sortedKeys { | ||
if i == sz-1 { | ||
sb.WriteString(fmt.Sprintf("%s=%v", encodeURL(key), encodeURL(tags[key]))) | ||
} else { | ||
sb.WriteString(fmt.Sprintf("%s=%v,", encodeURL(key), encodeURL(tags[key]))) | ||
} | ||
i++ | ||
} | ||
return sb.String() | ||
} | ||
|
||
func ExtractTraceparent(ctx context.Context) propagation.MapCarrier { | ||
// Serialize the context into carrier | ||
textMapPropogator := propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}) | ||
carrier := propagation.MapCarrier{} | ||
textMapPropogator.Inject(ctx, carrier) | ||
return carrier | ||
} | ||
|
||
type RequestExtractor interface { | ||
Route() string | ||
Action() string | ||
Framework() string | ||
} | ||
|
||
func ContextInject(ctx context.Context, h RequestExtractor) context.Context { | ||
ctx = context.WithValue(ctx, Route, h.Route()) | ||
ctx = context.WithValue(ctx, Action, h.Action()) | ||
ctx = context.WithValue(ctx, Framework, h.Framework()) | ||
return ctx | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module github.com/google/sqlcommenter/go/core | ||
|
||
go 1.19 | ||
|
||
require go.opentelemetry.io/otel v1.10.0 | ||
|
||
require go.opentelemetry.io/otel/trace v1.10.0 // indirect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= | ||
github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0= | ||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= | ||
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= | ||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= | ||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= | ||
go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4= | ||
go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ= | ||
go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E= | ||
go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM= | ||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= |