Skip to content

Commit

Permalink
Added go core implementation (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Thiyagu55 committed Sep 30, 2022
1 parent 89887ed commit a35d545
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 4 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/go-sql-tests.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
name: go-sql package test
name: go packages test

on:
push:
branches:
- master
paths:
- go/go-sql/**
- go/**
pull_request:
paths:
- go/go-sql/**
- go/**

jobs:
unittests:
Expand All @@ -25,7 +25,7 @@ jobs:
run: go build -v ./...
working-directory: ./go/go-sql

- name: Test
- name: Test go-sql
run: go test -v ./...
working-directory: ./go/go-sql

Expand Down
28 changes: 28 additions & 0 deletions go/core/README.md
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
}
```


99 changes: 99 additions & 0 deletions go/core/core.go
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
}
7 changes: 7 additions & 0 deletions go/core/go.mod
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
11 changes: 11 additions & 0 deletions go/core/go.sum
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=

0 comments on commit a35d545

Please sign in to comment.