Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
myrotvorets-team committed Sep 26, 2023
0 parents commit 675daf0
Show file tree
Hide file tree
Showing 35 changed files with 5,856 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .c8rc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extension": [".mts"],
"reporter": ["text", "lcov"]
}
3 changes: 3 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**/*.js
**/*.d.ts
node_modules/**
16 changes: 16 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"parserOptions": {
"project": ["./tsconfig.json"],
"sourceType": "module"
},
"extends": [
"@myrotvorets/myrotvorets-ts",
"plugin:mocha/recommended"
],
"root": true,
"env": {
"es2022": true,
"mocha": true,
"node": true
}
}
10 changes: 10 additions & 0 deletions .github/codeql-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: CodeQL Config

paths:
- lib

paths-ignore:
- node_modules

queries:
- uses: security-and-quality
6 changes: 6 additions & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": [
"local>myrotvorets/.github:renovate-config"
]
}
28 changes: 28 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json
name: Build and Test

on:
push:
branches:
- "**"
workflow_dispatch:

permissions:
contents: read

jobs:
build:
name: Build and test (Node ${{ matrix.node.name }})
runs-on: ubuntu-latest
if: ${{ !contains(github.event.head_commit.message, '[ci skip]') || github.event_name == 'workflow_dispatch' }}
strategy:
matrix:
node:
- { name: Current, version: current }
- { name: LTS, version: lts/* }
- { name: Previous LTS, version: lts/-1 }
steps:
- name: Build and test
uses: myrotvorets/composite-actions/build-test-nodejs@master
with:
node-version: ${{ matrix.node.version }}
43 changes: 43 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: CodeQL Analysis

on:
push:
branches:
- master
pull_request:
branches:
- master
paths:
- "lib/**.ts"
- ".github/workflows/codeql-analysis.yml"
schedule:
- cron: '37 8 * * 2'

permissions:
contents: read

jobs:
analyze:
name: Static Code Analysis with CodeQL
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
language:
- javascript
permissions:
actions: read
contents: read
security-events: write
steps:
- name: Checkout repository
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0

- name: Initialize CodeQL
uses: github/codeql-action/init@6a28655e3dcb49cb0840ea372fd6d17733edd8a4 # v2.21.8
with:
languages: ${{ matrix.language }}
config-file: ./.github/codeql-config.yml

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@6a28655e3dcb49cb0840ea372fd6d17733edd8a4 # v2.21.8
26 changes: 26 additions & 0 deletions .github/workflows/dependency-review.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Dependency Review

on:
pull_request:

permissions:
contents: read

jobs:
dependency-review:
runs-on: ubuntu-latest
name: Review Dependencies
steps:
- name: Harden Runner
uses: step-security/harden-runner@8ca2b8b2ece13480cda6dacd3511b49857a23c09 # v2.5.1
with:
egress-policy: block
allowed-endpoints: >
api.github.com:443
github.com:443
- name: Check out the source code
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0

- name: Review dependencies
uses: actions/dependency-review-action@6c5ccdad469c9f8a2996bfecaec55a631a347034 # v3
48 changes: 48 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: Publish Package

on:
release:
types:
- released
workflow_dispatch:
inputs:
gpr:
default: "yes"
description: Publish to GPR?
required: true

permissions:
contents: read

jobs:
prepare:
name: Prepare source code
runs-on: ubuntu-latest
if: github.event_name == 'release' || github.event.inputs.gpr == 'yes'
steps:
- name: Prepare source
uses: myrotvorets/composite-actions/node-prepublish@master

publish:
name: Publish package (${{ matrix.registry }})
runs-on: ubuntu-latest
needs: prepare
permissions:
contents: read
packages: write
statuses: write
strategy:
matrix:
registry:
- gpr
include:
- registry: gpr
secret: GITHUB_TOKEN
registry_url: https://npm.pkg.github.com/
steps:
- name: Publish package
uses: myrotvorets/composite-actions/node-publish@master
with:
node-auth-token: ${{ secrets[matrix.secret] }}
registry-url: ${{ matrix.registry_url }}
if: github.event.inputs[matrix.registry] == 'yes' || github.event_name == 'release'
20 changes: 20 additions & 0 deletions .github/workflows/package-audit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Package Audit

on:
push:
paths:
- package.json
- package-lock.json
- .github/workflows/package-audit.yml
workflow_dispatch:

permissions:
contents: read

jobs:
audit-npm:
name: NPM Audit
runs-on: ubuntu-latest
steps:
- name: Audit with NPM
uses: myrotvorets/composite-actions/node-package-audit@master
34 changes: 34 additions & 0 deletions .github/workflows/push-tag.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Pre-release Testing

on:
push:
tags:
- "**"

permissions:
contents: read

jobs:
build:
name: Build and test
runs-on: ubuntu-latest
steps:
- name: Build and test
uses: myrotvorets/composite-actions/build-test-nodejs@master

release:
name: Prepare the release
runs-on: ubuntu-latest
needs: build
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608 # v4.1.0

- name: Create a release
uses: softprops/action-gh-release@de2c0eb89ae2a093876385947365aca7b0e5f844 # v0.1.15
with:
generate_release_notes: true
env:
GITHUB_TOKEN: ${{ secrets.REPOSITORY_ACCESS_TOKEN }}
28 changes: 28 additions & 0 deletions .github/workflows/sonarscan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: SonarCloud Analysis

on:
push:
branches:
- master
pull_request:
branches:
- master
workflow_dispatch:

permissions:
contents: read

jobs:
build:
name: SonarCloud Scan
runs-on: ubuntu-latest
if: |
github.event_name == 'workflow_dispatch' ||
github.event_name == 'pull_request' && github.event.pull_request.head.repo.full_name == github.event.pull_request.base.repo.full_name && github.event.sender.login != 'dependabot[bot]' ||
github.event_name == 'push' && !contains(github.event.head_commit.message, '[ci skip]')
steps:
- name: Run SonarCloud analysis
uses: myrotvorets/composite-actions/node-sonarscan@master
with:
sonar-token: ${{ secrets.SONAR_TOKEN }}
test-script: 'test:coverage'
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.vscode
/coverage
/dist
/node_modules
/test-report.xml
13 changes: 13 additions & 0 deletions .mocharc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/** @type {import('mocha').MochaOptions} */
module.exports = {
recursive: true,
extension: ['.test.mts'],
'node-option': ['loader=ts-node/esm', 'loader=testdouble', 'no-warnings'],
require: 'mocha.setup.mjs',
reporter: 'mocha-multi',
'reporter-option': [
'spec=-',
process.env.GITHUB_ACTIONS === 'true' ? 'mocha-reporter-gha=-' : null,
process.env.SONARSCANNER === 'true' ? 'mocha-reporter-sonarqube=test-report.xml' : null,
].filter(Boolean),
}
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sign-git-tag=true
4 changes: 4 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package.json
package-lock.json
yarn.lock
dist
7 changes: 7 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"semi": true,
"trailingComma": "all",
"singleQuote": true,
"printWidth": 120,
"tabWidth": 4
}
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 Myrotvorets

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# opentelemetry-resource-detectors

OpenTelemetry resource detectors
36 changes: 36 additions & 0 deletions lib/detectors/dockerdetector.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {
type DetectorSync,
type IResource,
Resource,
type ResourceAttributes,
type ResourceDetectionConfig,
} from '@opentelemetry/resources';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import { getContainerIDFormCGroup, getContainerIDFormCGroup2 } from './utils.mjs';

export class DockerDetector implements DetectorSync {
// eslint-disable-next-line class-methods-use-this
public detect(_config: ResourceDetectionConfig): IResource {
return new Resource({}, DockerDetector.getAsyncAttributes());
}

private static async getAsyncAttributes(): Promise<ResourceAttributes> {
const cid = await DockerDetector.getContainerID();
if (cid) {
return {
[SemanticResourceAttributes.CONTAINER_ID]: cid,
};
}

return {};
}

private static async getContainerID(): Promise<string> {
return (
(await getContainerIDFormCGroup(/\/docker\/([0-9a-f]{12})[0-9a-f]{52}$/u)) ||
getContainerIDFormCGroup2(/\/containers\/([0-9a-f]{12})[0-9a-f]{52}\//u)
);
}
}

export const dockerDetector = new DockerDetector();
Loading

0 comments on commit 675daf0

Please sign in to comment.