Skip to content

Commit

Permalink
Merge pull request #3 from erajayatech/refactor/load-env
Browse files Browse the repository at this point in the history
refactor: remove load .env file from package
  • Loading branch information
zidni722 committed Nov 13, 2023
2 parents 37ae019 + cdb5590 commit 73a860e
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 24 deletions.
15 changes: 11 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
# go-opentelemetry

<a name="v1.2.0"></a>
## [v1.2.0] - 2023-10-19
### Code Improvements
- remove load .env file from package


<a name="v1.1.0"></a>
## [v1.1.0] - 2023-08-23
### Other Improvements
- update changelog

### Features
- add new otel start for worker

### Other Improvements
- update changelog


<a name="v1.0.3"></a>
## [v1.0.3] - 2022-10-12
Expand All @@ -21,7 +27,8 @@
<a name="v1.0.0"></a>
## v1.0.0 - 2022-10-10

[Unreleased]: https://github.com/erajayatech/go-opentelemetry/compare/v1.1.0...HEAD
[Unreleased]: https://github.com/erajayatech/go-opentelemetry/compare/v1.2.0...HEAD
[v1.2.0]: https://github.com/erajayatech/go-opentelemetry/compare/v1.1.0...v1.2.0
[v1.1.0]: https://github.com/erajayatech/go-opentelemetry/compare/v1.0.3...v1.1.0
[v1.0.3]: https://github.com/erajayatech/go-opentelemetry/compare/v1.0.2...v1.0.3
[v1.0.2]: https://github.com/erajayatech/go-opentelemetry/compare/v1.0.1...v1.0.2
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/erajayatech/go-opentelemetry
go 1.18

require (
github.com/agiledragon/gomonkey/v2 v2.10.1
github.com/gin-gonic/gin v1.9.0
github.com/golang/mock v1.6.0
github.com/joho/godotenv v1.4.0
github.com/spf13/cast v1.5.1
go.opentelemetry.io/otel v1.10.0
go.opentelemetry.io/otel/exporters/jaeger v1.10.0
Expand Down
9 changes: 7 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/agiledragon/gomonkey/v2 v2.10.1 h1:FPJJNykD1957cZlGhr9X0zjr291/lbazoZ/dmc4mS4c=
github.com/agiledragon/gomonkey/v2 v2.10.1/go.mod h1:ap1AmDzcVOAz1YpeJ3TCzIgstoaWLA6jbbgxfB4w2iY=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
Expand Down Expand Up @@ -151,18 +153,18 @@ github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm4
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+vpHVxEJEs9eg=
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/grpc-ecosystem/grpc-gateway v1.16.0/go.mod h1:BDjrQk3hbvj6Nolgz8mAMFbcEtjT1g+wF4CSlocrBnw=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0 h1:BZHcxBETFHIdVyhyEfOvn/RdU/QGdLI4y34qQGjGWO0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.7.0/go.mod h1:hgWBS7lorOAVIJEQMi4ZsPv9hVvWI6+ch50m39Pf2Ks=
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/joho/godotenv v1.4.0 h1:3l4+N6zfMWnkbPEXKng2o2/MR5mSwTrBih4ZEkkz1lg=
github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand All @@ -187,6 +189,8 @@ github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/cast v1.5.1 h1:R+kOtfhWQE6TVQzY+4D7wJLBgkdVasCEFxSUBYBYIlA=
github.com/spf13/cast v1.5.1/go.mod h1:b9PdjNptOpzXr7Rq1q9gJML/2cdGQAo69NKzQ10KN48=
Expand Down Expand Up @@ -378,6 +382,7 @@ golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190312170243-e65039ee4138/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190328211700-ab21143f2384/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190506145303-2d16b83fe98c/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
Expand Down
23 changes: 15 additions & 8 deletions helper.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,28 @@
package goopentelemetry

import (
"context"
"fmt"
"github.com/joho/godotenv"
"github.com/agiledragon/gomonkey/v2"
"github.com/spf13/cast"
"log"
"go.opentelemetry.io/otel/trace"
"os"
"runtime"
"strings"
)

// TracerPatch for unit test monkey patch
func TracerPatch() (ctx context.Context, reset func()) {
ctx = context.Background()
tracerPatch := gomonkey.ApplyFunc(NewSpan, func(ctx context.Context, name, operation string) (context.Context, trace.Span) {
return ctx, trace.SpanFromContext(ctx)
})

return ctx, func() {
tracerPatch.Reset()
}
}

func GetEnvOrDefault(key string, defaultValue interface{}) interface{} {
value := os.Getenv(key)
if len(value) == 0 {
Expand All @@ -19,12 +32,6 @@ func GetEnvOrDefault(key string, defaultValue interface{}) interface{} {
}

func GetEnv(key string) string {
err := godotenv.Load()
if err != nil {
log.Println("Cannot load file .env: ", err)
panic(err)
}

value := GetEnvOrDefault(key, "").(string)
return value
}
Expand Down
26 changes: 17 additions & 9 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ type (
}

otelTracer struct {
env string
version string
service string
sampled bool
jaegerUrl string
env string
version string
service string
sampled bool
jaegerUrl string
exporterEndpoint *string
apiKey *string
}
)

Expand Down Expand Up @@ -110,10 +112,16 @@ func (ot *otelTracer) tracerProviderJaeger() (*tracesdk.TracerProvider, error) {
// about the application.
func (ot *otelTracer) tracerProviderNewRelic(ctx context.Context) (*tracesdk.TracerProvider, error) {
// Create the NewRelic exporter
exporter, err := otlptrace.New(
ctx,
otlptracegrpc.NewClient(),
)
var options []otlptracegrpc.Option

if ot.exporterEndpoint != nil && ot.apiKey != nil {
options = append(options, otlptracegrpc.WithEndpoint(*ot.exporterEndpoint))
options = append(options, otlptracegrpc.WithHeaders(map[string]string{
"api-key": *ot.apiKey,
}))
}

exporter, err := otlptrace.New(ctx, otlptracegrpc.NewClient(options...))
if err != nil {
return nil, err
}
Expand Down
12 changes: 12 additions & 0 deletions tracer_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ func IsSampledEnable(isEnabled bool) OtelTracerOptionFunc {
ot.sampled = isEnabled
}
}

func SetExporterEndpoint(endpoint string) OtelTracerOptionFunc {
return func(ot *otelTracer) {
ot.exporterEndpoint = &endpoint
}
}

func SetApiKey(key string) OtelTracerOptionFunc {
return func(ot *otelTracer) {
ot.apiKey = &key
}
}

0 comments on commit 73a860e

Please sign in to comment.