Skip to content

Commit

Permalink
adding comments + linting
Browse files Browse the repository at this point in the history
  • Loading branch information
tbuchaillot committed Jul 19, 2023
1 parent c604463 commit 3db2386
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 4 deletions.
46 changes: 45 additions & 1 deletion trace/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,17 @@ func WithContext(ctx context.Context) Option {
}
}

/*
WithServiceID sets the resource service.id for the tracer provider
This is useful to identify service instance on the trace resource.
Example
provider, err := trace.NewProvider(trace.WithServiceID("instance-id"))
if err != nil {
panic(err)
}
*/
func WithServiceID(id string) Option {
return &opts{
fn: func(tp *traceProvider) {
Expand All @@ -88,14 +99,36 @@ func WithServiceID(id string) Option {
}
}

/*
WithServiceVersion sets the resource service.version for the tracer provider
This is useful to identify service version on the trace resource.
Example
provider, err := trace.NewProvider(trace.WithServiceVersion("v4.0.5"))
if err != nil {
panic(err)
}
*/
func WithServiceVersion(version string) Option {
return &opts{
fn: func(tp *traceProvider) {
tp.resources.id = version
tp.resources.version = version
},
}
}

/*
WithHostDetector sets the resource service.version for the tracer provider
This is will add attributes from the host to the configured resource.
Example
provider, err := trace.NewProvider(trace.WithHostDetector())
if err != nil {
panic(err)
}
*/
func WithHostDetector() Option {
return &opts{
fn: func(tp *traceProvider) {
Expand All @@ -104,6 +137,17 @@ func WithHostDetector() Option {
}
}

/*
WithContainerDetector sets the resource service.version for the tracer provider
This is will add attributes from the container to the configured resource.
Example
provider, err := trace.NewProvider(trace.WithContainerDetector())
if err != nil {
panic(err)
}
*/
func WithContainerDetector() Option {
return &opts{
fn: func(tp *traceProvider) {
Expand Down
28 changes: 28 additions & 0 deletions trace/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,31 @@ func Test_WithConfig(t *testing.T) {
assert.NotNil(t, tp.cfg)
assert.IsType(t, cfg, *tp.cfg)
}

func Test_WithServiceID(t *testing.T) {
tp := &traceProvider{}
WithServiceID("id1").apply(tp)

assert.Equal(t, "id1", tp.resources.id)
}

func Test_WithServiceVersion(t *testing.T) {
tp := &traceProvider{}
WithServiceVersion("v1").apply(tp)

assert.Equal(t, "v1", tp.resources.version)
}

func Test_WithHostDetector(t *testing.T) {
tp := &traceProvider{}
WithHostDetector().apply(tp)

assert.Equal(t, true, tp.resources.withHost)
}

func Test_WithContainerDetector(t *testing.T) {
tp := &traceProvider{}
WithContainerDetector().apply(tp)

assert.Equal(t, true, tp.resources.withContainer)
}
3 changes: 2 additions & 1 deletion trace/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ func resourceFactory(ctx context.Context, resourceName string, cfg resourceConfi
if cfg.id != "" {
attrs = append(attrs, semconv.ServiceInstanceID(cfg.id))
}

if cfg.version != "" {
attrs = append(attrs, semconv.ServiceVersion(cfg.version))
}

opts = append(opts, resource.WithAttributes(attrs...))

if cfg.withContainer {
opts = append(opts, resource.WithContainer())
opts = append(opts, resource.WithContainerID())
}

if cfg.withHost {
Expand Down
3 changes: 1 addition & 2 deletions trace/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func Test_ResourceFactory_base(t *testing.T) {
}

func TestResourceFactory(t *testing.T) {

currentHost, err := os.Hostname()
assert.Nil(t, err)

Expand Down Expand Up @@ -85,7 +84,7 @@ func TestResourceFactory(t *testing.T) {
expectedErr: nil,
},
{
//special scenario to unit test - we cannot see the container attrs here
// special scenario to unit test - we cannot see the container attrs here
name: "Test with container",
resourceName: "testResource",
cfg: resourceConfig{
Expand Down

0 comments on commit 3db2386

Please sign in to comment.