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

Add resource injection tool #8

Merged
merged 2 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion auto/auto.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

// Detect sequentially runs resource detection from environment varibales, AWS, and GCP.
func Detect(ctx context.Context) (*resource.Resource, error) {
return resource.ChainedDetector(
return resource.MultiDetector(
resource.FromEnv,
gcp.DetectGCEInstance,
aws.DetectEC2Instance,
Expand Down
2 changes: 1 addition & 1 deletion aws/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func DetectEC2Instance(context.Context) (*resource.Resource, error) {
}
return &resource.Resource{
Type: resourcekeys.AWSTypeEC2Instance,
Tags: map[string]string{
Labels: map[string]string{
resourcekeys.AWSKeyEC2Region: doc.Region,
resourcekeys.AWSKeyEC2AccountID: doc.AccountID,
resourcekeys.AWSKeyEC2InstanceID: doc.InstanceID,
Expand Down
1 change: 1 addition & 0 deletions cmd/ocdiscover/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ocdiscover
22 changes: 22 additions & 0 deletions cmd/ocdiscover/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# ocdiscover

This tool runs all known resource discovery mechanisms in the repository and
returns the result encoded in the well-known environment variables.

## Installation

Install the `ocdiscover` tool using:

```bash
go get contrib.go.opencensus.io/resource/cmd/ocdiscover
```

## Usage

To run an arbitrary command with resource information provided via the
OpenCensus environment varibales use:

```bash
env $(ocdiscovery) <command> [ <arg> ... ]
```

18 changes: 18 additions & 0 deletions cmd/ocdiscover/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package main

import (
"context"
"fmt"
"log"

"contrib.go.opencensus.io/resource/auto"
"go.opencensus.io/resource"
)

func main() {
res, err := auto.Detect(context.Background())
if err != nil {
log.Fatalf("detecting resource info failed: %s", err)
}
fmt.Printf("%s='%s' %s='%s'\n", resource.EnvVarType, res.Type, resource.EnvVarLabels, resource.EncodeLabels(res.Labels))
}
10 changes: 5 additions & 5 deletions gcp/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,25 @@ func DetectGCEInstance(context.Context) (*resource.Resource, error) {
return nil, nil
}
res := &resource.Resource{
Type: resourcekeys.GCPTypeGCEInstance,
Tags: map[string]string{},
Type: resourcekeys.GCPTypeGCEInstance,
Labels: map[string]string{},
}
instanceID, err := metadata.InstanceID()
logError(err)
if instanceID != "" {
res.Tags[resourcekeys.GCPKeyGCEInstanceID] = instanceID
res.Labels[resourcekeys.GCPKeyGCEInstanceID] = instanceID
}

projectID, err := metadata.ProjectID()
logError(err)
if projectID != "" {
res.Tags[resourcekeys.GCPKeyGCEProjectID] = projectID
res.Labels[resourcekeys.GCPKeyGCEProjectID] = projectID
}

zone, err := metadata.Zone()
logError(err)
if zone != "" {
res.Tags[resourcekeys.GCPKeyGCEZone] = zone
res.Labels[resourcekeys.GCPKeyGCEZone] = zone
}

return res, nil
Expand Down