Skip to content

Commit

Permalink
Merge pull request #27 from newrelic-experimental/custom_atts
Browse files Browse the repository at this point in the history
FR: Add custom attributes to logs and traces
  • Loading branch information
dpacheconr authored Oct 10, 2024
2 parents 2dc728f + 69ac005 commit 9483d54
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 0 deletions.
Binary file modified .DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ env:
NEW_RELIC_LICENSE_KEY: ${{ secrets.NEW_RELIC_LICENSE_KEY }}
GHA_RUN_ID: ${{ github.event.workflow_run.id }}
GHA_RUN_NAME: ${{ github.event.workflow_run.name }}
#GHA_CUSTOM_ATTS: '{"mycustomattributea":"test", "mycustomattributeb":10, "mycustomattributec":"My custom attribute"}'
jobs:
Expand Down
1 change: 1 addition & 0 deletions new-relic-exporter.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ env:
NEW_RELIC_API_KEY: ${{ secrets.NEW_RELIC_API_KEY }}
GHA_RUN_ID: ${{ github.event.workflow_run.id }}
GHA_RUN_NAME: ${{ github.event.workflow_run.name }}
#GHA_CUSTOM_ATTS: '{"mycustomattributea":"test", "mycustomattributeb":10, "mycustomattributec":"My custom attribute"}'

jobs:
new-relic-exporter:
Expand Down
22 changes: 22 additions & 0 deletions src/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,21 @@
"github.resource.type": "span"
}

# Example: GHA_CUSTOM_ATTS: '{"mycustomattributea":"test", "mycustomattributeb":10, "mycustomattributec":"My custom attribute"}'
# Check for custom attributes
if "GHA_CUSTOM_ATTS" in os.environ:
GHA_CUSTOM_ATTS = os.environ["GHA_CUSTOM_ATTS"]
else:
GHA_CUSTOM_ATTS = ""


if GHA_CUSTOM_ATTS != "":
try:
global_attributes.update(json.loads(GHA_CUSTOM_ATTS))
except:
print("Error parsing GHA_CUSTOM_ATTS check your configuration, continuing without custom attributes")
pass

#Set workflow level tracer and logger
global_resource = Resource(attributes=global_attributes)
tracer = get_tracer(endpoint, headers, global_resource, "tracer")
Expand Down Expand Up @@ -123,7 +137,15 @@
print("Processing step ->",step['name'],"from job",job['name'])
# Set steps tracer and logger
resource_attributes ={SERVICE_NAME: GHA_SERVICE_NAME,"github.source": "github-exporter","github.resource.type": "span","workflow_run_id": GHA_RUN_ID}
# Add custom attributes if they exist
if GHA_CUSTOM_ATTS != "":
try:
resource_attributes.update(json.loads(GHA_CUSTOM_ATTS))
except:
print("Error parsing GHA_CUSTOM_ATTS check your configuration, continuing without custom attributes")
pass
resource_log = Resource(attributes=resource_attributes)

step_tracer = get_tracer(endpoint, headers, resource_log, "step_tracer")

resource_attributes.update(create_resource_attributes(parse_attributes(step,"","step"),GHA_SERVICE_NAME))
Expand Down

0 comments on commit 9483d54

Please sign in to comment.