|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "go.opentelemetry.io/otel/attribute" |
| 6 | + "go.opentelemetry.io/otel/sdk/resource" |
| 7 | + semconv "go.opentelemetry.io/otel/semconv/v1.27.0" |
| 8 | + "os" |
| 9 | +) |
| 10 | + |
| 11 | +const ( |
| 12 | + gitlabCIEnvVar = "GITLAB_CI" |
| 13 | + gitlabPipelineNameEnvVar = "CI_PIPELINE_NAME" |
| 14 | + gitlabPipelineIdEnvVar = "CI_PIPELINE_ID" |
| 15 | + gitlabJobIdEnvVar = "CI_JOB_ID" |
| 16 | + gitlabJobNameEnvVar = "CI_JOB_NAME" |
| 17 | + gitlabJobStageEnvVar = "CI_JOB_STAGE" |
| 18 | + gitlabJobUrlEnvVar = "CI_JOB_URL" |
| 19 | + |
| 20 | + gitlabCommitRefNameEnvVar = "CI_COMMIT_REF_NAME" |
| 21 | + gitlabCommitTagEnvVar = "CI_COMMIT_TAG" |
| 22 | + gitlabMergeRequestIIDEnvVar = "CI_MERGE_REQUEST_IID" |
| 23 | + |
| 24 | + gitlabProjectUrlEnvVar = "CI_PROJECT_URL" |
| 25 | + gitlabProjectIDEnvVar = "CI_PROJECT_ID" |
| 26 | +) |
| 27 | + |
| 28 | +type resourceDetector struct { |
| 29 | +} |
| 30 | + |
| 31 | +// compile time assertion that resourceDetector implements the resource.Detector interface. |
| 32 | +var _ resource.Detector = (*resourceDetector)(nil) |
| 33 | + |
| 34 | +// NewResourceDetector returns a [ResourceDetector] that will detect Gitlab Pipeline resources. |
| 35 | +func NewResourceDetector() resource.Detector { |
| 36 | + return &resourceDetector{} |
| 37 | +} |
| 38 | + |
| 39 | +func (detector *resourceDetector) Detect(_ context.Context) (*resource.Resource, error) { |
| 40 | + |
| 41 | + var attributes []attribute.KeyValue |
| 42 | + |
| 43 | + isGitlabCI := os.Getenv(gitlabCIEnvVar) == "true" |
| 44 | + |
| 45 | + if isGitlabCI { |
| 46 | + attributes = append(attributes, detectCICDAttributes()...) |
| 47 | + attributes = append(attributes, detectVCSAttributes()...) |
| 48 | + } |
| 49 | + |
| 50 | + return resource.NewWithAttributes(semconv.SchemaURL, attributes...), nil |
| 51 | +} |
| 52 | + |
| 53 | +// detectCICDAttributes https://github.com/open-telemetry/semantic-conventions/blob/main/docs/attributes-registry/cicd.md |
| 54 | +func detectCICDAttributes() []attribute.KeyValue { |
| 55 | + var attributes []attribute.KeyValue |
| 56 | + |
| 57 | + ciPipelineName := os.Getenv(gitlabPipelineNameEnvVar) |
| 58 | + if ciPipelineName != "" { |
| 59 | + attributes = append(attributes, attribute.String(string(semconv.CICDPipelineNameKey), ciPipelineName)) |
| 60 | + } |
| 61 | + |
| 62 | + ciJobId := os.Getenv(gitlabJobIdEnvVar) |
| 63 | + if ciJobId != "" { |
| 64 | + attributes = append(attributes, attribute.String(string(semconv.CICDPipelineTaskRunIDKey), ciJobId)) |
| 65 | + } |
| 66 | + |
| 67 | + ciJobName := os.Getenv(gitlabJobNameEnvVar) |
| 68 | + if ciJobName != "" { |
| 69 | + attributes = append(attributes, attribute.String(string(semconv.CICDPipelineTaskNameKey), ciJobName)) |
| 70 | + } |
| 71 | + |
| 72 | + ciJobStage := os.Getenv(gitlabJobStageEnvVar) |
| 73 | + if ciJobStage != "" { |
| 74 | + attributes = append(attributes, attribute.String(string(semconv.CICDPipelineTaskTypeKey), ciJobStage)) |
| 75 | + } |
| 76 | + |
| 77 | + ciPipelineId := os.Getenv(gitlabPipelineIdEnvVar) |
| 78 | + if ciPipelineId != "" { |
| 79 | + attributes = append(attributes, attribute.String(string(semconv.CICDPipelineRunIDKey), ciPipelineId)) |
| 80 | + } |
| 81 | + |
| 82 | + ciPipelineUrl := os.Getenv(gitlabJobUrlEnvVar) |
| 83 | + if ciPipelineUrl != "" { |
| 84 | + attributes = append(attributes, attribute.String(string(semconv.CICDPipelineTaskRunURLFullKey), ciPipelineUrl)) |
| 85 | + } |
| 86 | + return attributes |
| 87 | +} |
| 88 | + |
| 89 | +// detectVCSAttributes https://github.com/open-telemetry/semantic-conventions/blob/main/docs/attributes-registry/vcs.md |
| 90 | +func detectVCSAttributes() []attribute.KeyValue { |
| 91 | + var attributes []attribute.KeyValue |
| 92 | + |
| 93 | + ciRefName := os.Getenv(gitlabCommitRefNameEnvVar) |
| 94 | + if ciRefName != "" { |
| 95 | + attributes = append(attributes, attribute.String(string(semconv.VCSRepositoryRefNameKey), ciRefName)) |
| 96 | + } |
| 97 | + |
| 98 | + ciTag := os.Getenv(gitlabCommitTagEnvVar) |
| 99 | + if ciTag != "" { |
| 100 | + attributes = append(attributes, semconv.VCSRepositoryRefTypeTag) |
| 101 | + } else { |
| 102 | + attributes = append(attributes, semconv.VCSRepositoryRefTypeBranch) |
| 103 | + } |
| 104 | + |
| 105 | + mrID := os.Getenv(gitlabMergeRequestIIDEnvVar) |
| 106 | + if mrID != "" { |
| 107 | + attributes = append(attributes, attribute.String(string(semconv.VCSRepositoryChangeIDKey), mrID)) |
| 108 | + } |
| 109 | + |
| 110 | + projectUrl := os.Getenv(gitlabProjectUrlEnvVar) |
| 111 | + if projectUrl != "" { |
| 112 | + attributes = append(attributes, attribute.String(string(semconv.VCSRepositoryURLFullKey), projectUrl)) |
| 113 | + } |
| 114 | + |
| 115 | + // There is no SemConv for the ProjectID var |
| 116 | + //projectID := os.Getenv(gitlabProjectIDEnvVar) |
| 117 | + //if projectID != "" { |
| 118 | + // attributes = append(attributes, attribute.String(string(semconv.VCSRepositoryProjectID), projectID)) |
| 119 | + //} |
| 120 | + |
| 121 | + return attributes |
| 122 | +} |
0 commit comments