Skip to content
This repository has been archived by the owner on Oct 3, 2023. It is now read-only.

Commit

Permalink
Add support for resource detector option. (#70)
Browse files Browse the repository at this point in the history
* Add support for resource detector option.

* fix copyright and remove setting reconnection period.

* rename WithResource to WithResourceDetector
  • Loading branch information
rghetia committed Jun 21, 2019
1 parent 88230df commit f912916
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module contrib.go.opencensus.io/exporter/ocagent
require (
github.com/census-instrumentation/opencensus-proto v0.2.0 // this is to match the version used in census-instrumentation/opencensus-service
github.com/golang/protobuf v1.3.1
github.com/google/go-cmp v0.3.0
github.com/grpc-ecosystem/grpc-gateway v1.8.5 // indirect
go.opencensus.io v0.22.0
google.golang.org/api v0.5.0
Expand Down
16 changes: 15 additions & 1 deletion ocagent.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type Exporter struct {
nodeInfo *commonpb.Node
grpcClientConn *grpc.ClientConn
reconnectionPeriod time.Duration
resourceDetector resource.Detector
resource *resourcepb.Resource
compressor string
headers map[string]string
Expand Down Expand Up @@ -125,7 +126,17 @@ func NewUnstartedExporter(opts ...ExporterOption) (*Exporter, error) {
viewDataBundler.BundleCountThreshold = 500 // TODO: (@odeke-em) make this configurable.
e.viewDataBundler = viewDataBundler
e.nodeInfo = NodeWithStartTime(e.serviceName)
e.resource = resourceProtoFromEnv()
if e.resourceDetector != nil {
res, err := e.resourceDetector(context.Background())
if err != nil {
panic(fmt.Sprintf("Error detecting resource. err:%v\n", err))
}
if res != nil {
e.resource = resourceToResourcePb(res)
}
} else {
e.resource = resourceProtoFromEnv()
}

return e, nil
}
Expand Down Expand Up @@ -512,7 +523,10 @@ func resourceProtoFromEnv() *resourcepb.Resource {
if rs == nil {
return nil
}
return resourceToResourcePb(rs)
}

func resourceToResourcePb(rs *resource.Resource) *resourcepb.Resource {
rprs := &resourcepb.Resource{
Type: rs.Type,
}
Expand Down
17 changes: 17 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package ocagent
import (
"time"

"go.opencensus.io/resource"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
Expand All @@ -30,6 +31,22 @@ type ExporterOption interface {
withExporter(e *Exporter)
}

type resourceDetector resource.Detector

var _ ExporterOption = (*resourceDetector)(nil)

func (rd resourceDetector) withExporter(e *Exporter) {
e.resourceDetector = resource.Detector(rd)
}

// WithResourceDetector allows one to register a resource detector. Resource Detector is used
// to detect resources associated with the application. Detected resource is exported
// along with the metrics. If the detector fails then it panics.
// If a resource detector is not provided then by default it detects from the environment.
func WithResourceDetector(rd resource.Detector) ExporterOption {
return resourceDetector(rd)
}

type insecureGrpcConnection int

var _ ExporterOption = (*insecureGrpcConnection)(nil)
Expand Down
54 changes: 54 additions & 0 deletions resource_detector_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright 2019, OpenCensus Authors
//
// 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 ocagent

import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"go.opencensus.io/resource"

resourcepb "github.com/census-instrumentation/opencensus-proto/gen-go/resource/v1"
)

func TestResourceDetector(t *testing.T) {
ocexp, err := NewExporter(
WithInsecure(),
WithAddress(":0"),
WithResourceDetector(customResourceDetector),
)
if err != nil {
t.Fatalf("Failed to create the ocagent exporter: %v", err)
}
defer ocexp.Stop()

got := ocexp.resource
want := &resourcepb.Resource{
Type: "foo",
Labels: map[string]string{},
}
if !cmp.Equal(got, want) {
t.Fatalf("Resource detection failed. got %v, want %v\n", got, want)
}
}

func customResourceDetector(context.Context) (*resource.Resource, error) {
res := &resource.Resource{
Type: "foo",
Labels: map[string]string{},
}
return res, nil
}

0 comments on commit f912916

Please sign in to comment.