Skip to content

Commit 66834e4

Browse files
authored
feat: add instrumentation to address error rates (#600)
1 parent 0fe0f90 commit 66834e4

4 files changed

Lines changed: 66 additions & 1 deletion

File tree

.changeset/sharp-pens-accept.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@slack/slack-github-action": patch
3+
---
4+
5+
feat: add instrumentation to address error rates

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "slack-github-action",
2+
"name": "@slack/slack-github-action",
33
"version": "3.0.2",
44
"private": true,
55
"description": "The official Slack GitHub Action. Use this to send data into your Slack workspace",

src/config.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import os from "node:os";
12
import webapi from "@slack/web-api";
23
import axios from "axios";
4+
import packageJson from "../package.json" with { type: "json" };
35
import Content from "./content.js";
46
import SlackError from "./errors.js";
57
import Logger from "./logger.js";
@@ -119,13 +121,29 @@ export default class Config {
119121
core.getInput("webhook") || process.env.SLACK_WEBHOOK_URL || null,
120122
webhookType: core.getInput("webhook-type"),
121123
};
124+
this.instrument();
122125
this.mask();
123126
this.validate(core);
124127
core.debug(`Gathered action inputs: ${JSON.stringify(this.inputs)}`);
125128
this.content = new Content().get(this);
126129
core.debug(`Parsed request content: ${JSON.stringify(this.content)}`);
127130
}
128131

132+
/**
133+
* Add user agent metadata for instrumentation.
134+
*/
135+
instrument() {
136+
this.webapi.addAppMetadata({
137+
name: packageJson.name,
138+
version: packageJson.version,
139+
});
140+
this.axios.defaults.headers.common["User-Agent"] =
141+
`${packageJson.name.replace("/", ":")}/${packageJson.version} ` +
142+
`axios/${this.axios.VERSION} ` +
143+
`node/${process.version.replace("v", "")} ` +
144+
`${os.platform()}/${os.release()}`;
145+
}
146+
129147
/**
130148
* Hide secret values provided in the inputs from appearing.
131149
*/

test/config.spec.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import assert from "node:assert";
22
import { beforeEach, describe, it } from "node:test";
3+
import webapi from "@slack/web-api";
4+
import sinon from "sinon";
35
import Config from "../src/config.js";
46
import SlackError from "../src/errors.js";
57
import send from "../src/send.js";
@@ -158,6 +160,46 @@ describe("config", () => {
158160
});
159161
});
160162

163+
describe("instrument", () => {
164+
it("adds metadata to webapi with package name and version", async () => {
165+
const stub = sinon.stub();
166+
const original = Object.getOwnPropertyDescriptor(
167+
webapi,
168+
"addAppMetadata",
169+
);
170+
Object.defineProperty(webapi, "addAppMetadata", {
171+
value: stub,
172+
configurable: true,
173+
});
174+
try {
175+
mocks.core.getInput.withArgs("method").returns("chat.postMessage");
176+
mocks.core.getInput.withArgs("token").returns("xoxb-example");
177+
new Config(mocks.core);
178+
assert.ok(stub.calledOnce);
179+
const { name, version } = stub.firstCall.args[0];
180+
assert.equal(name, "@slack/slack-github-action");
181+
assert.ok(version);
182+
} finally {
183+
Object.defineProperty(webapi, "addAppMetadata", original);
184+
}
185+
});
186+
187+
it("adds metadata to webhook with package name and version", async () => {
188+
mocks.core.getInput.withArgs("method").returns("chat.postMessage");
189+
mocks.core.getInput.withArgs("token").returns("xoxb-example");
190+
const config = new Config(mocks.core);
191+
assert.ok(
192+
config.axios.defaults.headers.common["User-Agent"].startsWith(
193+
"@slack:slack-github-action/",
194+
),
195+
);
196+
assert.ok(
197+
config.axios.defaults.headers.common["User-Agent"].length >
198+
"@slack:slack-github-action/".length,
199+
);
200+
});
201+
});
202+
161203
describe("mask", async () => {
162204
it("treats the provided token as a secret", async () => {
163205
mocks.core.getInput.withArgs("token").returns("xoxb-example");

0 commit comments

Comments
 (0)